Answer to Question #179958 in C++ for john

Question #179958

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



1
Expert's answer
2021-04-11T17:16:19-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS