0% found this document useful (0 votes)
33 views

OOP Lab7

The document discusses different types of inheritance in C++ including simple, multiple, hierarchical, and multipath inheritance. It provides code examples to demonstrate each type. For simple inheritance, it shows a BasicCar class that is inherited by an AdvancedCar class. For multiple inheritance, it defines classes for batsman, bowler, and allrounder that inherits from both. For hierarchical inheritance, it defines classes for cricketer, batsman and bowler that inherit from cricketer. For multipath inheritance, it defines classes for student, test, activities and result to demonstrate multiple inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

OOP Lab7

The document discusses different types of inheritance in C++ including simple, multiple, hierarchical, and multipath inheritance. It provides code examples to demonstrate each type. For simple inheritance, it shows a BasicCar class that is inherited by an AdvancedCar class. For multiple inheritance, it defines classes for batsman, bowler, and allrounder that inherits from both. For hierarchical inheritance, it defines classes for cricketer, batsman and bowler that inherit from cricketer. For multipath inheritance, it defines classes for student, test, activities and result to demonstrate multiple inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

OOP-Lab 7

Name : Utkarsh Trivedi Roll: 21053223


Section: CSE 39 Date: 26-10-22

1.Write individual programs to demonstrate each type of inheritance.


//Simple Inheritance

#include<iostream>
using namespace std;

class BasicCar{
    public:
        void start()
    {
            cout<<"Car started"<<endl;
    }
};

class AdvanceCar: public BasicCar


{
    public:
        void playMusic(){
            cout<<"Play music"<<endl;
    }
};

int main()
{
    AdvanceCar a;
    a.start();
    a.playMusic();
}

//Multiple Inheritance

#include<iostream>

using namespace std;


class batsman{

private:

int runstaken;

int BattAvg;

int Noh;

int Noc;

int Sr;

int HighestS;

int nos;

public:

void getdata()

cout<<"Enter Runstaken: ";

cin>>runstaken;

cout<<"Enter Batting Avg: ";

cin>>BattAvg;

cout<<"Enter number of half centuries: ";

cin>>Noh;

cout<<"Enter number of centuries: ";


cin>>Noc;

cout<<"Enter Strike rate: ";

cin>>Sr;

cout<<"Enter highest score: ";

cin>>HighestS;

cout<<"Enter number of sixes: ";

cin>>nos;

void showdata()

cout<<"Runstaken: "<<runstaken<<endl;

cout<<"Batting Avg: "<<BattAvg<<endl;

cout<<"Number of half centuries: "<<Noh<<endl;

cout<<"Number of centuries: "<<Noc<<endl;

cout<<"Strike rate: "<<Sr<<endl;

cout<<"Highest score: "<<HighestS<<endl;

cout<<"Number of sixes: "<<nos<<endl;

};
class bowler{

private:

char type[10];

int Now;

double RR;

int speed;

double economy;

char arm[10];

public:

void getdata()

cout<<"Enter type of bowler: ";

cin>>type;

cout<<"Enter number of wickets taken: ";

cin>>Now;

cout<<"Enter run rate: ";

cin>>RR;

cout<<"Enter bowling speed: ";

cin>>speed;

cout<<"Enter economy: ";


cin>>economy;

cout<<"Enter arm: ";

cin>>arm;

void showdata()

cout<<"bowler type: "<<type<<endl;

cout<<"number of wickets: "<<Now<<endl;

cout<<"Run rate: "<<RR<<endl;

cout<<"bowling speed: "<<speed<<endl;

cout<<"Economy: "<<economy<<endl;

cout<<"Arm: "<<arm<<endl;

};

