Write a C++ Program to demonstrate an Example of Hybrid Inheritance. Here’s a Simple C++ Program to demonstrate an Example of Hybrid Inheritance in C++ Programming Language.
Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
Types of Inheritance : :
There are different types of inheritance : :
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid (Virtual) Inheritance
Below is the source code for C++ Program to demonstrate an Example of Hybrid Inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to demonstrate an Example of Hybrid Inheritance */
#include<iostream>
using namespace std;
class stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
cout<<"Roll no :: "<<rno<<"\n";
}
};
class test:public stu
{
protected:
float part1,part2;
public:
void get_mark(float x,float y)
{
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks obtained :\n"<<"part1 = "<<part1<<"\n"<<"part2 = "<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore(void)
{
cout<<"Sports : "<<score<<"\n";
}
};
class result: public test, public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
putscore();
cout<<"Total Score = "<<total<<"\n";
}
int main()
{
result stu;
stu.get_no(123);
stu.get_mark(27.5,33.0);
stu.getscore(6.0);
stu.display();
return 0;
}
OUTPUT : :
/* C++ Program to demonstrate an Example of Hybrid Inheritance */
Roll no :: 123
Marks obtained :
part1 = 27.5
part2 = 33
Sports : 6
Total Score = 66.5
Process returned 0
Above is the source code and output for C++ Program to demonstrate an Example of Hybrid Inheritance which is successfully compiled and run on Windows System to produce desired output.
Types of Inheritance : :
There are different types of inheritance : :
Below is the source code for C++ Program to demonstrate an Example of Hybrid Inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
Above is the source code and output for C++ Program to demonstrate an Example of Hybrid Inheritance which is successfully compiled and run on Windows System to produce desired output.
need an explanation for this answer? contact us directly to get an explanation for this answer