2 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
拷貝構(gòu)造函數(shù)編譯可以自己生成 你可以嘗試用對(duì)象名作為參數(shù)來(lái)調(diào)用

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊
當(dāng)你在類中自定義了帶參數(shù)的構(gòu)造函數(shù),就構(gòu)成了重載,系統(tǒng)不會(huì)調(diào)用默認(rèn)的構(gòu)造函數(shù),所以要自己定義默認(rèn)構(gòu)造函數(shù)。除此之外,最后的拷貝構(gòu)造函數(shù)中strlen(c)的使用有誤。代碼修改如下:
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
using namespace std;
class circle
{
double radius;
public:
circle(){}//增加的默認(rèn)構(gòu)造函數(shù)
circle(double r)
{radius=r;}
double getarea()
{
double s;
s=radius*radius*3.1415926;
return s;
}
};
class table
{
double height;
public:
table(){}//增加的默認(rèn)構(gòu)造函數(shù)
table(double h)
{height=h;}
double getheight()
{return height;}
};
class roundtable:public circle,public table
{
char *color;
public:
roundtable(double a,double b,char *c=""):circle(a),table(b)
{
color=new char[strlen(c)];
strcpy(color,c);
}
char *getcolor()
{return color;}
~roundtable(){delete []color;}
roundtable(const roundtable&a)
{color=new char[strlen(a.color)];//此處進(jìn)行了修改
strcpy(color,a.color);};
};
int main()
{
roundtable rt(0.8,1.2,"黑色");
cout<<"圓桌屬性數(shù)據(jù):"<<endl;
cout<<"高度:"<<rt.getheight()<<"米"<<endl;
cout<<"面積:"<<rt.getarea()<<"平方米"<<endl;
cout<<"顏色:"<<rt.getcolor()<<endl;
getchar();
return 0;
}
- 2 回答
- 0 關(guān)注
- 125 瀏覽
添加回答
舉報(bào)