小弟是初学者,现有一道作业题,小弟编好后不能运行,请大家帮忙一下看小弟的程序是出了什么错

王朝干货·作者佚名  2011-12-23
窄屏简体版  字體: |||超大  

#include<iostream.h>

class fruit

{

private:char fruitcolour,fruitsize;float fruitprice;

public:fruit(char fc,char fs,float fp)

{fruitcolour=fc;fruitsize=fs;fruitprice=fp;}

};

class trees

{

private:char leafcolour;char leafsize;float averageheight;

public:trees(char lc,char ls,float ah)

{leafcolour=lc;leafsize=ls;averageheight=ah;}

};

class apple:public fruit,public trees

{

public:apple(char fco,char fsi,float fpr,char lco,char lsi,float ahi):fruit(fco,fsi,fpr),trees(lco,lsi,ahi){}

};

void main()

{

apple (red,big,5.00,green,thick,2.00);

cout<<"the apples's colour is "<<apple::fruitcolour;

}

这个程序有五个错误,分为两类:一类是在参数类型与形参类型不符:apple类的构造函数public:apple(char fco,char fsi,float fpr,char lco,char lsi,float ahi)的第1、2、4、5个参数是char,而在main()函数中你给出的是 red, big, green, thick,系统认为它们是变量名,而又找不到它们的定义,所以出错;第二类是在cout<<"the apples's colour is "<<apple::fruitcolour中,第一用类名直接访问的只有静态属性,非静态属性只能用对象名访问,但对象能访问的非静态属性只有公有属性,所以应该把fruitcolour改成公有属性。修改结果有以下两种:

1

#include<iostream.h>

class fruit

{

private:char fruitsize;float fruitprice;

public:char fruitcolour;

fruit(char fc,char fs,float fp)

{fruitcolour=fc;fruitsize=fs;fruitprice=fp;}

};

class trees

{

private:char leafcolour;char leafsize;float averageheight;

public:trees(char lc,char ls,float ah)

{leafcolour=lc;leafsize=ls;averageheight=ah;}

};

class apple:public fruit,public trees

{

public:apple(char fco,char fsi,float fpr,char lco,char lsi,float ahi):fruit(fco,fsi,fpr),trees(lco,lsi,ahi){}

};

void main()

{

apple aa('r','b',5.00,'g','t',2.00);

cout<<"the apples's colour is "<<aa.fruitcolour;

}

2

#include<iostream.h>

#include <string.h>

class fruit

{

private:char fruitsize[10];float fruitprice;

public:char fruitcolour[10];

fruit(char *fc,char *fs,float fp)

{strcpy(fruitcolour,fc);strcpy(fruitsize,fs);fruitprice=fp;}

};

class trees

{

private:char leafcolour[10];char leafsize[10];float averageheight;

public:trees(char *lc,char *ls,float ah)

{strcpy(leafcolour,lc);strcpy(leafsize,ls);averageheight=ah;}

};

class apple:public fruit,public trees

{

public:apple(char *fco,char *fsi,float fpr,char *lco,char *lsi,float ahi):fruit(fco,fsi,fpr),trees(lco,lsi,ahi){}

};

void main()

{

apple aa("red","big",5.00,"green","thick",2.00);

cout<<"the apples's colour is "<<aa.fruitcolour;

}

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航