INHERITANCE
It is a concept of reusability, we reuse the property of existing class by inheritating from it.
CODE:
#include<iostream>
using namespace std;
class employee{
public:
float salary;
int id;
employee(int inpId){
id=inpId;
salary=34.0;
}
employee(){}
};
class programmer:public employee{
public:
programmer(int inpId){
id=inpId;
}
int languagecode=9;
void getdata(){
cout<<id<<endl;
}
};
int main(){
employee harry(1),rohan(2);
cout<<harry.salary<<endl;
cout<<rohan.salary<<endl;
programmer skills(10);
cout<<skills.languagecode<<endl;
skills.getdata();
return 0;
}
SINGLE INHERITANCE
A derived class with only one base class.
SYNTAX:
class base-class{
};
class derived-class : access-mode base-class
CODE:
#include<iostream>
using namespace std;
class base{
int data1; //private by default is not inherited
public:
int data2;
void setdata();
int getdata1();
int getdata2();
};
void base::setdata(void){
data1=90;
data2=89;
};
int base::getdata1(){
return data1;
}
int base::getdata2(){
return data2;
}
class derived:public base{ //class is being derived publically
int data3;
public:
void process();
void display();
};
void derived::process(){
data3=data2*getdata1();
}
void derived::display(){
cout<<"value of data1 is"<<getdata1()<<endl;
cout<<"value of data2 is"<<data2<<endl;
cout<<"the value of data3 is"<<data3<<endl;
}
int main(){
derived der;
der.setdata();
der.process();
der.display();
return 0;
}
MULTI LEVEL INHERITANCE
Deriving a class from already derived class.
SYNTAX:
class base-class{
};
class derived-class1 : access-mode base-class{
};
class derived-class2 : access-mode derived-class1
CODE:
#include<iostream>
using namespace std;
class student{
protected:
int roll_number;
public:
void set_roll_number(int);
void get_roll_number(void);
};
void student::set_roll_number(int r){
roll_number=r;
}
void student::get_roll_number(){
cout<<"the roll number is"<<roll_number<<endl;
}
class exam:public student{
protected:
float maths;
float physics;
public:
void set_marks(float,float);
void get_marks(void);
};
void exam::set_marks(float m1,float m2){
maths=m1;
physics=m2;
}
void exam::get_marks(){
cout<<"the marks obtained in maths are:"<<maths<<endl;
cout<<"the marks obtained in physics are:"<<physics<<endl;
}
class result:public exam{
float percentage;
public:
void display(){
get_roll_number();
get_marks();
cout<<"your percentage is "<<(maths+physics)/2<<endl;
}
};
int main(){
result harry;
harry.set_roll_number(340);
harry.set_marks(90.0,90.0);
harry.display();
return 0;
}
MULTIPLE INHERITANCE
A derived class with more than one base class.
SYNTAX:
class base-class1{
};
class base-class2{
};
class derived-class: access-mode base-class1,access-mode base-class2
CODE:
#include<iostream>
using namespace std;
class base1{
protected:
int base1int;
public:
void set_base1int(int a){
base1int=a;
}
};
class base2{
protected:
int base2int;
public:
void set_base2int(int a){
base2int=a;
}
};
class derived:public base1,public base2{
public:
void show(){
cout<<"the value of base1 is"<<base1int<<endl;
cout<<"the value of base2 is"<<base2int<<endl;
cout<<"the sum of these value is"<<base1int+base2int<<endl;
}
};
int main(){
derived harry;
harry.set_base1int(90);
harry.set_base2int(34);
harry.show();
return 0;
}
HIERARCHICAL INHERITANCE
Several derived class from a single base class.
SYNTAX:
class A{
};
class B: public A{
};
class C : public A{
};
CODE:
#include <iostream>
using namespace std;
class A{
public:
void fun1(){
cout<<"function1"<<endl;
}
};
class B:public A{
public:
void fun2(){
cout<<"function2"<<endl;
}
};
class C: public A{
public:
void fun3(){
cout<<"function3"<<endl;
}
};
int main(){
A obj1;
obj1.fun1();
B obj2;
obj2.fun2();
obj2.fun1();
C obj3;
obj3.fun3();
obj3.fun1();
return 0;
}
HYBRID INHERITANCE
Combination of more than one type of inheritance.
It is also called virtual base class.
It is also famous for the name of "DIAMOND PROBLEM".
SYNTAX:
class A{
};
class B: virtual public A{
};
class C: virtual public A{
};
class D: public B, public C{
};
CODE:
#include<iostream>
using namespace std;
class student{
protected:
int roll_no;
public:
void set_number(int a){
roll_no=a;
}
void print_number(void){
cout<<"your roll no is"<<roll_no<<endl;
}
};
class test:virtual public student{
protected:
float maths,physics;
public:
void set_marks(float m1,float m2){
maths=m1;
physics=m2;
}
void print_marks(void){
cout<<"your result is here"<<endl;
cout<<"maths"<<maths<<endl;
cout<<"physics"<<physics<<endl;
}
};
class sports:virtual public student{
protected:
float score;
public:
void set_score(float sc){
score=sc;
}
void print_score(void){
cout<<"your pt score is"<<score<<endl;
}
};
class result:public test,public sports{
private:
float total;
public:
void display(void){
total=maths+physics+score;
print_number();
print_marks();
print_score();
cout<<"your total score is "<<total<<endl;
}
};
int main(){
result obj;
obj.set_marks(90.0,79.8);
obj.set_number(89);
obj.set_score(9);
obj.display();
return 0;
}