Advertisement
//C++ Hierarchical Inheritance
class Sports
{
//members of a top-level-base-class i.e. Sports
}
class Football : visibility-mode Sports
{
//members of a derived-class at a lower level i.e. Football
}
class Hockey : visibility-mode Sports
{
//members of another derived-class at a lower level i.e. Hockey
}
class Cricket : visibility-mode Sports
{
//members of another derived-class at a lower level i.e. Cricket
}
class JrFootballTeam : visibility-mode Football
{
//members of another derived-class at a lower level than class Football
}
class SrFootballTeam : visibility-mode Football
{
//members of another derived-class at a lower level than class Football
}
Advertisement
//C++ Example of Hierarchical Inheritance
#include<iostream>
#include<string>
using namespace std;
//First Base class
class Sports
{
private:
int total_players;
string coach_name;
protected:
string captain_name;
public:
//Getters
int get_total_players();
string get_coach_name();
string get_captain_name();
//Setters
void set_total_players(int);
void set_coach_name(string);
void set_captain_name(string);
};
//Definition of get_total_players() function of Sports class
int Sports :: get_total_players()
{
return total_players;
}
//Definition of get_coach_name() function of Sports class
string Sports :: get_coach_name()
{
return coach_name;
}
//Definition of get_captain_name() function of Sports class
string Sports :: get_captain_name()
{
return captain_name;
}
//Definition of set_coach_name() function of Sports class
void Sports :: set_coach_name(string cn)
{
coach_name = cn;
}
//Definition of set_captain_name() function of Sports class
void Sports :: set_captain_name(string cn)
{
captain_name = cn;
}
//Definition of set_total_players() function of Sports class
void Sports :: set_total_players(int num)
{
total_players = num;
}
//A derived class Hockey, publicly inheriting the class Sports
class Hockey : public Sports
{
};
//A derived class Cricket, publicly inheriting the class Sports
class Cricket : public Sports
{
};
//A derived class Football, publicly inheriting from class Sports
class Football : public Sports
{
};
//A derived class JuniorFootballTeam, publicly inheriting from class Football
class JrFootballTeam : public Football
{
};
//A derived class SeniorFootballTeam, publicly inheriting from class Football
class SrFootballTeam : public Sports
{
};
int main()
{
JrFootballTeam ob; //Making an object of derived class, Football
ob.set_captain_name("Superman");//Setting an inherited property captain_name
ob.set_coach_name("Motivator"); //Setting an inherited property coach_name
ob.set_total_players(11); //Setting an inherited property total_players
//ob.captain_name="Superman; is not possible because captain_name is private and cannot be inherited.
cout<< "The name of captain is : " << ob.get_captain_name() << "\n";
cout<< "The name of coach is : " << ob.get_coach_name() << "\n";
cout<< "The total number of players are : " << ob.get_total_players() << "\n";
return 0;
}
The name of captain is : Superman
The name of coach is : Motivator
The total number of players are : 11
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement