INDIAN INSTITUTE OF TECHNOLOGY ROORKEE
CEC-101: Computer Programming
Encapsulation, Abstraction, Inheritance and Polymorphism
Prof. Amit Agarwal and Prof. Anjaneya Dixit
Basic principles of Object-oriented Programming
• Encapsulation: Bundling of related data and functions together within a single
entity.
• 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.
• Polymorphism: Ability of the same entity (function or operator) to behave
differently in different scenarios.
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
all public members
4
Abstraction
• Showing only the essential attributes of the class while hiding the technical details from the
user.
• Abstraction barrier: An invisible wall around an object that encapsulates implementation
details. The wall can be breached only through the public interface.
• 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
• We can not access any function from the class directly. We
need an object to access that function that is using the
member variables of that class.
• The function that we are making inside the class must use
only member variables; only then it is called encapsulation.
• Encapsulation improves readability, maintainability, and
security by grouping data and methods together.
• It helps to control the modification of our data members.
1) Make all the data members private.
2) Create public setter and getter functions for each
data member in such a way that the set function
set the value of the data member and the get
function gets the value of the data member.
6
Abstraction
7
Inheritance
• Reusability is an integral aspect for C++ and other OOP languages
• C++ allows to reuse classes by adding additional features to it
• Saving in time (debugging, compilation) and programming effort
• Concept of reusability is termed as Inheritance
• Existing class can be reused by ‘inheriting’ its members and methods
• Existing class is termed as base class
• New class formed by inheriting is termed as derived class
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 !!!
It is compile-time (static) polymorphism because the compiler determines
which function to call based on the number and types of arguments passed
during a function call.
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;
}
};
void AnimalFood(Animal *jaanwar){
(*jaanwar).eat();
}
24
Practice codes
// Online C++ compiler to run C++ program
online int main() {
#include <iostream> base *base_ptr;
using namespace std; derived *derived_ptr;
class base{ base obj_base;
public: derived obj_derived;
int varbase; base_ptr = &obj_derived; //Base
virtual void display(){ pointer towards derived object
cout << "Displaying base var from derived_ptr= &obj_derived; //
base " << varbase<<endl; Derived pointer towards derived object
} base_ptr->varbase = 34;
}; base_ptr->varderived=134;
class derived: public base{ base_ptr->display();
public:
int varderived; derived_ptr->varbase = 44;
void display(){ derived_ptr->varderived=134;
cout << "Displaying derived var from base_ptr->display();
derived " << varderived<<endl; derived_ptr->display();
} return 0;
}; }
25