- 帖子
- 164
- 积分
- 164
- 经验
- 164 点
- 威望
- 0 点
- 金钱
- 9 NG
- 魅力
- 42
- 性别
- 男
- 注册时间
- 2003-9-4
我们约会吧!
|
15#
发表于 2003-2-22 23:29
| 只看该作者
Re:【讨论】编程
thermo真厉害,问题非常深入,有些问题确实出乎意外。我试了一下,那样得到的age是一个不定的值,也就是说the_hansome_home无法取到age的值。
我按你的意思在程序中加了age,如下:
#include <iostream.h>
class boy//男人类
{
public://他敞开胸怀
int age;
void LoveYou(float result)
{
cout << "Preety, I love you, marry me! My age is:" << age << endl;
}
void KissYou(float result)
{
cout << "Pretty, you are too sexy, I want to kiss you...my age is:" << age << endl;
}
};
class girl : public boy//上帝说女人是……
{
public://她也敞开胸怀……我什么都没看见:)
int age;
void LoveYou(int result)
{
cout << "I love you too...But I am too shy to say...My age is:" << age << endl;
}
void KissYou(float result)
{
cout << "You do what you want...My age is:" << age << endl;
}
};
void main(void)
{
girl pretty;//一个女孩叫pretty
boy handsome;//一个男孩叫handsome
pretty.age=25;
handsome.age=21;
girl *the_pretty_home = &pretty;//女孩说:我不要离开家……不嫁给你(真的?)!
boy *the_handsome_home = &pretty;//男孩说:来我家吧,我爱你!
boy *the_handsome_Boyhome = &handsome;
////////////////////////////////////////
//----------?????他们在干什么呀?请写出结果-------------
the_handsome_home->LoveYou(52.1f);
the_pretty_home ->LoveYou(52.1f);
the_handsome_home->KissYou(52.1f);
the_pretty_home ->KissYou(52.1f);
the_handsome_Boyhome->LoveYou(52.1f);
the_handsome_Boyhome->KissYou(52.1f);
////////////////////////////////////////
}
输出结果是:
Preety, I love you, marry me! My age is:4219448
I love you too...But I am too shy to say...My age is:25
Pretty, you are too sexy, I want to kiss you...my age is:4219448
You do what you want...My age is:25
Preety, I love you, marry me! My age is:21
Pretty, you are too sexy, I want to kiss you...my age is:21
说明the_handsome_home根本就得不到任何类的age值。the_handsome_home只能访问被隐藏的成员函数,而不能访问到变量。 |
|