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

Program Practical 13

The document contains two C++ programs. The first program calculates the area and perimeter of rectangles using inheritance. It defines a base Rectangle class and derived Area and Perimeter classes that calculate these values. The second program defines a class hierarchy for representing cricketers with classes for Bowler, Batsman and a derived Allrounder class. It accepts and displays player names using functions from the base classes.

Uploaded by

Samyak Kalaskar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Program Practical 13

The document contains two C++ programs. The first program calculates the area and perimeter of rectangles using inheritance. It defines a base Rectangle class and derived Area and Perimeter classes that calculate these values. The second program defines a class hierarchy for representing cricketers with classes for Bowler, Batsman and a derived Allrounder class. It accepts and displays player names using functions from the base classes.

Uploaded by

Samyak Kalaskar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Rollno.

20

1. Write a C++ Program to calculate the area and perimeter of rectangles using concept of
inheritance

#include <iostream>
using namespace std;
class Rectangle
{
protected:
float length, breadth;
public:
Rectangle(): length(0.0), breadth(0.0)
{
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}

};

/* Area class is derived from base class Rectangle. */


class Area : public Rectangle
{
public:
float calc()
{
return length*breadth;
}

};

/* Perimeter class is derived from base class Rectangle. */


class Perimeter : public Rectangle
{
public:
float calc()
{
return 2*(length+breadth);
}
};

int main()
{
cout<<"Enter data for first rectangle to find area.\n";
Area a;
cout<<"Area = "<<a.calc()<<" square meter\n\n";

cout<<"Enter data for second rectangle to find perimeter.\n";


Perimeter p;
cout<<"\nPerimeter = "<<p.calc()<<" meter";
return 0;
}
Output:

Enter data for first rectangle to find area.

Enter length: 20

Enter breadth: 30

Area = 600 square meter

Enter data for second rectangle to find perimeter.

Enter length: 30

Enter breadth: 45

Perimeter = 150 meter


2. write a c++ program for representation of class hierarchy as below . assume suitable data
and function member

#include<iostream>

using namespace std;

class Cricketer

protected:

char name[20];

};

class Bowler:public Cricketer

public:

void accept()

cout<<"\n\n Enter Bowler name: ";

cin>>name;

void display()

cout<<"\n\n Bowler : "<<name;

};

class Batsman:public Cricketer

public:

void accept()

{
cout<<"\n\n Enter Batsman name: ";

cin>>name;

void display()

cout<<"\n\n Batsman : "<<name;

};

class Allrounder:public Bowler,public Batsman

char name[10];

public:

void accept()

Bowler::accept();

Batsman::accept();

cout<<"\n\n Enter Allrounder name: ";

cin>>name;

void display()

Bowler::display();

Batsman::display();

cout<<"\n\n Allrounder : "<<name;

}
};

int main()

Allrounder a;

a.accept();

a.display();

return 0;

Ouput :

Enter Bowler name: bolt

Enter Batsman name: virat

Enter Allrounder name: hardik

Bowler : bolt

Batsman : virat

Allrounder : hardik

You might also like