Write a c++ program using constructor overloading to implement the following tasks:
1. Print sides of the cube
2. Print radius and height of the cylinder
3. Calculate and display the volume of the cube
4. Calculate and display volume of the cylinder
#include <iostream>
#define PI 3.14159
using namespace std;
class Shape
{
float side;
float radius;
float height;
public:
Shape(float _side):side(_side),radius(0),height(0){}
Shape(float _radius,float _height)
:radius(_radius), height(_height),side(0){}
void PrintCube()
{
cout << "Sides of cube is " << side;
}
void PrintCylinder()
{
cout << "\nRadius of the cylinder is " << radius
<< "\nHeight of the cylinder is " << height;
}
void CalculateCube()
{
cout << "\nThe volume of the cube is " << side*side*side;
}
void CalculateCylinder()
{
cout << "\nThe volume of th cylinder is " << PI*radius*radius*height;
}
};
int main()
{
Shape a(3);
a.PrintCube();
a.CalculateCube();
Shape b(4, 5);
b.PrintCylinder();
b.CalculateCylinder();
}
Comments
Leave a comment