Polynomials in C++ - Encapsulation, Data Abstraction and Inheritance

Polynomials in C++ - Encapsulation, Data Abstraction and Inheritance
Polynomials in C++ - Encapsulation, Data Abstraction and Inheritance


Polynomials in C++ - Encapsulation, Data Abstraction and Inheritance

Encapsulation: The process of combining many elements into a single entity is called Encapsulation. In the field of the programming language, the process of combining data member and member function into a single entity-like class is called Data encapsulation.


It is an important feature of object-oriented programming. It is used to prevent the direct accessibility of data member and member function and this is done by using access specifier public, private, and protected

Access Specifier: It is a keyword that is used to provide accessibility of data member(variable) and member function(function) of a class. It is also called an access modifier.

Types of Access Specifier


There are three types of access specifiers.

1. Public

2. Private

3. Protected

👉Super Keyword in Java

public: It allows the accessibility of data member and member function to the other classes. The public element of a class can be accessed anywhere in the program.

Private: It is used to hide data member and member functions from the other classes. The private element of a class can be accessed only inside its own class.

Protected: private element of a class can not be accessed outside of that class.
protected. It is approximately the same as private but it allows the accessibility of data member and member function to the child class. protected is used in the case of inheritance.

How To Use Encapsulation in C++?

#include<iostream>

using namespace std;

class Circle

{

private:

float area;

float radius;

public:

void getRadius()

{

cout<<"Enter a Circle Radius:";

cin>>radius;

}

void findArea()

{

area=3.14*radius*radius;

cout<<"Area of Circle:"<<area;

}

};

int main()

{

Circle c;

c.getRadius();

c.findArea();

}

*****OUTPUT*****

Enter a Circle Radius:5

Area of Circle:78.5


Data Abstraction: Abstraction means Data hiding, in another word we can say that in this type of programming essential data is shown to the user or outside class and unessential data is hidden.
Members defined with a public access specifier are accessible throughout the program. Members defined with a private access specifier are not accessible throughout the program means a private element of a class can be accessed only inside its own class.

Example of Real Life Data Abstraction

Let's take a real-life example to assume that you are going to buy a car in a showroom then you can know about the company name, model name, color, cost, and oil type but you don't know about piston and its functionality, the person who made that model of car.


How To Use Data Abstraction in C++?

#include<iostream>

using namespace std;

class CAR

{

 public:

void company()

{

cout<<"Car Company Name Renault\n";

}

public:

void model()

{

cout<<"Car Model Name Duster\n";

}

public:

void color()

{

 cout<<"Car Color Red/Brown/Silver\n";

}

public:

void cost()

{

cout<<"Car Price Rs. 600,000 To 900,000\n";

}

public:

void oil()

{

cout<<"Car Fuel Type Petrol\n";

}

private:

void piston()

{

cout<<"Car number of Piston 4\n";

}

private:

void manWhoMade()

{

cout<<"Car Markus Librette\n";

}

};

int main()

{

CAR c;

c.company();

c.model();

c.color();

c.cost();

c.oil();

}

 

*****OUTPUT*****

Car Company Name Renault

Car Model Name Duster

Car Color Red/Brown/Silver

Car Price Rs. 600,000 To 900,000

Car Fuel Type Petrol


Advantage of Data Abstraction

1. It can provide security of data using Abstraction.
2. Data Abstraction avoids code duplication and increases code reusability.

3. We do not have to write the low-level code because the private element of a class can not be accessed outside that class.

Inheritance
: The process of getting the property of one class into another class is called Inheritance. In another word,,,, we can say that the process of deriving a new class from an old class is called inheritance in which the new class is called derived or child or subclass and the old class are called Base or A parent or Superclass.

When a class inherits the property of a class means it can access all the data members and members of the function of that class except for the private element.

Inheritance oops concept are two types of classes are used

1.Parent/Super/Base class

2.Child/Sub/Derived class

Parent/Super/Base class: The class which is inherited by another class is called Parent or Super or Base class.

Child/Sub/Derived class: The class which inherits the property of another class is called Child or Sub or Derived class.

How to inherit one class into another class in C++?

#include<iostream>

using namespace std;

class Addition

{

public:

void add()

{

 int x=100;

int y=100;

int z=x+y;

cout<<"Addition:"<<z<<endl;

}

};

class Subtraction: public Addition

{

public:

void sub()

{

 int x=100;

int y=50;

int z=x-y;

cout<<"Subtraction:"<<z<<endl;

}

};

int main()

{

Subtraction s;

s.add();

s.sub(); }

*****OUTPUT*****

Addition:200

Subtraction:50

Here class Addition is a base class and Subtraction is a derived class because the Addition property is inherited into Subtraction therefore we can call all the functions using the object of Subtraction.

👉Java Interface|Abstract Method


There are five types of inheritance in C++.

