OOP Concepts
OOP Concepts
• Abstraction: Showing only the essential attributes of the class, while hiding the
technical details from the user.
• Inheritance: Using features from an existing class within a new class, without
modifying the existing class.
2
Encapsulation
• Encapsulation:
• Combining a number of items, such as variables and functions, into a single package,
such as an object of some class, is called encapsulation.
• Designing a class so that its attributes are isolated from the actions of external code
except through the formal interface.
• Formal interface: The components of a class that are externally accessible, which consist of
the class’s nonprivate attributes and responsibilities.
3
Encapsulation
4
Abstraction
• Showing only the essential attributes of the class while hiding the technical details from the
user.
• Abstract classes are used to represent general concepts (for example, Shape, Animal),
which can be used as base classes for concrete classes (for example, Circle, Dog).
• Abstraction is not the same as data hiding. Abstraction is showing only the relevant
information, while data hiding is restricting access to data members (variables, arrays,
structures, etc.) so that they cannot be accessed from outside the class.
• Abstraction allows changing internal implementations without affecting its user-level code.
• Since the programmer can use the same code repeatedly, it helps us perform similar tasks
for similar operations.
5
Encapsulation leads to Abstraction
Private and public members
• The function that we are making inside the class must use
only member variables; only then it is called encapsulation.
6
Abstraction
7
Inheritance
8
Inheritance
• Using features from an existing class in a new class, without modifying the existing class.
• The derived class inherits the features of the base class.
• In addition to the features of the base class, the derived class also has features of its own
9
Inheritance
10
Inheritance
11
Inheritance
12
Inheritance
13
Inheritance
14
Inheritance
15
Polymorphism
Polymorphism:
• Ability of the same entity (function or operator) to behave differently in different
scenarios
• It enables to write code that can work with objects of multiple classes in a more
general way.
• It makes code reusable and easier to work with complex systems.
• Two ways:
• function overloading and
• virtual functions
Function Overloading → Recall !!!
Conditions:
• Parameters should have different types (e.g., int vs double)
• Different number of parameters (e.g., two in one function, three in another)
• Different sequence of parameters (e.g., int and double vs double and int)
16
Polymorphism
17
Polymorphism
Virtual functions:
• It is often associated with inheritance and class hierarchies
• Dynamic polymorphism → the actual function called at runtime depends on the type
of the object
• It allows you to create a base class with virtual functions and then derive subclasses
that override these functions
• If a derived class is handled using a pointer or reference to the base class, a call to an
overridden virtual function would invoke the behavior defined in the derived class.
• Objects from different classes respond to the same message/command.
18
Polymorphism
19
Polymorphism
20
Polymorphism
21
Polymorphism
22
Practice codes
#include <iostream>
using namespace std;
//Base class
class Animal{ int main() {
string color; Dog d1;
protected: //Calling members of base class
string type; d1.eat();
public: d1.sleep();
void eat(){cout << "I can eat" << endl;} d1.setcolor("Black");
void sleep(){cout << "I can sleep" << endl;}
void setcolor(string clr){color=clr;} //Calling members of derived class
string getcolor(){ return color;} d1.bark();
}; d1.settype("Mammal");
//Derived class
class Dog:public Animal{ //Using base class method
public: 'getcolor’ as argument
void settype(string t){type = t;} //for derived class method
void displayInfo(string c){ 'displayInfo’
cout << "I am a " << type << endl; d1.displayInfo(d1.getcolor());
cout << "My color is " << c << endl; return 0;
} }
void bark(){
cout << "I can bark meow meow" << endl;
}
};
23
Practice codes
#include <iostream>
using namespace std;
class Animal{ int main(){
public: Animal prani;
virtual void eat(){ Horse ashva;
cout<<"Eats everything"<<endl; Tiger bagh;
}
}; AnimalFood(&prani);
AnimalFood(&ashva);
class Horse:public Animal{ AnimalFood(&bagh);
public:
void eat(){ // prani.eat();
cout<<"Eats grass"<<endl; // ashva.eat();
} // bagh.eat();
}; return 0;
}
class Tiger:public Animal{
public:
void eat(){
cout<<"Eats horse"<<endl;
}
};
25