1.C++如何定义一个类?
时间: 2024-05-30 10:15:18 浏览: 86
To define a class in C++, you need to use the 'class' keyword followed by the class name, and then list the member variables and functions within the curly braces. Here's an example:
class MyClass {
private:
int myVar; // private member variable
public:
void setMyVar(int val) { // public member function to set myVar
myVar = val;
}
int getMyVar() { // public member function to get myVar
return myVar;
}
};
In this example, the class is named 'MyClass', and it contains a private member variable 'myVar', as well as two public member functions 'setMyVar' and 'getMyVar' to manipulate the variable.
阅读全文