Given the C++ statements as below: #include using namespace std; const PI = 3.142; class Cone{ private: double radius; double height; public: Cone(){ // default constructor radius = 0; height = 0; cout << "Default constructor created !" << endl; } Cone(double radius, double height){ // overloaded constructor radius = radius; height = height; cout << "Overloaded constructor created !" << endl; } ~Cone(){ // destructor cout << "Object destroyed !" << endl; } double getRadius(){return radius;} double getHeight(){return height;} double calVolume(){ return (1.0/3.0) * PI * radius * radius * height; } void displayOutput(){ cout << "Volume of cone with radius of " << getRadius() << " and height of " << getHeight() << " is " << calVolume() << endl; } }; int main(){ Cone c1(); Cone c2(2, 6); c1.displayOutput(); c2.displayOutput(); return 0; } 1-There are some errors in the code. Identify and correct the errors. 2- Write the output of the above C++ program. 3- Rewrite the 2 constructors using constructor initializer list.
- Given the C++ statements as below:
#include <iostream> using namespace std;
const PI = 3.142;
class Cone{ private: double radius; double height; public: Cone(){ // default constructor radius = 0; height = 0; cout << "Default constructor created !" << endl; } Cone(double radius, double height){ // overloaded constructor radius = radius; height = height; cout << "Overloaded constructor created !" << endl; } ~Cone(){ // destructor cout << "Object destroyed !" << endl; } double getRadius(){return radius;} double getHeight(){return height;}
double calVolume(){ return (1.0/3.0) * PI * radius * radius * height; } void displayOutput(){ cout << "Volume of cone with radius of " << getRadius() << " and height of " << getHeight() << " is " << calVolume() << endl; } }; int main(){ Cone c1(); Cone c2(2, 6); c1.displayOutput(); c2.displayOutput(); return 0; } |
1-There are some errors in the code. Identify and correct the errors.
2- Write the output of the above C++ program.
3- Rewrite the 2 constructors using constructor initializer list.
Step by step
Solved in 3 steps