⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex_classderived.cpp

📁 Visual C++应用教程-源代码 本书在复习C++基础知识后
💻 CPP
字号:
// [例Ex_ClassDerived] 派生类的构造和析构示例
#include <iostream.h>

class CPoint
{
public:
	CPoint( int x = 0, int y = 0)
	{
		xPos = x;	yPos = y;
		cout<<"CPoint构造函数"<<endl;
	}
	void ShowPos(bool isEnd = false)
	{
		cout<<"pos("<<xPos<<", "<<yPos<<")";
		if (isEnd)	cout<<endl;
	}
private:
	int xPos, yPos;
};

class CRect
{
public:
	CRect( int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0)
		: ptLT(x1, y1), ptRB(x2, y2)
	{
		cout<<"CRect构造函数"<<endl;
	}
	void ShowPos()
	{
		ptLT.ShowPos();	 cout<<", ";	 ptRB.ShowPos(true);
	}
private:
	CPoint ptLT, ptRB;
};

class CCuboid: public CRect
{
public:
	CCuboid( int x1, int y1, int x2, int y2, int height )
		: CRect(x1, y1, x2, y2),
		ptCenter((x1+x2)/2, (y1+y2)/2),
		fHeight(height)
	{
		cout<<"CCuboid构造函数"<<endl;
	}
	void ShowAll()
	{
		cout<<"矩形的角点为:";
		CRect::ShowPos();
		cout<<"底面矩形的中点为:";
		ptCenter.ShowPos(true);
		cout<<"高为:"<<fHeight<<endl;
	}
private:
	CPoint	ptCenter;
	float	fHeight;
};

int main()
{
	CCuboid one( 5, 5, 30, 30, 50);
	one.ShowAll();

	return 0;
}

⌨️ 快捷键说明

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