成员对象问题

[复制链接]
 楼主| sinadz 发表于 2012-7-20 22:28 | 显示全部楼层 |阅读模式
// class2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<string.h>
#include <iostream>
using namespace std;

class CPoint
{
public:
CPoint (int x,int y){
nPosX =x;nPosY = y;
}
void ShowPos(){
cout<<"当前位置:x="<<nPosX<<",y="<<nPosY<<endl;
}
private:
int nPosX;int nPosY;
};
class CSize{
public:
CSize(int l, int w)
{

nLength = 1;nWidth = w;
}
void ShowSize(){
cout<<"当前大小:l="<<nLength<<",w = "<< nWidth<<endl;
}
private:
int nLength ,nWidth;
};
class CRect
{
public:
CRect (int left,int top, int right,int bottom );
void Show(){
ptCenter.ShowPos();
size.ShowSize();
}
private:
CPoint ptCenter;
CSize size;
};
CRect ::CRect (int left,int top,int right,int bottom)
:size(right -left,bottom -top),ptCenter((left+right)/2,(top+bottom)/2)
{}
int _tmain(int argc, _TCHAR* argv[])
{
CRect rc(10,100,80,250);
rc.Show();
return 0;
}

在这段代码中void Show(){
ptCenter.ShowPos();
size.ShowSize();
}的ptCenter怎么知道是属于哪一个类的??只有一个调用类的成员函数就知道是该类的对象了么??
如果有关成员函数的详细资料的就更好了!!
火箭球迷 发表于 2012-7-20 22:48 | 显示全部楼层
ptCenter不是CPoint 类型么,那么你调用了ShowPos不就是调用该类的ShowPos函数么?
yybj 发表于 2012-7-20 22:57 | 显示全部楼层
CRect rc(10,100,80,250);
在你声明CRect 类的对象的时候,在对象内部就已经申请了两个类变量:
private:
CPoint ptCenter;
CSize size;
所以程序才能正常运行.
秋天落叶 发表于 2012-7-20 23:11 | 显示全部楼层
LZ自己找本C++的书再学习一下
jangyouhua 发表于 2012-7-23 09:40 | 显示全部楼层
好奇怪的问题!好像对c++一无所知....如果代码是自已写的,怀疑作者的表达能力!:lol
您需要登录后才可以回帖 登录 | 注册

本版积分规则

304

主题

2313

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部