class成员函数的案例

1  立方体相关比较

#include <iostream>
using namespace std;


class cube
{
public:
	void setl(int l)
	{
		m_l = l;
	}
	int showl()
	{
		return m_l;
	}
	void setw(int w)
	{
		m_w = w;
	}
	int showw()
	{
		return m_w;
	}
	void seth(int h)
	{
		m_h = h;
	}
	int showh()
	{
		return m_h;
	}
	//获取立方体的面积
	int calculateS()
	{
		return 2 * (m_l * m_w + m_l * m_h + m_w * m_h);

	}
	//获取立方体的体积
	int calculateV()
	{

		return m_l * m_w * m_h;
	}
	//利用成员函数来进行的判断
	bool issamebyclass(cube &c)
	{
		if (m_l == c.showl() && m_h == c.showh() && m_w == c.showw())
		{
			return true;
		}
		return false;
	}
	
private:
	int m_l;
	int m_w;
	int m_h;
};
//利用全局函数来进行的判断
bool issame(cube&c1, cube&c2)
{
	if (c1.showl() == c2.showl() && c1.showh() == c2.showh() && c1.showw() == c2.showw())
	{
		return true;
	}
	return false;
}



int main()
{

	cube c1;
	c1.setl(10);
	c1.setw(10);
	c1.seth(10);
	cout << c1.calculateS() << endl;
	cout << c1.calculateV() << endl;
	cube c2;
	c2.setl(10);
	c2.setw(10);
	c2.seth(10);
	//利用全局函数来进行的判断
	bool ret = issame(c1, c2);
	if (ret)
	{
		cout << "相等" << endl;
	}
	else
	{
		cout << "不等" << endl;
	}
	//利用成员函数进行的判断
	ret = c1.issamebyclass(c2);
	if (ret)
	{
		cout << "成员相等" << endl;
	}
	else
	{
		cout << "成员不等" << endl;
	}
	system("pause");
	return 0;
}

2  点和圆的关系

设计一个圆形类(circle),和一个点类(point),计算点和圆的关系。

#include <iostream>
using namespace std;

class point
{
public:
	//设置x
	void setx(int x)
	{
		center_x = x;
	}
	//显示x
	int showx()
	{
		return center_x;
	}
	// 设置y
	void sety(int y)
	{
		center_y = y;
	}
	//显示y
	int showy()
	{
		return center_y;
	}

private:
	int center_x;
	int center_y;
};

class circle
{
public:
	//设置半径及获取
	void setr(int r)
	{
		center_r = r;
	}
	int showr()
	{
		return center_r;
	}

	//设置圆心及获取
	void setcenter(point center)
	{
		m_center = center;
	}
	point showcenter()
	{
		return m_center;
	}
private:
	int center_r;
	//在类中可以让另外一个类,作为本来中的成员 
	point m_center;
};
//判断点和圆的关系
void iscirecle(circle& c, point& p)//用&的原因是相比普通定义,占内存更小
{
	//计算两点之间的距离平方
	int distance =
		(c.showcenter().showx() - p.showx()) * (c.showcenter().showx() - p.showx()) +
		(c.showcenter().showy() - p.showy()) * (c.showcenter().showy() - p.showy());

		//计算半径的平方
		int rdistance = c.showr()* c.showr();
	//比较
		if (distance == rdistance)
		{
			cout << "在圆上" << endl;
		}
		else if (distance > rdistance)
		{
			cout << "在圆外" << endl;
		}
		else {
			cout << "在圆内" << endl;
		}
}


int main()
{
	
	
	//创建圆
	point center1;
	center1.setx(10);
	center1.sety(0);
	circle c1;
	c1.setr(10);
	c1.setcenter(center1);

	//创建点
	point p;
	p.setx(10);
	p.sety(10);
	//判断
	iscirecle(c1, p);

	system("pause");
	return 0;
}

3  点和圆拆开分写头文件

3.1 point分开写

3.1.1 point.h

#pragma once//防止头文件重复包含
#include <iostream>
using namespace std;
//类设计的时候一般只需要成员函数的声明即可,把实现{..}全部删掉
class point
{
public:
	//设置x
	void setx(int x);
	
	//显示x
	int showx();
	
	// 设置y
	void sety(int y);
	
	//显示y
	int showy();
	

private:
	int center_x;
	int center_y;
};

3.1.2 point.cpp

#include"point.h"
//只留函数实现即可,其他全部删掉
//此处的{center_x = x;}标红原因是计算机认为删掉之后它是全局函数,其实他是成员函数,
// 所以要在setx(int x)前面加个point::告诉计算机这是point作用域下面的一个函数
//shift+tab整体进行一个缩进
 

//设置x
void point::setx(int x)
{
	center_x = x;
}
//显示x
int point::showx()
{
	return center_x;
}
// 设置y
void point::sety(int y)
{
	center_y = y;
}
//显示y
int point::showy()
{
	return center_y;
}

3.2 circle 拆开写

3.2.1 circle.h

#pragma once
#include <iostream>
using namespace std;
//此时操作后发现所有point都报红,因为这里circle使用了另外一个point的类,所以需要加入#include"point.h"
#include"point.h"
class circle
{
public:
	//设置半径及获取
	void setr(int r);
	
	int showr();
	

	//设置圆心及获取
	void setcenter(point center);
	point showcenter();
	
private:
	int center_r;
	//在类中可以让另外一个类,作为本来中的成员 
	point m_center;
};

3.2.2 circle.cpp

#include"circle.h"

	//设置半径及获取
	void circle::setr(int r)
	{
		center_r = r;
	}
	int circle::showr()
	{
		return center_r;
	}

	//设置圆心及获取
	void circle::setcenter(point center)
	{
		m_center = center;
	}
	point circle::showcenter()
	{
		return m_center;
	}

3.3  mian函数

#include <iostream>
using namespace std;
#include"circle.h"
#include"point.h"

//判断点和圆的关系
void iscirecle(circle& c, point& p)//用&的原因是相比普通定义,占内存更小
{
	//计算两点之间的距离平方
	int distance =
		(c.showcenter().showx() - p.showx()) * (c.showcenter().showx() - p.showx()) +
		(c.showcenter().showy() - p.showy()) * (c.showcenter().showy() - p.showy());

		//计算半径的平方
		int rdistance = c.showr()* c.showr();
	//比较
		if (distance == rdistance)
		{
			cout << "在圆上" << endl;
		}
		else if (distance > rdistance)
		{
			cout << "在圆外" << endl;
		}
		else {
			cout << "在圆内" << endl;
		}
}


int main()
{
	
	
	//创建圆
	point center1;
	center1.setx(10);
	center1.sety(0);
	circle c1;
	c1.setr(10);
	c1.setcenter(center1);

	//创建点
	point p;
	p.setx(10);
	p.sety(10);
	//判断
	iscirecle(c1, p);

	system("pause");
	return 0;
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值