C++ VS2008 基本写法
来源:互联网 发布:ubuntu 编译c 编辑:程序博客网 时间:2024/06/08 00:40
/*类成员的初始化: 求2点组成的矩形*/#include <iostream>using namespace std;class CCenterPoint{public: CCenterPoint(int posX, int posY) : mPosX(posX), mPosY(posY) { } void ShowPos() const { cout << "2点之间的中点坐标: (" << mPosX << "," << mPosY << ")" << endl; }private: int mPosX; int mPosY;};class CArea{public: CArea(int length, int width) : mLength(length), mWidth(width) { } void ShowArea() const { cout << "2点组成的矩形面积: " << mLength * mWidth << endl; }private: int mLength; int mWidth;};class CRect{public: CRect(int posX1, int posY1, int posX2, int posY2) : mPoint((posX1+posY1)/2, (posX2+posY2)/2), mArea(posX2-posX1, posY2-posY1) { } void Show() const { mPoint.ShowPos(); mArea.ShowArea(); }private: CCenterPoint mPoint; CArea mArea;};int main(){ CRect rect(10, 100, 20, 200); rect.Show(); return 0;}