C++:类的定义和使用示例

一、类的头文件

//DefineClass.h
//定义平面上的一个点类
using namespace std;
class Point
{
public:
	Point(double a, double b);
	double  GetX();
	double  GetY();

	~Point();
private:
	double m_x, m_y;
};


//定义圆类
class Circle
{
public:
	Circle(double cx, double cy, double cr);	//构造函数包括圆心、半径
	void DisplayCircleInfo();
	~Circle();

private:
	Point m_center;		//对象成员
	double m_radius;	//非对象成员

};

二、类的实现文件

//#ifndef DEFINECLASS
//#define DEFINECLASS
#include "stdafx.h"

#include "DefineClass.h"
#include 
using namespace std;
Point::Point(double a, double b)
{
	m_x = a;
	m_y = b;
}

Point::~Point()
{
}
double Point:: GetX()
{
	return m_x;
}
double Point::GetY()
{
	return m_y;
}

Circle::Circle(double cx, double cy, double cr)
	:m_center(cx, cy), m_radius(cr)			//:后是触发列表,创建时对对象成员m_center赋值
{
	//相当于
	//m_center.GetX = x;
	//m_center.GetX y;
	//m_radius = cr;			
}
void Circle::DisplayCircleInfo()
{
	c

你可能感兴趣的:(c++)