LAB 7 Inheritance (Home Task) s2023266146
LAB 7 Inheritance (Home Task) s2023266146
Lab Tasks
Task 1:
Create Class Person with variables weight,height, gender. Create another Class Employee with variables
designation, HoursPerDay. Now create another class Teacher and inherit it from Person and Employee and
add function display() which should show all the details related to teacher.
Program:
#include<iostream>
using namespace std;
class person
{
protected:
double weight=0;
double height=0;
string gender=" ";
public:
person()
{
this->weight=weight;
this->height=height;
this->gender=gender;
void input()
{
cout<<"enter your weight"<<endl;
cin>>weight;
cout<<"enter your height "<<endl;
cin>>height;
cout<<"enter your gender "<<endl;
cin>>gender;
}
};
class employee
{
protected:
string designation=" ";
long hoursperday=0;
public:
employee()
{
this->designation=designation;
this->hoursperday=hoursperday;
void input2()
{
cout<<"enter your desination = "<<endl;
cin>>designation;
cout<<"enter your hours per day "<<endl;
cin>>hoursperday;
}
};
class teacher:public employee,public person
{
public:
void display()
{
cout<<"weight = "<<weight<<endl;
cout<<"height ="<<height<<endl;
cout<<"gender ="<<gender<<endl;
cout<<"designation"<<designation<<endl;
cout<<"hoursperday = "<<hoursperday<<endl;
}
};
int main()
{
teacher t;
t.input();
t.input2();
t.display();
}
Compiler on screen :
Task 2:
Create Class Person with variables weight,height, gender and functions walk() sit(). Ceate another Class
Sudent with variable ID,First name, Last name, Graduation and function PrintDetail() Write(). Now create
a class GraduationStudent with variables UniversityName, YearGraduation and functions Display().
Note: In both parent classes simply add a printf() statement about the task. In child display function show
all data
Program :
#include<iostream>
using namespace std;
class person
{
protected:
double weight;
double height;
string gender;
public:
void walk()
{
weight=0;
height=0;
gender=" ";
}
void sit(double w,double h,string g)
{
weight=w;
height=h;
gender=g;
}
};
class employee
{
protected:
int id;
string firstname;
string lastname;
long graduation;
public:
void write()
{
firstname="";
lastname="";
graduation=0;
}
void printdetail(string f,string l,long gr)
{
firstname=f;
lastname=l;
graduation=gr;
}
};
class graduationstudent:public person,public employee{
string uni_name="";
long graduation_y=0;
public:
graduationstudent(string u,long y)
{
uni_name=u;
graduation_y=y;
}
public:
void display()
{
cout<<"weight = "<<weight<<endl;
cout<<"height ="<<height<<endl;
cout<<"gender ="<<gender<<endl;
cout<<"first name = "<<firstname<<endl;
cout<<"last name ="<<lastname<<endl;
cout<<"university = "<<uni_name<<endl;
cout<<"graduation ="<<graduation_y<<endl;
}
};
int main()
{
graduationstudent g("umt",2023);
g.sit(47,53.6,"female");
g.printdetail("ali","haider",2023);
g.display();
}
Compiler on screen :
Task 3:
Create Class Date with variables day,month,year and a function display() and another class Time with
variables hour,minutes,seconds and a function display(). Now create a third class named Time_Date it will
inherit from both Classes Date and Time it should have a function display() which will display time and
date.
Program:
#include<iostream>
using namespace std;
class date
{
protected:
long day;
string month;
long year;
public:
date()
{
day=0;
month="";
year=0;
}
date(long d,string m,long y)
{
day=d;
month=m;
year=y;
}
void setday(int d)
{
day=d;
}
void setmonth(string m)
{
month=m;
}
void setyear(int y)
{
year=y;
}
long getday()
{
return day;
}
string getmonth()
{
return month;
}
long getyear()
{
return year;
}
void display1()
{
cout<<"day ="<<day<<"month = "<<month<<" year = "<<year<<endl;
}
};
class time
{
protected:
int hours ,minutes,seconds;
public:
time()
{
hours=0;
minutes=0;
seconds=0;
}
time(int h,int min,int sec)
{
hours=h;
minutes=min;
seconds=sec;
}
void sethours(int h)
{
hours=h;
}
void setminutes(int min)
{
minutes=min;
}
void setseconds(int sec)
{
seconds=sec;
}
int gethours()
{
return hours;
}
int getminutes()
{
return minutes;
}
int getseconds()
{
return seconds;
}
public:
void display2()
{
cout<<"hours = "<<hours<<"minutes = "<<minutes<<"seconds = "<<seconds<<endl;
}
};
class date_time:public date,public time
{
public:
void display()
{
cout<<"day ="<<getday()<<"month = "<<getmonth()<<" year = "<<getyear()<<endl;
cout<<"hours = "<<gethours()<<"minutes = "<<getminutes()<<"seconds =
"<<getseconds()<<endl;
}
};
int main()
{
date_time d;
d.setday(23);
d.getday();
d.setmonth("january");
d.getmonth();
d.setyear(2023);
d.getyear();
d.sethours(24);
d.gethours();
d.setminutes(45);
d.getminutes();
d.setseconds(60);
d.getseconds();
d.display();
return 0;
Compiler on screen :
Task 4:
Create class named shape with variables height and width. Create another class named color with variable
color_name. Now create third class named Rectangle with variable area. Inherit rectangle class from shape
class and color class. Now calculate area of rectangle and define color of rectangle as well.
Program:
#include<iostream>
using namespace std;
class shape
{
protected:
double height;
double width;
public:
shape()
{
height=0;
width=0;
}
void setheight(int h)
{
height=h;
}
void setwidth(int w)
{
width=w;
}
int getheight()
{
return height;
}
int getwidth()
{
return width;
}
};
class color
{
protected:
string color_name;
public:
color()
{
color_name=" ";
}
void setcolor_name(string n)
{
color_name=n;
}
string getcolor_name()
{
return color_name;
}
};
class rectangle:public shape,public color
{
public:
void calarea()
{
cout<<"your result area ="<<getheight()*getwidth();
cout<<"your color = "<<getcolor_name()<<endl;
}
};
int main()
{
rectangle r;
r.setheight(24);
r.setwidth(32);
r.getheight();
r.getwidth();
r.setcolor_name("yellow");
r.getcolor_name();
r.calarea();
}
Compiler on screen :