Lec-10 Polymorphism
Lec-10 Polymorphism
Programming
Mohona Ghosh
Encapsulation
• Encapsulation in C++ is defined as the wrapping up of data and
functions that manipulate them together in a single unit.
2.The function which we are making inside the class must use only member
variables, only then it is called encapsulation.
3.If we don’t make a function inside the class which is using the member
variable of the class then we don’t call it encapsulation.
• Another type of abstraction in C++ can be header files. For example, consider
the pow() method present in math.h header file. Whenever we need to
calculate the power of a number, we simply call the function pow() present in
the math.h header file and pass the numbers as arguments without knowing
the underlying algorithm according to which the function is actually
calculating the power of numbers.
#include<iostream>
using namespace std; void color()
{
class Vehicle cout<<"Red/GREEN/Silver\n";
{ }
private: void cost()
void piston() {
{ cout<<"Rs. 60000 to 900000\n";
cout<<"4 piston\n"; }
} void oil()
{
void manWhoMade() cout<<"PETROL\n";
{ }
cout<<"Markus Librette\n"; };
} int main()
public: {
void company()
{ Vehicle obj;
cout<<"GFG\n"; obj.company();
} obj.model();
void model() obj.color();
{ obj.cost();
cout<<"SIMPLE\n"; obj.oil();
} }
Advantages of Data Abstraction
• Can change the internal implementation of the class independently
without affecting the user.
int main() {
Calculator calc;
int result1 = calc.add(5, 3); // Calls add(int, int)
double result2 = calc.add(2.5, 1.5); // Calls add(double, double)
return 0;
}
Example demonstrating function overloading using a Calculator class that contains overloaded add
function
#include <iostream>
using namespace std;
class Display {
public: int main() {
// Function to display an integer Display obj;
void show(int a) {
cout << "Integer: " << a << endl; obj.show(25);
} obj.show(3.14);
obj.show("Hello, World!");
// Function to display a double
void show(double a) { return 0;
cout << "Double: " << a << endl; }
}