Hierarchical model follows a top down approach by breaking up a complex class into
simpler constituent classes. In other words, in the hierarchical model, a complex class is
conceptualized as being made up of simpler classes, see figure.
In C++ hierarchical programs can be easily converted into class hierarchies. The
superclass (base class) includes the features that are common to all the subclasses
(derived classes). A subclass is created by inheriting the properties of the base class and
adding some of its own features. The sub can serve as a superclass for the lower level
classes again and so on.
#include <iostream.h>
class Vehicle
{
protected:
char name[25];
int WheelsCount;
public:
void GetData(void)
{
cout<<”Name of the Vehicle”;
cin>>name;
cout<<”Wheels”;
cin>>WheelsCount;
}
void DisplayData(void)
{
cout<<”Name of the Vehicle “<<name<<endl;
cout<<”Wheels “<<WheelsCount<<endl;
}
};
class LightMotor : public Vehicle
{
protected:
int SpeedLimit;
public:
void GetData(void)
{
Vehicle::GetData();
Cout<<”Speed Limit”;
Cin>>SpeedLimit;
}
void DisplayData(void)
{
Vehicle::DisplayData();
Cout<<”Speed Limit “<<SpeedLimit<<endl;
}
};
class HeavyMotor :public Vehicle
{
protected:
int LoadCapacity;
char permit[25];
public:
void Getdata(void)
{
Vehicle::GetData();
Cout<<”Load Carrying Capacity”;
Cin>>LoadCapacity;
Cout<<”Permit Type”;
Cin>>permit;
}
void DisplayData(void)
{
Vehicle::DisplayData();
Cout<<”Load Carrying Capacity”<<LoadCapacity<<endl;
Cout<<”Permit”<<permit<<endl;
}
};
class GearMotoe : public LightMotor
{
protected:
int GearCount;
public:
void GetData(void)
{
LightMotor::GetData();
Cout<<”No. of Gears”;
Cin>>GearCount;
}
void DisplayData(void)
{
LightMotor::DisplayData();
Cout<<”Gears”<<GearCount<<endl;
}
};
class NonGearMotor : public LightMotor
{
public:
void GetData(void)
{
LightMotor::GetData();
}
void DisplayData(void)
{
LightMotor::DisplayData();
}
};
class passenger: public HeavyMotor
{
protected:
int sitting;
int standing;
public:
void GetData(void)
{
HeavyMotor::GetData();
Cout<<”Maximum Seats”;
Cin>>sitting;
Cout<<”Maximum Standing”;
Cin>>standing;
}
void DisplayData(void)
{
HeavyMotor::DisplayData();
Cout<<”Maximum Seats”<<sitting<<endl;
Cout<<”Maximum Standing”<<standing<<endl;
}
};
class Goods : public HeavyMotor
{
public:
void GetData(void)
{
HeavyMotor::GetData();
}
void DisplayData(void)
{
HeavyMotor::DisplayData();
}
};
void main()
{
GearMotor vehi1;
Passenger vehi2;
Cout<<”Enter Data for Gear Motor Vehicle…….”<<endl;
Vehi1.GetData();
Cout<<”Enter data for passendger motor vehicle…”<<endl;
Vehi2.GetData();
Cout<<”data of gear motor vehicle…”<<endl;
Vehi1.DisplayData();
Cout<<”Data of passenger motor vehicle”<<endl;
Vehi2.DisplayData();
}
No comments:
Post a Comment