0% found this document useful (0 votes)
3 views

week 1 and 2

Oop

Uploaded by

mianh2221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

week 1 and 2

Oop

Uploaded by

mianh2221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Object Oriented Programming

Week 2

Lecture 1 & 2

What is OOP?

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or functions that perform operations on the
data, while object-oriented programming is about creating objects that contain both data and
functions.

Object-oriented programming has several advantages over procedural programming:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP helps to makes the code easier to maintain, modify and debug
 OOP makes it possible to create full reusable applications with less code and shorter
development time.

Features of Object Oriented Programming:

 Encapsulation: Encapsulation is a process of wrapping data members and member


functions in a single unit called class. Using the method of encapsulation, the
programmer cannot directly access the data. Data is only accessible through the object of
the class.
 Inheritance: Inheritance means access the properties and features of one class into
another class. The class who is going to provide its features to another class will be called
base class and the class who is using the properties and features of another class will be
called derived class.
 Data Abstraction: The basic idea of data abstraction is to visible only the necessary
information, unnecessary information will be hidden from the outside world. This can be

Page 1 of 12
Object Oriented Programming

done by making class members as private members of class. Private members can be
accessed only within the same class where they are declared.
 Polymorphism: Polymorphism means more than one function with same name, with
different working. It can be static or dynamic. In static polymorphism memory will be
allocated at compile time. In dynamic polymorphism memory will be allocated at
runtime. Both function overloading and operator overloading are an examples of static
polymorphism. Virtual function is an example of dynamic polymorphism.

Class : Class is an encapsulation of data and coding. Classes are an expanded version of
structures. Structure can contain multiple variables. Classes can contain multiple variables,
even more, classes can also contain functions as class member. Variables available in class
are called Data Members. Functions available in class are called Member Functions.

Object : Class is a user-defined data type and object is a variable of class type. Object is used
to access class members. Classes and objects are the two main aspects of object-oriented
programming.

Look at the following illustration to see the difference between class and objects:

Another example:

Page 2 of 12
Object Oriented Programming

So, a class is a template for objects, and an object is an instance of a class. When the individual
objects are created, they inherit all the variables and functions from the class.

Declaration of class

Declaration of class must start with the keyword class followed by the class name and class
members are declared within braces.

Page 3 of 12
Object Oriented Programming

Instantiation of object means, create an object of class to access its members. Object is a
variable of class type. Class members are accessed using the dot operator(.) between class's
object and class's member name.

Syntax for creating an object of class

class-name obj;

Example for creating an object of class

Employee Emp;

Defining Class Member Functions


We can define member function inside or outside class.

Page 4 of 12
Object Oriented Programming

Syntax of defining member function outside class

return_type class_name :: function_name(argument list)

----------

body of function

----------

return_type Type of value function will return.

class_name:: A program may contain more than one class and these classes may have similar
member functions. Class_name:: tells the compiler which class the function belongs to and the
scope of the member function is restricted to the class_name.

function_name Can be any valid C++ identifier.

argument list Represents the type and number of value function will take, values are sent by
the calling statement.

Example of defining member function outside class

#include<iostream.h>

#include<conio.h>

class Employee

Page 5 of 12
Object Oriented Programming

int Id;

char Name[25];

int Age;

long Salary;

public:

void GetData();

void PutData();

};

void Employee :: GetData() //Statement 1 : Defining GetData()

cout<<"\n\tEnter Employee Id : ";

cin>>Id;

cout<<"\n\tEnter Employee Name : ";

cin>>Name;

cout<<"\n\tEnter Employee Age : ";

cin>>Age;

Page 6 of 12
Object Oriented Programming

cout<<"\n\tEnter Employee Salary : ";

cin>>Salary;

void Employee :: PutData() //Statement 2 : Defining PutData()

cout<<"\n\nEmployee Id : "<<Id;

cout<<"\nEmployee Name : "<<Name;

cout<<"\nEmployee Age : "<<Age;

cout<<"\nEmployee Salary : "<<Salary;

void main()

Employee E; //Statement 3 : Creating Object

E.GetData(); //Statement 4 : Calling GetData()

E.PutData(); //Statement 5 : Calling PutData()

Output :

Page 7 of 12
Object Oriented Programming

Enter Employee Id : 1

Enter Employee Name : Kumar

Enter Employee Age : 29

Enter Employee Salary : 45000

Employee Id : 1

Employee Name : Kumar

Employee Age : 29

Employee Salary : 45000

In the above program, we have declared two member functions GetData() and PutData() within
class and statement 1 and 2 is defining these methods outside class. Statement 3 is creating an
object E of Employee class. Statement 4 and 5 is invoking/calling member function through the
object E of Employee class.

Syntax of defining member function inside class

return_type function_name(argument list)

----------

body of function

----------

Page 8 of 12
Object Oriented Programming

return_type Type of value function will return.

function_name Can be any valid C++ identifier.

argument list Represents the type and number of value function will take, values are sent by
the calling statement.

Definition of member function inside class is similar to defining normal function. There is no
need to tell compiler about the class the function belongs to because the definition of member
function is already in the class.

Example of defining member function inside class

#include<iostream.h>

#include<conio.h>

class Employee

int Id;

char Name[25];

int Age;

long Salary;

public:

void GetData() //Statement 1 : Defining GetData()

Page 9 of 12
Object Oriented Programming

cout<<"\n\tEnter Employee Id : ";

cin>>Id;

cout<<"\n\tEnter Employee Name : ";

cin>>Name;

cout<<"\n\tEnter Employee Age : ";

cin>>Age;

cout<<"\n\tEnter Employee Salary : ";

cin>>Salary;

void PutData() //Statement 2 : Defining PutData()

cout<<"\n\nEmployee Id : "<<Id;

cout<<"\nEmployee Name : "<<Name;

cout<<"\nEmployee Age : "<<Age;

cout<<"\nEmployee Salary : "<<Salary;

};

Page 10 of 12
Object Oriented Programming

void main()

Employee E; //Statement 3 : Creating Object

E.GetData(); //Statement 4 : Calling GetData()

E.PutData(); //Statement 5 : Calling PutData()

Output :

Enter Employee Id : 1

Enter Employee Name : Kumar

Enter Employee Age : 29

Enter Employee Salary : 45000

Employee Id : 1

Employee Name : Kumar

Employee Age : 29

Employee Salary : 45000

Page 11 of 12
Object Oriented Programming

In the above program, we have defined two member functions GetData() and PutData() within
class. Statement 3 is creating an object E of Employee class. Statement 4 and 5 is
invoking/calling member function through the object E of Employee class.

Page 12 of 12

You might also like