请判断类的声明形式是否正确 class A int x; public: A(int t=0):x(t){ } int operator-(const A&a1){return x-a1.x;} };
时间: 2023-07-15 10:14:53 浏览: 42
类的声明形式不正确。应该使用下面的形式:
```
class A {
public:
int x;
A(int t = 0): x(t) {}
int operator-(const A& a1) {
return x - a1.x;
}
};
```
在类的声明中,成员变量和成员函数应该分别在 public、protected 或 private 关键字下进行声明。同时,在成员函数的定义中,应该加上类名和作用域解析符号 ::。
相关问题
请判断类的声明形式是否正确 classA int x; public: A(int t=θ):x(t){ } int operator- (const AGal){return x-a1.x;} };
这个类的声明形式是不正确的。应该是这样的:
```
class A {
private:
int x;
public:
A(int t=0):x(t){ }
int operator- (const A &a1){return x-a1.x;}
};
```
修正后的类声明中,类名应该是 `A` 而不是 `classA`,并且需要声明类中的成员变量和成员函数的访问权限。此外,`operator-` 的参数应该是 `const A &a1` 而不是 `const AGal`。
以下定义是否正确( )。 class A { public: int n=0; void fun(); };
这个定义是不正确的。在类定义中,不能直接初始化非静态成员变量。正确的写法应该是在类定义中声明非静态成员变量,但不进行初始化,在类外部定义成员函数时进行初始化,例如:
```
class A {
public:
int n;
void fun();
};
void A::fun() {
n = 0; // 在成员函数中初始化成员变量
}
```