200-500 words per explanation and avoid plagiarism.
Explain the following in your own words without copying it from anywhere. Give
examples each in your explanation.
a. Class hierarchies
/*When several classes are derived from common base class it is called hierarchical inheritance.
In C++ hierarchical inheritance, the feature of the base class is inherited onto more than one
sub-class.
For example, a Construction is a common class from which House, Fabric, Plant etc can be derived.
Following block diagram highlights its concept.
Construction
/ | \
House Fabric Plant
As shown in above block diagram, in C++ hierarchical inheritance all the derived classes have
common base class. The base class includes all the features that are common to derived classes.
As in other inheritance, based on the visibility mode used or access specifier used while deriving,
the properties of the base class are derived. Access specifier can be private, protecte
*/
//Example
#include <iostream>
using namespace std;
class Construction//single base class
{
public:
double square;
double height;
void getdata()
{
cout << "Enter value of square and height:\n";
cin >> square>> height;
}
};
class House : public Construction //House is derived from class base
{
public:
void Volume()
{
cout << "Volume= " << square * height;
}
};
class Fabric : public Construction//Fabric is also derived from class base
{
int coeff=4;//Coefficient for account Volume of Fabric
public:
void Volume()
{
cout << "Volume= " << square * height*coeff;
}
};
class Plant: public Construction//Plant is also derived from class base
{
int coeff=8;//Coefficient for account Volume of Plant
public:
void Volume()
{
cout << "Volume= " << square * height*coeff;
}
};
int main()
{
House h; //object of derived class House
Fabric f ; //object of derived class Fabric
Plant p; //object of derived class Plant
cout<<"Calculate the volume of House:\n";
h.getdata();
h.Volume();
cout<<"\nCalculate the volume of Fabric:\n";
f.getdata();
f.Volume();
cout<<"\nCalculate the volume of Plant:\n";
p.getdata();
p.Volume();
}
Comments
Leave a comment