What is C++ Class Example?
Here's a simple C++ class example:
#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
Rectangle(int w, int h) : width(w), height(h) {}
int area() {
return width * height;
}
};
int main() {
Rectangle rect(5, 10);
cout << "Area: " << rect.area() << endl;
return 0;
}
This code defines a Rectangle
class with private attributes for width and height and a public method to calculate the area.