改正后的代码如下:

class A { private: static int cnt; int x; public: A(int _x) { x = _x; cnt++; } A(const A& a) :x(a.x) { cnt++; } ~A() { cnt--; } int get() const { return x; } void set(int _x) { x = _x; } static int count() { return cnt; } };

int A::cnt = 0;

class B { private: A a; int v; public: B(int _x, int _v) :a(_x) { v = _v; } int getX() const { return a.get(); } int getV() const { return v; } void setX(int _x) { a.set(_x); } void setV(int _v) { v = _v; } };

注释:

  1. A类的构造函数中不能在初始化列表中初始化静态变量cnt,应该在类外初始化。
  2. A类的count函数是静态函数,不能有const修饰。
  3. B类的构造函数应该通过成员初始化列表初始化a对象。
  4. B类的getX函数应该调用a类的get函数。
  5. B类的setX函数应该调用a类的set函数,且不应该有const修饰。
  6. B类的setV函数不应该有const修饰
1 下面类定义中有6个语法错误请指出并改正:class A private static int cnt; int x;public Aint _xcnt0 x = _x; cnt++; Aconst A& a xax cnt++; ~A cnt--; int get const return x; void setint _x x =

原文地址: https://www.cveoy.top/t/topic/fD8q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录