一个入者者的问题

programmer2003 2003-05-07 11:16:31
小弟刚学继承写了这样一个程序:
#include<conio.h>
#include<iostream.h>
class Man
{
public:
Man(int age,int height,int weight);
~Man(){cout<<"Men destructor..."<<endl;}
int GetAge(void)const{return itsage;}
int GetHeight(void)const{return itsheight;}
int GetWeight(void)const{return itsweight;}
void SetAge(int age){itsage=age;}
void SetHeight(int height){itsheight=height;}
void SetWeight(int weight){itsweight=weight;}
protected:
int itsage;
int itsheight;
int itsweight;
};
Man::Man(int age,int height,int weight):itsage(age),itsheight(height),itsweight(weight)
{
cout<<"man destructor..."<<endl;
}

class Female:public Man
{
public:
Female(int age,int height,int weight);
~Female(){cout<<"Men destructor..."<<endl;}
int GetAge(void)const{return itsage+2;}
int GetHeight(void)const{return itsheight+2;}
int GetWeight(void)const{return itsweight+2;}
void SetAge(int age){itsage=age;}
void SetHeight(int height){itsheight=height;}
void SetWeight(int weight){itsweight=weight;}
protected:
int itsage;
int itsheight;
int itsweight;
};
Female::Female(int age,int height,int weight):itsage(age),itsheight(height),itsweight(weight)
{
cout<<"Male construcot..."<<endl;
}

class Male:public Man
{
public:
Male(int age,int height,int weight);
~Male(){cout<<"Men destructor..."<<endl;}
int GetAge(void)const{return itsage+1;}
int GetHeight(void)const{return itsheight+1;}
int GetWeight(void)const{return itsweight+1;}
void SetAge(int age){itsage=age;}
void SetHeight(int height){itsheight=height;}
void SetWeight(int weight){itsweight=weight;}
protected:
int itsage;
int itsheight;
int itsweight;
};
Male::Male(int age,int height,int weight):itsage(age),itsheight(height),itsweight(weight)
{
cout<<"Male construcot..."<<endl;
}


void main()
{
void clrscr();
Man *p1=new Man(20,40,80);
cout<<p1->GetAge()<<"\t"<<p1->GetHeight()<<"\t"<<p1->GetWeight()<<endl;
delete p1;
Female *p2=new Female(2,4,8);
cout<<p2->GetAge()<<"\t"<<p2->GetHeight()<<"\t"<<p2->GetWeight()<<endl;
delete p2;
Male *p3=new Male(200,400,800);
cout<<p3->GetAge()<<"\t"<<p3->GetHeight()<<"\t"<<p3->GetWeight()<<endl;
delete p3;
}


怎么有这样两个提示呀,搞不懂,能给我说明么

cpp(41) : error C2512: 'Man' : no appropriate default constructor available
...全文
19 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lifanxi 2003-05-07
  • 打赏
  • 举报
回复
Ohoh,不好意思,看错了,您没想练多态,我一看到指针就以为你在练多态了,没想到你没用基类指针去指派生类,没有多态的话就不用虚函数了。把我的程序中的virtual都去掉好了。
lifanxi 2003-05-07
  • 打赏
  • 举报
回复
另外,你既然从man派生了male和female,那这两个派生类就不必要有age,height,weight这些成员变量了。
还有,你的程序可以看出,想练练多态的运用,所以,基类的部分函数应该是做成虚函数的。
我修改后的程序如下:
#include<conio.h>
#include<iostream.h>
class Man
{
public:
Man(int age,int height,int weight);
virtual ~Man(){cout<<"Men destructor..."<<endl;}
virtual int GetAge(void)const{return itsage;}
virtual int GetHeight(void)const{return itsheight;}
virtual int GetWeight(void)const{return itsweight;}
void SetAge(int age){itsage=age;}
void SetHeight(int height){itsheight=height;}
void SetWeight(int weight){itsweight=weight;}
protected:
int itsage;
int itsheight;
int itsweight;
};
Man::Man(int age,int height,int weight):itsage(age),itsheight(height),itsweight(weight)
{
cout<<"man constructor..."<<endl;
}

class Female:public Man
{
public:
Female(int age,int height,int weight);
~Female(){cout<<"Female destructor..."<<endl;}
int GetAge(void)const{return itsage+2;}
int GetHeight(void)const{return itsheight+2;}
int GetWeight(void)const{return itsweight+2;}
};
Female::Female(int age,int height,int weight):Man(age,height,weight)
{
cout<<"FeMale construcot..."<<endl;
}

class Male:public Man
{
public:
Male(int age,int height,int weight);
~Male(){cout<<"Male destructor..."<<endl;}
int GetAge(void)const{return itsage+1;}
int GetHeight(void)const{return itsheight+1;}
int GetWeight(void)const{return itsweight+1;}
};
Male::Male(int age,int height,int weight):Man(age,height,weight)
{
cout<<"Male constructor..."<<endl;
}


void main()
{
Man *p1=new Man(20,40,80);
cout<<p1->GetAge()<<"\t"<<p1->GetHeight()<<"\t"<<p1->GetWeight()<<endl;
delete p1;
Female *p2=new Female(2,4,8);
cout<<p2->GetAge()<<"\t"<<p2->GetHeight()<<"\t"<<p2->GetWeight()<<endl;
delete p2;
Male *p3=new Male(200,400,800);
cout<<p3->GetAge()<<"\t"<<p3->GetHeight()<<"\t"<<p3->GetWeight()<<endl;
delete p3;
}
lifanxi 2003-05-07
  • 打赏
  • 举报
回复
你的基类没有默认构造函数,请为你的man类加上man()这个默认构造函数。
或者就是在派生类中要显示的调man的非默认构造函数,来初始化man对象。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