LAB Assignment5 Solutions
LAB Assignment5 Solutions
--
#include <iostream>
using namespace std;
class Person {
public:
string name;
string address;
void get() {
cout << "Enter the name: ";
cin >> name;
cout << "Enter the address: ";
cin >> address;
}
};
void getInfo() {
cout << "Enter the employee ID: ";
cin >> employee_id;
cout << "Enter the department: ";
cin >> department;
}
};
void getAgain() {
cout << "Enter the student ID: ";
cin >> student_id;
cout << "Enter the grade: ";
cin >> grade;
}
};
class TeachingAssistant : public Student, public Staff {
public:
void display() {
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "Department: " << department << endl;
cout << "Student ID: " << student_id << endl;
cout << "Grade: " << grade << endl;
cout << "Employee ID: " << employee_id << endl;
}
};
int main() {
TeachingAssistant ta;
ta.get(); // Gets the name and address
ta.getInfo(); // Gets staff-specific info
ta.getAgain(); // Gets student-specific info
ta.display(); // Displays all the information
return 0;
}
$$QUESTION 9
--
#include<iostream>
#include<string>
using namespace std;
class Vehicle{
public:
string make;
string model;
int year;
void get(){
cin>>make>>model>>year;
}
};
class Truck:public Vehicle{
public:
float load_capacity;
void getinfo(){
cout<<"enter the load capacity: ";
cin>>load_capacity;
}
};
class RefrigeratedTruck:public Truck{
public:
float temperature_control;
void gettemp(){
cout<<"enter the temperature control: ";
cin>>temperature_control;
}
void display(){
cout<<"make-"<<make<<endl<<"model-"<<model<<endl<<"year-"<<year<<endl<<"load
capacity-"<<load_capacity<<endl<<"temperature control-"<<temperature_control;
}
};
int main(){
RefrigeratedTruck r;
r.get();
r.getinfo();
r.gettemp();
r.display();
}
$$QUESTION 8
--
#include<iostream>
#include<string>
using namespace std;
class LibraryUser{
public:
string name;
int id,contact;
void getdata(){
cout<<"enter the name: ";
cin>>name;
cout<<"enter the id: ";
cin>>id;
cout<<"enter the contact: ";
cin>>contact;
}
};
class teacher:public LibraryUser{
public:
string department;
void getdep(){
cout<<"enter the department: ";
cin>>department;
}
void show(){
cout<<"name-"<<name<<endl<<"id-"<<id<<endl<<"contact-"<<contact<<endl<<"department-"<
<department;
}
};
class student:public LibraryUser{
public:
string grade;
void getgrade(){
cout<<"enter the grade level: ";
cin>>grade;
}
void show2(){
cout<<"name-"<<name<<endl<<"id-"<<id<<endl<<"contact-"<<contact<<endl<<"grade-"<<grad
e;
}
};
int main(){
student s;
cout<<"for student: ";
s.getdata();
s.getgrade();
s.show2();
teacher t;
cout<<"\nfor teacher: ";
t.getdata();
t.getdep();
t.show();
}
$$QUESTION 7
#include <iostream>
using namespace std;
class general{
public:
string name;
int ID,contact;
string speed;
SpeedoMeter(){
speed="10 km/hr";
}
};
class Teacher{
public:
string fuel;
FuelGage(){
fuel="5 Liters";
}
};
class Class{
public:
string temp;
Thermometer(){
temp="900 Kelvin";
}
};
class dashboard:public Thermometer,public FuelGage,public SpeedoMeter{
public:
dashboard(){
cout<<"FUEL :"<<fuel<<endl;
cout<<"SPEED :"<<speed<<endl;
cout<<"TEMPERATURE :"<<temp<<endl;
}
};
int main(){
dashboard car;
return 0;
}
$$QUESTION 6
#include <iostream>
using namespace std;
class BOOK{
public:
string authour,title;
int price;
BOOK(){
cout<<"ENTER title authour and price"<<endl;
cin>>title>>authour>>price;
}
};
class TEXTBOOK:public BOOK{
public:
string subject;
TEXTBOOK(){
cout<<"enter subject"<<endl;
cin>>subject;
}
void display(){
cout<<"\n";
cout<<"NAME :"<<title<<endl;
cout<<"AUTHOUR :"<<authour<<endl;
cout<<"SUBJECT :"<<subject<<endl;
cout<<"PRICE :"<<price<<endl;
}
};
int main() {
TEXTBOOK t;
t.display();
}
$$QUESTION 5
#include <iostream>
using namespace std;
class base{
public:
int x;
base(){
x=10;
cout<<"your number is base "<<x<<endl;
}
~base(){
cout<<"base killed"<<endl;
}
};
class derived:public base{
public:
derived(){
x++;
cout<<"your number is child "<<x<<endl;
}
~derived(){
cout<<"child killed"<<endl;
}
};
int main() {
derived d1;
}
$$QUESTION 4
--SINGLE INHERITANCE
int main() {
derived d1;
d1.y=10;
d1.display();
}
--HYBRID INHERITANCE
#include <iostream>
using namespace std;
class base{
public:
int x;
};
int main() {
DERIVED d;
d.x=10;
d.add();
d.display();
}
--MULTIPLE INHERITANCE
#include <iostream>
using namespace std;
class base_a{
public:
int x;
};
class base_b{
public:
int y;
};
int main() {
derived d1;
d1.x=11;
d1.y=12;
d1.display();
--hierarchial INHERITANCE
int main() {
derived_b d1;
d1.y=1;
cout<<d1.y;
}