1. Single Inheritance

2. Multiple Inheritance

3. Multilevel Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance

Single Inheritance: In this type of inheritance only two classes are used in which one is inherited by another.

How To Use Single Inheritance in C++?

#include<iostream>

using namespace std;

class Addition

{

public:

void add()

{

 int x=100;

int y=100;

int z=x+y;

cout<<"Addition:"<<z<<endl;

}

};

class Subtraction: public Addition

{

public:

void sub()

{

 int x=100;

int y=50;

int z=x-y;

cout<<"Subtraction:"<<z<<endl;

}

};

int main()

{

Subtraction s;

s.add();

s.sub(); }

*****OUTPUT*****

Addition:200

Subtraction:50

In the above example, you can see that there are only two classes are used in which Addition is inherited by Subtraction, therefore, using object of subtraction we can call function add() and sub().

Multiple Inheritance: When two or more than two classes are inherited by a single class simultaneously called multiple inheritances. In another word, we can say that in this type of inheritance The base class may be two or more than two but the derived class should be one. In this type of inheritance at least three classes are compulsory.


How To Use Multiple Inheritance in C++? 

#include<iostream>

using namespace std;

class Addition

{

public:

void add()

{

int x=100;

int y=100;

int z=x+y;

cout<<"Addition:"<<z<<endl;

}

};

class Subtraction

{

public:

void sub()

{

int x=100;

int y=50;

int z=x-y;

cout<<"Subtraction:"<<z<<endl;

}

};

class Multiplication: public Addition, public Subtraction

{

public:

void multiply()

{

int x=10;

int y=10;

int z=x*y;

cout<<"Multiplication:"<<z<<endl;

}

};

 

int main()

{

Multiplication m;

m.add();

m.sub();

m.multiply();

}

*****OUTPUT*****

Addition:200

Subtraction:50

Multiplication:100

In the above example, you can see that there are three classes(Addition, Subtraction,n, and Multiplication) are used in which Addition and Subtraction are inherited by Multiplication, therefore, using object of Multiplication we can call function to add(), sub() and multiply().

Multilevel Inheritance: When the first class is inherited by the second class, the second class is inherited by the third class, and so on is called multilevel inheritance. In this type of inheritance, each derived class is the base class for the next class.
In this type of inheritance at least three classes are compulsory.

👉Java Class or Object


How To Use Multilevel Inheritance in C++?

#include<iostream>

using namespace std;

class Addition

{

public:

void add()

{

int x=100;

int y=100;

int z=x+y;

cout<<"Addition:"<<z<<endl;

}

};

class Subtraction: public Addition

{

public:

void sub()

{

int x=100;

int y=50;

int z=x-y;

cout<<"Subtraction:"<<z<<endl;

}

};

class Multiplication: public Subtraction

{

public:

void multiply()

{

int x=10;

int y=10;

int z=x*y;

cout<<"Multiplication:"<<z<<endl;

}

};

 

int main()

{

Multiplication m;

m.add();

m.sub();

m.multiply();

}

*****OUTPUT*****

Addition:200

Subtraction:50

Multiplication:100


Hierarchical Inheritance: When a single class is inherited by two or more than two classes simultaneously called hierarchical inheritance. In other words, we can say that in this type of inheritance derived class maybe two or more than two but the Base class should be one. In this type of inheritance at least three classes are compulsory.

👉Python Class Or Object 


How To Use Hierarchical Inheritance in C++?

#include<iostream>

using namespace std;

class Addition

{

public:

void add()

{

int x=100;

int y=100;

int z=x+y;

cout<<"Addition:"<<z<<endl;

}

};

class Subtraction: public Addition

{

public:

void sub()

{

int x=100;

int y=50;

int z=x-y;

cout<<"Subtraction:"<<z<<endl;

}

};

class Multiplication: public Addition

{

public:

void multiply()

{

int x=10;

int y=10;

int z=x*y;

cout<<"Multiplication:"<<z<<endl;

}

};

 

int main()

{

Multiplication m;

m.add();

m.multiply();

}

*****OUTPUT*****

Addition:200

Multiplication:100

In the above example, you can see that there are three classes(Addition, Subtraction, and Multiplication) are used in which Addition is inherited by Subtraction and Multiplication therefore using the object of Multiplication we can call the function only add() and multiply() because there is no relation between Subtraction and Multiplication, therefore, function sub() can not be called by an object of Multiplication. Similarly, by using object of class Subtraction we can call only function to add() and sub().

Hybrid Inheritance: The combination of two or more than two inheritance is called Hybrid inheritance. It can be a combination of any two or more than two inheritance single, multiple, multilevel, hierarchical).In this type of inheritance at least three classes are compulsory.

How To Use Hybrid Inheritance in C++?

#include<iostream>

using namespace std;

class Addition

