Object Oriented Programming provide a feature of inheritance.Inheritance is a process to create new class which acquires the properties of existing class with some changes.
class new_class_name: public old_class_name { statement 1 statement 2 statement 3 }
#include class School { public:int id=2; }; class Student: public School { public:int roll_no=1; }; void main() { Student s; cout << "Id: " << s.id << endl; cout << "Roll No: "<< s.roll_no << endl; }
output:
Id: 2
Roll No: 1
When one class inherits another class which is again inherited by another class,is known as multilevel inheritance in C++.
#include class A { public:void aa() { cout << "Hello..."<< endl; } }; class B: public A { public:void bb() { cout <<" How Are You..."<< endl; } }; class C: public B { public:void cc() { cout <<" Where Are You From..."; } }; void main() { C c; c.aa(); c.bb(); c.cc(); }
output:
Hello...
How Are You...
Where Are You From...