C++语言实现组合类实例时间:2022-08-28 09:02:41/****************C_Point.h**********************/#pragma onceclass C_Point{public: C_Point(void); ~C_Point(void); C_Point(C_Point &_point); C_Point(int xx,int yy); //设置X坐标 int SetX(int xx); //设置Y坐标 int SetY(int yy); //获取X坐标 int GetX(); //获取Y坐标 int GetY(); // 显示点坐标 void showPoin(void);private: int X;//X坐标 int Y;//Y坐标}; /********************C_Rectangles.h*********************/#pragma once#include "c_point.h"class C_Rectangles : public C_Point{public: C_Rectangles(void); C_Rectangles(C_Rectangles &_r); C_Rectangles(int Ax1,int Ay1,int Cx2,int Cy2);//用A,C点坐标构造矩形 // 获取长度 int GetLength(void); // 获取宽度 int GetWidth(void); int RectanglePerimeter(void); //返回周长 int RectangleArea(void);//返回面积 ~C_Rectangles(void); void showRectanglePoint();//打印四角坐标private: C_Point A;//从左下角开始,逆时针依次是ABCD C_Point B; C_Point C; C_Point D; int length;//这样申明有点多余 int width;}; /*******************stdafx.h*****************/// stdafx.h : 标准系统包含文件的包含文件,// 或是经常使用但不常更改的// 特定于项目的包含文件//#pragma once#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料#include <stdio.h>#include <tchar.h>#include <math.h>#include <iostream>using namespace std;// TODO: 在此处引用程序需要的其他头文件 /**********************C_Point.cpp**************************/#include "StdAfx.h"#include "C_Point.h"C_Point::C_Point(void){ this->X = 0; this->Y = 0;}C_Point::~C_Point(void){}// 显示点坐标void C_Point::showPoin(void){ cout<<"("<<X<<","<<Y<<")"<<endl;}C_Point::C_Point(C_Point &_point){ this->X = _point.X; this->Y = _point.Y;}C_Point::C_Point(int xx,int yy){ this->X = xx; this->Y = yy;}int C_Point::SetX(int xx){ this->X = xx; return 0;}int C_Point::SetY(int yy){ this->Y = yy; return 0;}int C_Point::GetX(){ return this->X;}int C_Point::GetY(){ return this->Y;} /****************C_Rectangles.cpp*********************/#include "StdAfx.h"#include "C_Rectangles.h"C_Rectangles::C_Rectangles(void){ this->A.SetX(0); this->A.SetY(0); this->B.SetX(0); this->B.SetY(0); this->C.SetX(0); this->C.SetY(0); this->D.SetX(0); this->D.SetY(0); this->length = 0; this->width = 0;}C_Rectangles::~C_Rectangles(void){}C_Rectangles::C_Rectangles(C_Rectangles &_r){ //拷贝构造函数,用_r初始化新对象 cout<<"C_Rectangles拷贝构造函数被调用"<<endl; this->A.SetX( _r.A.GetX() ); this->A.SetY( _r.A.GetY() ); this->B.SetX( _r.B.GetX() ); this->B.SetY( _r.B.GetY() ); this->C.SetX( _r.C.GetX() ); this->C.SetY( _r.C.GetY() ); this->D.SetX( _r.D.GetX() ); this->D.SetY( _r.D.GetY() ); this->length = _r.GetLength(); this->width = _r.GetWidth();}C_Rectangles::C_Rectangles(int Ax1,int Ay1,int Cx2,int Cy2){ //根据约定:从左下角开始,逆时针依次是ABCD。 //则 //A和B有相同的Y坐标,A和D有相同的X坐标 //C和B有相同的X坐标,C和D有相同的Y坐标 this->A.SetX(Ax1); this->A.SetY(Ay1); this->B.SetX(Cx2); this->B.SetY(Ay1); this->C.SetX(Cx2); this->C.SetY(Cy2); this->D.SetX(Ax1); this->D.SetY(Cy2); this->length = abs(A.GetX() - C.GetX()); this->width = abs(C.GetY() - A.GetY());}void C_Rectangles::showRectanglePoint(){ //输出各点坐标 cout<<"A="; this->A.showPoin(); cout<<"B="; this->B.showPoin(); cout<<"C="; this->C.showPoin(); cout<<"D="; this->D.showPoin(); cout<<"length= "<<length<<endl; cout<<"width = "<<width<<endl;}//返回周长int C_Rectangles::RectanglePerimeter(void){ int _Perimeter = 0; //周长计算公式。 _Perimeter = ( length + width )*2; return _Perimeter;}//返回面积int C_Rectangles::RectangleArea(void){ return length*width;}// //获取长度int C_Rectangles::GetLength(void){ return length;}// //获取宽度int C_Rectangles::GetWidth(void){ return width;} /*************************************Rectangle.cpp*******/// Rectangle.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "C_Point.h"#include "C_Rectangles.h"void _wait_any_key();int _tmain(int argc, _TCHAR* argv[]){ C_Point temp=C_Point(2,3); C_Point a(temp);//拷贝构造函数测试 temp.showPoin();//显示 a.showPoin();//显示 C_Rectangles Rtemp = C_Rectangles(1,1,2,2);//带形参初始化 Rtemp.showRectanglePoint();//显示 C_Rectangles R1(Rtemp);//拷贝构造函数测试 R1.showRectanglePoint();//显示 cout <<"周长=" << R1.RectanglePerimeter()<<endl;//周长计算测试 cout <<"面积=" << R1.RectangleArea()<<endl;//面积计算测试 _wait_any_key(); return 0;}void _wait_any_key(){ fflush(stdin); cout<<"Press any key to continue...."<<endl; getchar();} // stdafx.cpp : 只包括标准包含文件的源文件// Rectangle.pch 将作为预编译头// stdafx.obj 将包含预编译类型信息#include "stdafx.h"// TODO: 在 STDAFX.H 中// 引用任何所需的附加头文件,而不是在此文件中引用