注册 登录
编程论坛 C++教室

[求助]有个c++题目

xuyuke 发布于 2007-04-14 21:04, 531 次点击

1.编写一个父类DADY,有一个私有整型变量 GIRL,赋值函数SETCHILD和读函数GETCHILD对GIRL进行操作,有一带参数的构造函数对GIRL初始化。
编写一个子类 CHILD继承父类DADY,有一个私有整型变量 BOY,重写赋值函数SETCHILD和读函数GETCHILD对BOY进行操作,有一带参数的构造函数对BOY初始化,并同时调用父类函数初始化父类的私有变量。
在主函数MAIN中用DADY和CHILD定义一个对象,分别调用对象的赋值函数进行赋值,再用读函数显示设置的值。
#include<iostream.h>
class DADY
{
public:
DADY(){}
~DADY(){}
DADY(int a){GIRL=a;}
void SETCHILD(int a){GIRL=a;}
int GETCHILD() const {return GIRL;}
private:
int GIRL;
};
class CHILD : public DADY
{
public:
CHILD(){}
~CHILD(){}
CHILD(int a){BOY=a;DADY(b);}
void SETCHILD(int a){BOY=a;}
int GETCHILD() const {return BOY;}
private:
int BOY;
};
void main()
{
DADY b;
CHILD d;
b.SETCHILD(30);
d.SETCHILD(40);
cout <<b.GETCHILD()<<endl;
cout<<d.GETCHILD()<<endl;

}
我做的,不知道对不对,那位大哥帮我看看啊

[此贴子已经被作者于2007-4-14 21:43:09编辑过]

9 回复
#2
xuyuke2007-04-15 09:04
怎么没人帮忙我啊?
#3
踏魔狼2007-04-15 09:31
有一带参数的构造函数对BOY初始化,并同时调用父类函数初始化父类的私有变量。
CHILD(int a) : DADY(a){BOY=a;}
#4
菜鸟上路2007-04-15 09:55
以下是引用xuyuke在2007-4-14 21:04:32的发言:
#include<iostream.h>
class DADY
{
public:
DADY(){}
~DADY(){}
DADY(int a){GIRL=a;}
void SETCHILD(int a){GIRL=a;}
int GETCHILD() const {return GIRL;}
private:
int GIRL;
};
class CHILD : public DADY
{
public:
CHILD(){}
~CHILD(){}
CHILD(int a,int b){BOY=a;DADY(b);}
void SETCHILD(int a){BOY=a;}
int GETCHILD() const {return BOY;}
private:
int BOY;
};
void main()
{
DADY b;
CHILD d;
b.SETCHILD(30);
d.SETCHILD(40);
cout <<b.GETCHILD()<<endl;
cout<<d.GETCHILD()<<endl;

}


#5
song42007-04-15 10:15
CHILD(int a){BOY=a;DADY(b);}
这个B你要是什么都没有写
他怎么知道这个B是什么
#6
condor8882007-04-16 18:55

#include<iostream>
using namespace std;
class DADY
{
private :
int GIRL;
public:
DADY()
{
}
~DADY()
{
}
DADY(int a)
{
GIRL=a;
};
void SETCHILD(int a)
{
GIRL=a;
};
int GETCHILD() const
{
return GIRL;
};
};
class CHILD : public DADY
{
private:
int BOY;
public:
CHILD()
{
}
~CHILD()
{
}
CHILD(int a)
{
BOY=a;
DADY(b);
}
void SETCHILD(int a)
{
BOY=a;
}
int GETCHILD () const
{
return BOY;
}
};

int main()
{
DADY b;
CHILD d;
b.SETCHILD(30);
d.SETCHILD(40);
cout<<b.SETCHILD()<<endl;
cout<<d.SETCHILD()<<endl;
return 0;

}
不过还有错,
C:\1\1.cpp(61) : error C2660: 'SETCHILD' : function does not take 0 parameters
C:\1\1.cpp(62) : error C2660: 'SETCHILD' : function does not take 0 parameters

#7
condor8882007-04-16 18:56
那位高手在来调试下啊,在线等。。。。。。。。。
#8
condor8882007-04-16 19:01
CHILD d;
b.SETCHILD(30);
d.SETCHILD(40);
还有d哪有啊,这是什么意思
#9
condor8882007-04-16 19:08

不好意思,我打错了,是GETCHILD,而不是SETCHILD,解决了。

#10
condor8882007-04-16 19:11

完整的如下:
#include<iostream>
using namespace std;
class DADY
{
private :
int GIRL;
public:
DADY()
{
}
~DADY()
{
}
DADY(int a)
{
GIRL=a;
};
void SETCHILD(int a)
{
GIRL=a;
};
int GETCHILD() const
{
return GIRL;
};
};
class CHILD : public DADY
{
private:
int BOY;
public:
CHILD()
{
}
~CHILD()
{
}
CHILD(int a)
{
BOY=a;
DADY(b);
}
void SETCHILD(int a)
{
BOY=a;
}
int GETCHILD () const
{
return BOY;
}
};
int main()
{
DADY b;
CHILD d;
b.SETCHILD(30);
d.SETCHILD(40);
cout<<b.GETCHILD()<<endl;
cout<<d.GETCHILD()<<endl;
return 0;

}

1