l7_1.cpp

来自「《C++程序设计教程》电子教案及例题源码」· C++ 代码 · 共 69 行

CPP
69
字号
#include <iostream.h>
#include <string.h>
class CLocation
{
private:
	int x;
	int y;
public:
	int Getx();
	int Gety();
	void MoveTo(int x, int y);
	CLocation(int x=0, int y=0);
};

void CLocation::MoveTo(int x, int y)
{
	CLocation::x = x;  // 可以写成 this->x = x;
	CLocation::y = y;  // 可以写成 this->y = y;
}
int CLocation::Getx() 
{ 
	return x;
}
int CLocation::Gety()
{
	return y;
}

CLocation::CLocation(int x, int y)
{
    
	CLocation::x = x;  // 可以写成 this->x = x;
	CLocation::y = y;  // 可以写成 this->y = y;
}

class CPoint:public CLocation
{
private:
	char Color[10];
public:
	CPoint(char *c);
	void SetColor(char *c);
	void Show();
};

CPoint::CPoint(char *c)
{ 
	strcpy(Color,c);
}
void CPoint::SetColor(char *c)
{
	strcpy(Color,c);
}

void CPoint::Show()
{
	cout << Getx() << "," << Gety() << "  " << Color << endl ;
}

void main(void)
{
	CPoint p("Red");
	p.Show();
	p.MoveTo(7,8);
	p.Show();
	p.SetColor("Green");   
	p.Show();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?