Answer to Question #184112 in C++ for Uzair

Question #184112

Define a class Shape and create constant object from the class with at least one constant data member. Create object in main function from the class Shape. Also display state of the constant object in main( ) function


1
Expert's answer
2021-04-22T04:48:41-0400
#include <iostream>
 
using namespace std;
 
class Shape
{
public:
	Shape(double r);
	void SetRadius(double r) { radius = r; }
	double GetRadius() { return radius; }
	double GetArea() { return PI * radius * radius; }
 
private:
	double radius;
	const double PI = 3.145;
 
};
 
Shape::Shape(double r) : radius(r)
{
}
 
int main()
{
	cout << "Define new shape" << endl;
	Shape Circle(5);
	cout << "Shape is circle with radius " << Circle.GetRadius() << endl;
	cout << "Set radius to our shape to 10" << endl;
	Circle.SetRadius(10);
	cout << "Shape is circle with radius " << Circle.GetRadius() << endl;
	cout << "And its area is: " << Circle.GetArea() << endl;
	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