打印

成员对象问题

[复制链接]
889|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
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++的书再学习一下

使用特权

评论回复
5
jangyouhua| | 2012-7-23 09:40 | 只看该作者
好奇怪的问题!好像对c++一无所知....如果代码是自已写的,怀疑作者的表达能力!:lol

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

304

主题

2313

帖子

0

粉丝