{

public:

void add()

{

int x=100;

int y=100;

int z=x+y;

cout<<"Addition:"<<z<<endl;

}

};

class Subtraction

{

public:

void sub()

{

int x=100;

int y=50;

int z=x-y;

cout<<"Subtraction:"<<z<<endl;

}

};

class Multiplication: public Addition, public Subtraction

{

public:

void multiply()

{

int x=10;

int y=10;

int z=x*y;

cout<<"Multiplication:"<<z<<endl;

}

};

class Division: public Multiplication

{

public:

void divi()

{

int x=50;

int y=5;

int z=x/y;

cout<<"Division:"<<z<<endl;

}

};

int main()

{

Division d;

d.add();

d.sub();

d.multiply();

d.divi();

}

*****OUTPUT*****

Addition:200

Subtraction:50

Multiplication:100

Division:10

In the above example, you can see that there are four classes(Addition, Subtraction, Multiplication, and Division) in which Addition and Subtraction are inherited by Multiplication class so in class Addition, Subtraction and Multiplication there is Multiple inheritances but class Multiplication is inherited by Division so in class Multiplication and Division there is Single inheritance. Therefore the above program is a combination of Multiple and Single inheritances so it is called Hybrid Inheritance.

Advantage of Inheritance in C++?

Code Reusability: It means the function of the inside base class is shared by all the derived classes.

Time-Saving: Because there is no need to define an existing property(same code) of a class in another class.

Polymorphism: It means one name in many forms so we can say that in this type of programming same function is used to perform a different kind of operations. It is an important part of object-oriented programming language.

which as you can see the class name is poly and there are four functions with the same name a() with different parameters so the execution of the function is based on the value passing at the time of calling.

How To Use Polymorphism in C++?

#include<iostream>

using namespace std;

class polymor

{

public:

void a()

{

cout<<"No Parameter"<<endl;

}

void a(int x)

{

cout<<"Integer Parameter"<<endl;

}

void a(doubled)

{

cout<<"Double Parameter"<<endl;

}

void a(char ch)

{

cout<<"Character Parameter"<<endl;

}

};

int main()

{

polymor p;

p.a();

p.a(100);

p.a(2.900);

p.a('i');        

}

*****OUTPUT*****

No Parameter

Integer Parameter

Double Parameter

Character Parameter


Two Types of Polymorphism in C++

Compile Time Polymorphism: A compile-time Function overloading and Operator overloading are examples of Compile time Polymorphism.

Runtime Polymorphism: A Runtime Polymorphism Function overriding is an example of Runtime Polymorphism.

Function overloading: The function with the same name and the different parameters is called function overloading.


How To Use Overloading in C++?

#include<iostream>

using namespace std;

class overloading

{

public:

void area(int h, int w)

{

int a=h*w;

cout<<"Area Of Rectangle:"<<a<<endl;

}

void area(int side)

{

int a=side*side;

cout<<"Area Of Square:"<<a<<endl;

         

}

};

int main()

{

overloading o;

o.area(12,12);

o.area(10);

}

*****OUTPUT*****

Area Of Rectangle:144

Area Of Square:100

Here overloading is a class name that contains two functions with the same name area() with different parameters, first with two-parameter h and w and second with a single parameter side. Therefore when two integer parameters are passed at the time of calling Rectangle area will be calculated, when a single integer parameter is passed Square area will be calculated.

Operator overloading: It is a type of Polymorphism in which an operator is overloaded to give user-defined meaning to it. Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined, By using an operator overloading we can change the meaning of the operator. The overloaded operator is used to perform operations on the user-defined data types.


How To Use Operator Overloading in C++?

#include<iostream>

using namespace std;

class data

{

public:

int h;

int w;

int area;

void operator +()

{

int h=10;

int w=20;

}

void operator ++()

{

area=h*w;

cout<<"Area of Rectangle:"<<area<<endl;   

}

};

int main()

{

data op;

+op;

++op;

}

*****OUTPUT*****

Area  of Rectangle:200                 

In the above example, there are two functions, one function with Addition operator(+) and other with increment operator(++).+ operator is used to add number and ++ operator is used to increment the number by one but here you can see that we are using them with function so by using operator overloading we can change the behavior of the operator

Function Overriding: A function with the same name and same parameter is called function overriding. It is not possible to make two functions with the same name and the same parameter in a single class therefore to implement function overriding derived class is used.

How To use Function Overriding in C++?

#include<iostream>

using namespace std;

class data1

{

public:

void area(int h, int w)

{

int a=h*w;

cout<<"Area of Rectangle1:"<<a<<endl;      

}

};

class data2:public data1

{

public:

void area(int h, int w)

{

int a=h*w;

cout<<"Area of Rectangle2:"<<a<<endl;

}

};

int main()

{

data2 d;

d.area(10,10);

}

*****OUTPUT*****

Area of Rectable2:100              


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.