3 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
之所以要提供沒有參數(shù)的構(gòu)造函數(shù)或給參數(shù)賦予初始值,是因?yàn)槎x或動(dòng)態(tài)分配對(duì)象數(shù)組時(shí)需要調(diào)用一個(gè)沒有參數(shù)的構(gòu)造函數(shù),由于CShape類內(nèi)部給出了一個(gè)帶參數(shù)的構(gòu)造函數(shù),因而編譯器不會(huì)自動(dòng)給類生成一個(gè)默認(rèn)構(gòu)造函數(shù),所以創(chuàng)建對(duì)象數(shù)組時(shí)會(huì)出錯(cuò)。給參數(shù)賦予初值的意思是,如果某個(gè)參數(shù)沒有提供值將會(huì)使用默認(rèn)值,否則使用實(shí)參傳遞過(guò)來(lái)的值。

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
no appropriate default constructor available
是沒有默認(rèn)的構(gòu)造函數(shù),不是構(gòu)造參數(shù)
CShape(){}
可以連帶,繼承的屬性不能設(shè)置成保護(hù)
public:
int location;
#include <stdio.h>
#include <iostream>
using namespace std;
class CShape
{
public:
CShape() {}
CShape(int pre_location) { location=pre_location; }
~CShape() {}
virtual void display() =0;
char randomchar();
public:
int location;
};
class CTriangle: public CShape
{
public:
CTriangle(int pre_height, int pre_location): CShape(pre_location) { height=pre_height; }
~CTriangle() {}
virtual void display()=0;
private:
int height;
};
class CReserveTriangle: public CTriangle
{
public:
CReserveTriangle(int pre_height, int pre_location): CTriangle(pre_height, pre_location) {}
~CReserveTriangle() {}
virtual void display();
protected:
int height;
};
void CReserveTriangle::display()
{
}
void main( )
{
CReserveTriangle crt(1,2);
cout<< crt.location<<endl;
}

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊
class CShape
{
public:
CShape(int pre_location) { location=pre_location; }
CShape(){}/////////////////////////////add this line
~CShape() {}
virtual void display() =0;
char randomchar();
protected:
int location;
};
添加回答
舉報(bào)