Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP)
(OOP)
Course Contents
• Object-Orientation
• Objects and Classes
• Overloading
• Inheritance
• Polymorphism
• Generic Programming
• Exception Handling
• Introduction to Design Patterns
Object-Orientation (OO)
What is Object-Orientation?
• Highway maps
• Architectural models
• Mechanical models
Example – OO Model
…Example – OO Model
lives-in
Ali House
• Objects
– Ali drives
– House
– Car Car Tree
– Tree
• Interactions
– Ali lives in the house
– Ali drives the car
Object-Orientation - Advantages
• People think in terms of objects
• State (attributes)
• Well-defined behaviour (operations)
• Unique identity
Example – Ali is a Tangible Object
• State (attributes)
– Name
– Age
• behaviour (operations)
– Walks
– Eats
• Identity
– His name
Example – Car is a Tangible Object
• State (attributes)
- Color
- Model
• behaviour (operations)
- Accelerate - Start Car
- Change Gear
• Identity
- Its registration number
Encapsulation
• We don’t know
– How the data is stored
– How Ali translates this information
Example – Encapsulation
• A Phone stores phone numbers in digital
format and knows how to convert it into
human-readable characters
• We don’t know
– How the data is stored
– How it is converted to human-readable
characters
Encapsulation – Advantages
• Low complexity
• Better understanding
Object has an Interface
• Principle of abstraction:
• Attributes
- Name - Employee
ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Example – Abstraction
Ali is a PhD student and teaches BS
students
• behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Example – Abstraction
Student’s Perspective
• Attributes
- Name - Employee
ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Example – Abstraction
Student’s Perspective
• behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Example – Abstraction
Teacher’s Perspective
• Attributes
- Name - Employee
ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Example – Abstraction
Teacher’s Perspective
• behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Example – Abstraction
A cat can be viewed with different
perspectives
Engineer’s View
Driver’s View
Abstraction – Advantages
(Class Name)
(Class Name)
(attributes)
Suppressed
(operations)
Form
Normal Form
Example – Graphical Representation
of Classes
Circle
center Circle
radius
draw Suppressed
computeArea Form
Normal Form
Example – Graphical Representation
of Classes
Person
name Person
age
gender Suppressed
eat Form
walk
Normal Form
Structures in C / C++
• Structure in C :
– Supports only data abstraction
– Only function pointer can be data member
• Structure in C++
– Supports data abstraction and procedure
abstraction
– Therefore functions can also be members of
structure
Structures in C / C++
Structure in ‘C’ Structure in ‘C++’
struct directory struct directory
{ {
char Name[30]; char Name[30];
long PhoneNo; long PhoneNo;
void Display(void)
{
cout<<PhoneNo;
}
}; };
Class
• struct keyword can be replaced by class keyword
class Complex
{
……
public:
Complex( ) default constructor
{ written as a inline function
m_real = 0 ;
m_imag = 0 ;
}
};
Class : Constructor
Class Complex
{
…
public:
void Display (void); //member function declaration
int GetReal(void); //accessor member function
void SetReal(int); // mutator member function
}
Class Complex : Few member functions
Member
functions
Class Complex
P Complex
Object
stack heap
Class Complex : const objects
{ {
} int x = c5.GetReal();
}
Conventional way of writing C++ Program
class String
{
private: access specifier
int m_len; data member
char* m_pbuff; pointer as data member
public: access specifier
void Display (void); member function
….
};
Class String : Default Constructor
Class String
{
…...
public:
~String( )
{
if(m_pbuff)
delete [ ] m_pbuff;
}
};
Object Creation/Destruction
Class Test
{……
public:
Test( )
{ cout<<“Constructor is invoked”<<endl; }
~Test( )
{ cout << “Destructor is invoked<<endl; “ }
};
Scope of an Object
Test t1;
void main (void)
{ cout<<“main begins”;
Test t2;
{
cout<<“Block begins”<<endl;
Test t3;
cout<<“Block Ends”<<endl;
}
cout<<“main ends”<<endl;
}
Class String : Copy Constructor
1001 3003
1001 3003
S E E D \0 S E E D \0
Copy constructor creates a new space in the heap for the
string to be copied.
Class String : Copy Constructor
C1 C2 C3
count
Static members
Constant members
class myclass void main()
{ {
int x; myclass obj1,obj3;
public: const myclass obj2=obj1;
void func(int m) obj2=obj3;
{ obj2.func();
x=m;
} }
};
Constant specifiers
class myclass
{
int x;
int y;
public:
void func(int m)const
{
x=m;
}
};
void main()
{
myclass obj;
obj.func(10);
}
Constant specifier
class myclass
{
int x;
mutable int y;
public:
void func(int m)const
{
y=m;
}
};
void main()
{
myclass obj;
obj.func(10);
}
Constant specifiers
class myclass
void func()
{ {
int x; myclass obj;
myclass(){ obj.display();
x=10;
}
}
public:
void display()
{
cout<<x;
}
friend void myfunc();
};
Friend functions
class myclass
{
int x;
public: void main()
myclass(){ {
x=10; func();
} myclass obj;
private: obj.func();
friend void myfunc(); }
};
void func()
{
….
}
Friend functions
class myclass
{
int x;
public:
myclass(){
x=10;
}
void myfunc(){ ……..}
};
class myclass1
{
friend void myclass::func();
};
Friend classes