OOP Lab7
OOP Lab7
#include<iostream>
using namespace std;
class BasicCar{
public:
void start()
{
cout<<"Car started"<<endl;
}
};
int main()
{
AdvanceCar a;
a.start();
a.playMusic();
}
//Multiple Inheritance
#include<iostream>
private:
int runstaken;
int BattAvg;
int Noh;
int Noc;
int Sr;
int HighestS;
int nos;
public:
void getdata()
cin>>runstaken;
cin>>BattAvg;
cin>>Noh;
cin>>Sr;
cin>>HighestS;
cin>>nos;
void showdata()
cout<<"Runstaken: "<<runstaken<<endl;
};
class bowler{
private:
char type[10];
int Now;
double RR;
int speed;
double economy;
char arm[10];
public:
void getdata()
cin>>type;
cin>>Now;
cin>>RR;
cin>>speed;
cin>>arm;
void showdata()
cout<<"Economy: "<<economy<<endl;
cout<<"Arm: "<<arm<<endl;
};
private:
char name[20];
int Nmom;
int Nimp;
int Noc;
int rankingIcc;
int stylistPlayer;
public:
void getdata()
cin>>name;
batsman::getdata();
bowler::getdata();
cin>>Nmom;
cin>>Nimp;
cin>>Noc;
cin>>rankingIcc;
void showdata()
cout<<"Name: "<<name<<endl;
batsman::showdata();
bowler::showdata();
};
int main()
AllRounder ar1;
ar1.getdata();
ar1.showdata();
return 0;
//Hierarchial inheritance
#include<iostream>
class cricketer{
protected:
char name[20];
int age;
int jn;
int nop;
public:
void getdata()
cin>>name;
cin>>age;
cout<<"Jersey number: ";
cin>>jn;
cin>>nop;
void showdata()
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
};
private:
int runstaken;
int BattAvg;
int Noh;
int Noc;
int Sr;
int HighestS;
int nos;
public:
void getdata()
cricketer::getdata();
cin>>runstaken;
cin>>BattAvg;
cin>>Noh;
cin>>Noc;
cin>>Sr;
cin>>HighestS;
cin>>nos;
}
void showdata()
cricketer::showdata();
cout<<"Runstaken: "<<runstaken<<endl;
};
private:
char type[10];
int Now;
double RR;
int speed;
double economy;
char arm[10];
public:
void getdata()
cricketer::getdata();
cin>>type;
cin>>Now;
cin>>RR;
cin>>speed;
cin>>economy;
cin>>arm;
void showdata()
cricketer::showdata();
cout<<"bowler type: "<<type<<endl;
cout<<"Economy: "<<economy<<endl;
cout<<"Arm: "<<arm<<endl;
};
int main()
batsman b1;
b1.getdata();
b1.showdata();
bowler b2;
b2.getdata();
b2.showdata();
return 0;
}
//Multipath Inheritance
#include <iostream>
class student
private:
char name[50];
int roll;
int age;
public:
void getdata()
void showdata()
cout << "Name of the student: " << name << endl;
};
private:
int marks[3];
public:
int getSubMarks(int i)
else
return 0;
void getdata()
student::getdata();
void showdata()
student::showdata();
};
private:
int act[2];
public:
int getActMarks(int i)
return act[i];
else
return 0;
void getdata()
{
cin >> act[i];
void showdata()
student::showdata();
};
private:
float total;
float perc;
char grade;
public:
void totalMM()
total = 0;
void showTotal()
void percent()
perc = total / 3;
void showGrade()
else
};
int main()
result r;
r.test::getdata();
r.test::showdata();
r.Activities::getdata();
r.Activities::showdata();
r.totalMM();
r.showTotal();
r.percent();
r.showGrade();
return 0;
//Multilevel Inheritance
#include <iostream>
class student
private:
char name[50];
int roll;
int age;
public:
void getdata()
void showdata()
cout << "Name of the student: " << name << endl;
};
private:
int marks[5];
public:
int getithmarks(int i)
return marks[i];
else
return 0;
void getdata()
student::getdata();
}
}
void showdata()
student::showdata();
};
private:
float total;
float perc;
char grade;
public:
void totalMM()
{
total = 0;
//test::getdata();
void showTotal()
cout<<total<<endl;
void percent()
perc = total/5;
cout<<perc<<"%"<<endl;
void showGrade()
{
if(perc>90)
cout<<"A"<<endl;
else if(perc>80)
cout<<"B"<<endl;
else if(perc>70)
cout<<"BA"<<endl;
else
cout<<"F"<<endl;
};
int main()
result r;
r.getdata();
r.showdata();
r.totalMM();
r.showTotal();
r.percent();
r.showGrade();
return 0;
2.Create a class ‘shape’. Derive three classes from it: Circle, Triangle and
Rectangle. Include the relevant data members and functions in all the classes.
Find the area of each shape and display it.
#include <bits/stdc++.h>
using namespace std;
class Shape
{
protected:
double area1;
public:
int getarea()
{
return area1;
}
void showdata()
{
cout << "Area is: " << endl;
}
};
private:
int rad;
public:
void getdata()
{
cout << "Enter radius of circle: ";
cin >> rad;
}
void area()
{
Shape::getarea();
area1 = 3.14 * rad * rad;
Shape::showdata();
cout << area1 << endl;
}
};
public:
void getdata()
{
cout << "Enter sides of triangle: ";
cin >> a >> b >> c;
}
void area()
{
double s;
s = (a + b + c) / 2;
Shape::getarea();
area1 = sqrt(s * (s - a) * (s - b) * (s - c));
Shape::showdata();
public:
void getdata()
{
cout << "Enter lenght of rectangle: ";
cin >> length;
cout << "Enter breadth of rectangle: ";
cin >> breadth;
}
void area()
{
Shape::getarea();
area1 = length * breadth;
Shape::showdata();
int main()
{
Circle c1;
c1.getdata();
c1.area();
Triangle t1;
t1.getdata();
t1.area();
Rectangle r1;
r1.getdata();
r1.area();
}
3.
a)Create a class student which stores name, roll number and age of a student.
Derive a class test from student class, which stores marks in 5 subjects.
Input and display the details of a student.
#include <iostream>
using namespace std;
class student
{
private:
char name[50];
int roll;
int age;
public:
void getdata()
{
cout << "Enter name of student: ";
cin >> name;
cout << endl
<< "Enter roll number: ";
cin >> roll;
cout << endl
<< "Enter age: ";
cin >> age;
}
void showdata()
{
cout << "Name of the student: " << name << endl;
cout << "Roll number: " << roll << endl;
cout << "Age of student: " << age << endl;
}
};
public:
void getdata()
{
student::getdata();
int main()
{
test t1;
t1.getdata();
t1.showdata();
return 0;
}
b)Extend the program (a)to derive a class from result from classs ‘test’ which
includes member function to calculate total marks
and percentage of a student. Input the data for a student and display its total
marks and percentage.
#include <iostream>
using namespace std;
class student
{
private:
char name[50];
int roll;
int age;
public:
void getdata()
{
cout << "Enter name of student: ";
cin >> name;
cout << endl
<< "Enter roll number: ";
cin >> roll;
cout << endl
<< "Enter age: ";
cin >> age;
}
void showdata()
{
cout << "Name of the student: " << name << endl;
cout << "Roll number: " << roll << endl;
cout << "Age of student: " << age << endl;
}
};
public:
int getithmarks(int i)
{
if (i >= 0 && i < 5)
return marks[i];
else
return 0;
}
void getdata()
{
student::getdata();
public:
void totalMM()
{
total = 0;
//test::getdata();
for (int i = 0; i < 5; i++)
{
total = total + getithmarks(i);
}
}
void showTotal()
{
cout<<"Your total: ";
cout<<total<<endl;
}
void percent()
{
perc = total/5;
cout<<"Your percentage: ";
cout<<perc<<"%"<<endl;
}
void showGrade()
{
cout<<"Your Grade: ";
if(perc>90)
cout<<"A"<<endl;
else if(perc>80)
cout<<"B"<<endl;
else if(perc>70)
cout<<"BA"<<endl;
else
cout<<"F"<<endl;
}
};
int main()
{
result r;
r.getdata();
r.showdata();
r.totalMM();
r.showTotal();
r.percent();
r.showGrade();
return 0;
}
c)Extend the program (b) to include a class sports, which stores the marks in
sports activity.
Derive the result class from the classes ‘test’ and ‘sports’.
Calculate the total marks and percentage of a student.
// Multipath Inheritance
#include <iostream>
using namespace std;
class student
{
private:
char name[50];
int roll;
int age;
public:
void getdata()
{
cout << "Enter name of student: ";
cin >> name;
cout << endl
<< "Enter roll number: ";
cin >> roll;
cout << endl
<< "Enter age: ";
cin >> age;
}
void showdata()
{
cout << "Name of the student: " << name << endl;
cout << "Roll number: " << roll << endl;
cout << "Age of student: " << age << endl;
}
};
public:
int getSubMarks(int i)
{
if (i >= 0 && i < 3)
return marks[i];
else
return 0;
}
void getdata()
{
student::getdata();
public:
int getActMarks(int i)
{
if (i >= 0 && i < 2)
return act[i];
else
return 0;
}
void getdata()
{
cout << "Enter activity marks: ";
for (int i = 0; i < 2; i++)
{
cin >> act[i];
}
}
void showdata()
{
student::showdata();
public:
void totalMM()
{
total = 0;
void percent()
{
perc = total / 3;
cout << "Your percentage: ";
cout << perc << "%" << endl;
}
void showGrade()
{
cout << "Your Grade: ";
if (perc > 90)
cout << "A" << endl;
else if (perc > 80)
cout << "B" << endl;
else if (perc > 70)
cout << "BA" << endl;
else
cout << "F" << endl;
}
};
int main()
{
result r;
r.test::getdata();
r.test::showdata();
r.Activities::getdata();
r.Activities::showdata();
r.totalMM();
r.showTotal();
r.percent();
r.showGrade();
return 0;
}