class AllRounder : public batsman,bowler{

private:

char name[20];

int Nmom;
int Nimp;

int Noc;

int rankingIcc;

int stylistPlayer;

public:

void getdata()

cout<<"Enter name of allrounder: ";

cin>>name;

batsman::getdata();

bowler::getdata();

cout<<"Enter number of man of the match: ";

cin>>Nmom;

cout<<"Enter number of international matches played: ";

cin>>Nimp;

cout<<"Number of catches: ";

cin>>Noc;

cout<<"Enter ICC ranking: ";

cin>>rankingIcc;

cout<<"Stylish player of the match: ";


cin>>stylistPlayer;

void showdata()

cout<<"Name: "<<name<<endl;

batsman::showdata();

bowler::showdata();

cout<<"number of man of the match: "<<Nmom<<endl;

cout<<"number of catches: "<<Noc<<endl;

cout<<"number of international matches played: "<<Nimp<<endl;

cout<<"ICC ranking: "<<rankingIcc<<endl;

cout<<"stylish player of the match: "<<stylistPlayer<<endl;

};

int main()

AllRounder ar1;

ar1.getdata();

ar1.showdata();
return 0;

//Hierarchial inheritance

#include<iostream>

using namespace std;

class cricketer{

protected:

char name[20];

int age;

int jn;

int nop;

public:

void getdata()

cout<<"Enter name of cricketer: ";

cin>>name;

cout<<"Enter age: ";

cin>>age;
cout<<"Jersey number: ";

cin>>jn;

cout<<"Number of ODI's played: ";

cin>>nop;

void showdata()

cout<<"Name: "<<name<<endl;

cout<<"Age: "<<age<<endl;

cout<<"jersey number: "<<jn<<endl;

cout<<"number of ODI's played: "<<nop<<endl;

};

class batsman : public cricketer{

private:

int runstaken;

int BattAvg;

int Noh;

int Noc;

int Sr;
int HighestS;

int nos;

public:

void getdata()

cricketer::getdata();

cout<<"Enter Runstaken: ";

cin>>runstaken;

cout<<"Enter Batting Avg: ";

cin>>BattAvg;

cout<<"Enter number of half centuries: ";

cin>>Noh;

cout<<"Enter number of centuries: ";

cin>>Noc;

cout<<"Enter Strike rate: ";

cin>>Sr;

cout<<"Enter highest score: ";

cin>>HighestS;

cout<<"Enter number of sixes: ";

cin>>nos;

}
void showdata()

cricketer::showdata();

cout<<"Runstaken: "<<runstaken<<endl;

cout<<"Batting Avg: "<<BattAvg<<endl;

cout<<"Number of half centuries: "<<Noh<<endl;

cout<<"Number of centuries: "<<Noc<<endl;

cout<<"Strike rate: "<<Sr<<endl;

cout<<"Highest score: "<<HighestS<<endl;

cout<<"Number of sixes: "<<nos<<endl;

};

class bowler:public cricketer{

private:

char type[10];

int Now;

double RR;

int speed;

double economy;

char arm[10];
public:

void getdata()

cricketer::getdata();

cout<<"Enter type of bowler: ";

cin>>type;

cout<<"Enter number of wickets taken: ";

cin>>Now;

cout<<"Enter run rate: ";

cin>>RR;

cout<<"Enter bowling speed: ";

cin>>speed;

cout<<"Enter economy: ";

cin>>economy;

cout<<"Enter arm: ";

cin>>arm;

void showdata()

cricketer::showdata();
cout<<"bowler type: "<<type<<endl;

cout<<"number of wickets: "<<Now<<endl;

cout<<"Run rate: "<<RR<<endl;

cout<<"bowling speed: "<<speed<<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>

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;

};

class test : virtual public student

private:

int marks[3];

public:

int getSubMarks(int i)

if (i >= 0 && i < 3)


return marks[i];

else

return 0;

void getdata()

student::getdata();

cout << endl

<< "Enter marks: " << endl;

for (int i = 0; i < 3; i++)

cin >> marks[i];

void showdata()

student::showdata();

cout << "Marks of student in subject: " << endl;

for (int i = 0; i < 3; i++)

cout << "\t\t" << marks[i] << endl;


}

};

