Write a program using constructor overloading to implement the following tasks:
1.Print sides of a cube
2.Print radius and height of the cylinder
3.Calculate the volume of a cube
4.Calculate thevolume of a cylinder
#include <iostream>
using namespace std;
class Shape{
float side = 0, radius, height, pi = 3.14159;
public:
Shape(float s){
side = s;
}
Shape(float r, float h){
radius = r;
height = h;
}
void print(){
if(side != 0)
cout<<"The sides of the cube is "<<side;
else
cout<<"\nThe radius is "<<radius<<" and height is "<<height;
}
void volume(){
if(side != 0)
cout<<"\nThe volume of the cylinder is "<<pi * radius * radius * height;
else
cout<<"\nThe volume of the cube is "<<side * side * side;
}
};
int main(){
Shape cube(5), cylinder(7, 15);
cube.print();
cylinder.print();
cube.volume();
cylinder.volume();
return 0;
}
Comments
Leave a comment