A class is a blueprint. An object is an instance of that blueprint.
class Rectangle {
public:
double width;
double height;
double area() {
return width * height;
}
double perimeter() {
return 2 * (width + height);
}
};
Rectangle r;
r.width = 5.0;
r.height = 3.0;
cout << r.area(); // 15
cout << r.perimeter(); // 16
struct members are public by default; class members are private by default. Otherwise they're identical.
Create a Circle class with:
double radiusdouble area() — returns PI * r * rdouble circumference() — returns 2 * PI * rUse 3.14159265 for PI.
Click "Run" to execute your code.