2. The method by which we can derive a new
class from an old called Inheritance or
derivation.
Old class- Base class
New class- Derived class/ subclass
Parent/Old/Base
Child/new/derived
4. Reusability- use the function/data in derived
class
Reliability- Derived class is used or adapt the
feature from the base class
Save time and memory
Enhancement
Classification
Industry used
5. Syntax:
class base
{
class body;
members of the class base;
};
class derived
:visibility_mode base
{
class body;
members of the class
derived;
};
example:
class shapes
{
char color;
void show();
};
class square :public shapes
{
int side;
void area();
};
6. If we declare members in private visibility
mode, then that used is only within a self
class and not other class.
If we declare members in protected
visibility mode, then that used is only within a
self class and derived class not other class.
If we declare members in public visibility
mode, then that used for all function, which
declared in the program.
7. Parent
Child
• Only one base/Parent class and only
one derived/child class
• If a single class is derived from one
base class then it is called single
inheritance.
• Child has own functionality, which is
differ than parent class.
8. #include <iostream>
using namespace std;
class parent //single base class
{
public: int x;
void getdata()
{
cout << "Enter the value of x = ";
cin >> x;
}
};
class child : public parent //single
derived class
{
private: int y;
public: void readdata()
{
cout << "Enter the value of y = ";
cin >> y;
}
void product()
{
cout << "Product = " << x * y;
} };
int main()
{
child a; //object of derived class
a.getdata();
a.readdata();
a.product();
return 0; }
9. If a class(ABC) is derived from another
derived class(XYZ) then it is called multilevel
inheritance.
So in C++ multilevel inheritance, a class has
more than one parent class.
XYZ class is also known as
intermediate base class.
Grand
Parent
XYZ
ABC
10. #include <iostream>
using namespace std;
class grandparent //single base class
{
public: int x;
void getdata()
{
cout << "enter the value of x = ";
cin >> x;
}
};
class parent : public grandparent // derived class
{
public: int y;
void readdata()
{
cout << "enter the value of y = ";
cin >> y;
}
};
class child : public parent
// derived from class parent
{
private: int z;
public: void intdata()
{
cout << "nenter value of z= ";
cin >> z; }
void product()
{
cout << "nproduct= " << x * y * z;
} };
int main()
{
child a; //object of clild class
a.getdata();
a.readdata();
a.intdata();
a.product();
return 0; }