TASK2:
Complete the following tasks:
a. Create a base class named Rectangle that includes data members for the length
and width of a Rectangle, as well as functions to as sign and display those values.
Derive a class named Block that contains an additional data member to store height,
and contains functions to assign and display the height. Write a main()function that
demonstrates the classes by instantiating and displaying the values for both a
Rectangle and a Block. Save the file as RectangleAndBlock.cpp.
b. Add a member function to the Rectangle class that computes the area of a
Rectangle (length multiplied by width). Add a member function to Block that has the
same name, but overrides the computation with a volume calculation (length by
width by height). Write a main() function that demonstrates the classes. Save the file
as RectangleAndBlock2.cpp.
CODE(A):
#include<iostream>
#include<conio.h>
using namespace std;
class rectangle
{
private:
float length,width;
public:
void getdata()
{
cout<<"Enter
the value of length:";cin>>length;
cout<<"Enter
the value of width:";cin>>width;
}
void showdata()
{
cout<<"Length:"<<length<<endl;
cout<<"Width:"<<width<<endl;
}
};
class block:public rectangle
{
private:
float height;
public:
void getdata()
{
rectangle::getdata();
cout<<"Enter
value of height:";cin>>height;
}
void showdata()
{
rectangle::showdata();
cout<<"Height:"<<height<<endl;
}
};
int main()
{
rectangle rec1;
block bl1;
cout<<"Enter data for rectangle
"<<endl;
rec1.getdata();
cout<<"Enter dat for block"<<endl;
bl1.getdata();
cout<<"Entered data for rectangle
is"<<endl;
rec1.showdata();
cout<<"Entered data for block
is"<<endl;
bl1.showdata();
cout<<endl;
getche();
return 0;
}
OUTPUT:
Enter data for rectangle
Enter the value of length:75
Enter the value of width:38
Enter dat for block
Enter the value of length:92
Enter the value of width:83
Enter value of height:23
Entered data for rectangle is
Length:75
Width:38
Entered data for block is
Length:92
Width:83
Height:23
CODE(B):
#include<iostream>
#include<conio.h>
using namespace std;
float area1;
class rectangle
{
private:
float length,width;
public:
void getdata()
{
cout<<"Enter
the value of length:";cin>>length;
cout<<"Enter
the value of width:";cin>>width;
}
void showdata()
{
cout<<"Length:"<<length<<endl;
cout<<"Width:"<<width<<endl;
cout<<"Area:";
cout<<AreaAndVolume(length,width)<<endl;
}
int AreaAndVolume(float
length2,float width2)
{
area1=length*width;
return area1;
}
};
class block:public rectangle
{
private:
float height;
public:
void getdata()
{
cout<<"Enter
value of height:";cin>>height;
}
void showdata()
{
cout<<"Height:"<<height<<endl;
cout<<"volume:";
cout<<AreaAndVolume(height)<<endl;
}
int AreaAndVolume(float
height)
{
float vol1;
vol1=area1*height;
return vol1;
}
};
int main()
{
rectangle rec1;
block bl1;
cout<<"Enter data for rectangle
"<<endl;
rec1.getdata();
cout<<"Enter dat for block"<<endl;
bl1.getdata();
cout<<"Entered data for rectangle
is"<<endl;
rec1.showdata();
cout<<"Entered data for block
is"<<endl;
bl1.showdata();
cout<<endl;
getche();
return 0;
}
Output:
Enter data for rectangle
Enter the value of length:75
Enter the value of width:86
Enter dat for block
Enter value of height:758
Entered data for rectangle is
Length:75
Width:86
Area:6450
Entered data for block is
Height:758
volume:4889100