What is Example Of Class C++?
Here's a simple example of a class in C++:
#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Constructor
Rectangle(int w, int h) : width(w), height(h) {}
// Method to calculate area
int area() {
return width * height;
}
};
int main() {
Rectangle rect(5, 10);
cout << "Area: " << rect.area() << endl; // Output: Area: 50
return 0;
}
This class defines a Rectangle
with a constructor and a method to compute its area.