zhuguocheng29 2014-10-12 14:15 采纳率: 0%
浏览 1529

c++构造函数的问题,求大神赐教

#include
class A
{
public :
A()
{
a=0;
cout<<"A's default constructor called.\n";
}
A(int i)
{
a=i;
cout<<"A's constructor called.\n";
}
~A()
{
cout<<"A's destructor called.\n";
}
void print()const
{
cout<<a<<",";
}
int Geta()
{
return a;
}
private :
int a;

};

class B:public A
{
public :
B()
{
b=0;
cout<<"B's default constructor called.\n";
}
B(int i,int j,int k);
~B()
{
cout<<"B's destructor called.\n";
}
void print();
private:
int b;
A aa;
};

B::B(int i,int j,int k):A(i),aa(j)
{
b=k;
cout<<"B's constructor called.\n";
}

void B::print()
{
A::print();
cout<<b<<","<<aa.Geta()<<endl;
}

void main()
{
B bb;
bb=B(1,2,5);
bb.print();
}

主要是main函数中这条语句不理解:bb=B(1,2,5). B(1,2,5)是什么意思?不应该是bb=B b1(1,2,5)吗?

  • 写回答

2条回答 默认 最新

  • xiezongzhou123 2014-10-13 04:03
    关注

    bb=B(1,2,5)
    先调用B的构造函数,
    然后调用缺省的赋值操作符赋值给bb

    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果