Explain the type of inheritance with code.
//1 - C++ Program to demonstrate single inheritance
#include <iostream>
using namespace std;
class ClassOne
{
public:
void classOneGreet()
{
cout<<"Hello from class One"<<endl;
}
};
class ClassTwo : public ClassOne //One is the base class of Class two and Class Two inherits only one class.
{
public:
void classTwoGreet()
{
cout<<"Hello from class Two"<<endl;
}
};
//Driver code
int main()
{
ClassTwo classTwo;
classTwo.classOneGreet();
classTwo.classTwoGreet();
return 0;
}
/****************End of single inheritance program****************************/
//2 - C++ Program to demonstrate multiple inheritance
#include <iostream>
using namespace std;
//Multiple inheritance
class ClassOne
{
public:
void classOneGreet()
{
cout<<"Hello from class One"<<endl;
}
};
class ClassTwo
{
public:
void classTwoGreet()
{
cout<<"Hello from class Two"<<endl;
}
};
//ClassThree inherits more than one class
class ClassThree : public ClassOne, public ClassTwo
{
public:
void classThreeGreet()
{
cout<<"Hello from class Three"<<endl;
}
};
//Driver code
int main()
{
ClassThree classThree ;
classThree.classOneGreet();
classThree.classTwoGreet();
classThree.classThreeGreet();
return 0;
}
/************************End of mulitple inheritance program****************************/
//3 - C++ Program to demonstrate hierachical inheritance. This process derives more than one class from a base class.
#include <iostream>
using namespace std;
//hierachical inheritance
class ClassOne
{
public:
void classOneGreet()
{
cout<<"Hello from class One"<<endl;
}
};
//Inherits ClassOne
class ClassTwo : public ClassOne
{
public:
void classTwoGreet()
{
cout<<"Hello from class Two"<<endl;
}
};
//ClassThree inherits ClassOne
class ClassThree : public ClassOne
{
public:
void classThreeGreet()
{
cout<<"Hello from class Three"<<endl;
}
};
//Driver code
int main()
{
ClassTwo classTwo;
classTwo.classOneGreet();
classTwo.classTwoGreet();
ClassThree classThree ;
classThree.classOneGreet();
classThree.classThreeGreet();
return 0;
}
/*************************End of hierachical inheritance program*************************/
//4 - C++ Program to demonstrate Mulitilevel inheritance.
#include <iostream>
using namespace std;
//Mulitilevel inheritance
class ClassOne
{
public:
void classOneGreet()
{
cout<<"Hello from class One"<<endl;
}
};
//Inherits ClassOne
class ClassTwo : public ClassOne
{
public:
void classTwoGreet()
{
cout<<"Hello from class Two"<<endl;
}
};
//ClassThree inherits ClassTwo
class ClassThree : public ClassTwo
{
public:
void classThreeGreet()
{
cout<<"Hello from class Three"<<endl;
}
};
//Driver code
int main()
{
ClassThree classThree ;
classThree.classOneGreet();
classThree.classTwoGreet();
classThree.classThreeGreet();
return 0;
}
/**************End of mulitlevel inheritance********/
using namespace std;
//5 - Hybrid inheritance
class ClassOne
{
public:
void classOneGreet()
{
cout<<"Hello from class One"<<endl;
}
};
//Inherits ClassOne - Single level
class ClassTwo : public ClassOne
{
public:
void classTwoGreet()
{
cout<<"Hello from class Two"<<endl;
}
};
class ClassThree
{
public:
void classThreeGreet()
{
cout<<"Hello from class Three"<<endl;
}
};
//Multiple inheritance
class ClassFour: public ClassTwo, public ClassThree
{
void classFourGreet()
{
cout<<"Hello from class Four"<<endl;
}
};
//Driver code
int main()
{
//Single inheritance
ClassTwo classTwo;
classTwo.classOneGreet();
classTwo.classTwoGreet();
//Mulitiple inheritance
ClassFour classFour ;
classFour.classTwoGreet();
classFour.classThreeGreet();
return 0;
}
/******************End of hybrid inheritance
Comments
Leave a comment