class Activities : virtual public student

private:

int act[2];

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();

cout << "Marks of student in activity: " << endl;

for (int i = 0; i < 2; i++)

cout << "\t\t" << act[i] << endl;

};

class result : public test , public Activities

private:

float total;

float perc;

char grade;
public:

void totalMM()

total = 0;

for (int i = 0; i < 3; i++)

total = total + getSubMarks(i) + getActMarks(i);

void showTotal()

[14:20, 13/10/2022] 𝙐𝙩𝙠𝙖𝙧𝙨𝙝𝙏: {

cout << "Your total: ";

cout << total << endl;

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;

//Multilevel 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;

};

class test : public student


{

private:

int marks[5];

public:

int getithmarks(int i)

if (i >= 0 && i < 5)

return marks[i];

else

return 0;

void getdata()

student::getdata();

cout << endl

<< "Enter marks: " << endl;

for (int i = 0; i < 5; i++)

cin >> marks[i];

}
}

void showdata()

student::showdata();

cout << "Marks of student: " << endl;

for (int i = 0; i < 5; i++)

cout << "\t\t" << marks[i] << endl;

};

class result : public test

private:

float total;

float perc;

char grade;

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;

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;
  }
};

class Circle : public Shape


{

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;
  }
};

class Triangle : public Shape


{
private:
    int a;
    int b;
    int c;

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();

        cout << area1 << endl;


  }
};

class Rectangle : public Shape


{
private:
    int length;
    int breadth;

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();

        cout << area1 << endl;


  }
};

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;
  }
};

class test : public student


{
private:
    int marks[5];

public:
        void getdata()
  {
        student::getdata();

        cout << endl


             << "Enter marks: " << endl;
        for (int i = 0; i < 5; i++)
    {
            cin >> marks[i];
    }
  }
    void showdata()
  {
        student::showdata();
        cout << "Marks of student: "<<endl;
        for (int i = 0; i < 5; i++)
    {
            cout <<"\t\t"<< marks[i] << endl;
    }
  }
};

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;
  }
};

class test : public student


{
private:
    int marks[5];

public:
    int getithmarks(int i)
  {
        if (i >= 0 && i < 5)
            return marks[i];
        else
            return 0;
  }
    void getdata()
  {
        student::getdata();

        cout << endl


             << "Enter marks: " << endl;
        for (int i = 0; i < 5; i++)
    {
            cin >> marks[i];
    }
  }
    void showdata()
  {
        student::showdata();
        cout << "Marks of student: " << endl;
        for (int i = 0; i < 5; i++)
    {
            cout << "\t\t" << marks[i] << endl;
    }
  }
};

class result : public test


{
private:
    float total;
    float perc;

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;
  }
};

class test : virtual public student


{
private:
    int marks[3];

public:
    int getSubMarks(int i)
  {
        if (i >= 0 && i < 3)
            return marks[i];
        else
            return 0;
  }
    void getdata()
  {
        student::getdata();

        cout << endl


             << "Enter marks: " << endl;
        for (int i = 0; i < 3; i++)
    {
            cin >> marks[i];
    }
  }
    void showdata()
  {
        student::showdata();
        cout << "Marks of student in subject: " << endl;
        for (int i = 0; i < 3; i++)
    {
            cout << "\t\t" << marks[i] << endl;
    }
  }
};

class Activities : virtual public student


{
private:
    int act[2];

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();

        cout << "Marks of student in activity: " << endl;


        for (int i = 0; i < 2; i++)
    {
            cout << "\t\t" << act[i] << endl;
    }
  }
};

class result : public test, public Activities


{
private:
    float total;
    float perc;

public:
    void totalMM()
  {
        total = 0;

        for (int i = 0; i < 3; i++)


    {
            total = total + getSubMarks(i) + getActMarks(i);
    }
  }
    void showTotal()
  {
        cout << "Your total: ";
        cout << total << endl;
  }

    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;
}

You might also like