目录
基本知识
例题1:正方体
完整代码:
不足:
例题2:点与圆
要点:
完整代码:
头文件point.h
源文件
基本知识
class 类名{ 访问权限:属性(参数)/行为(函数)};
注:类中的属性和行为统称为成员
属性 也称:成员属性 成员变量
行为 也称:成员函数 成员方法
例题1:正方体
设置正方体类,并分别使用全局函数和类内函数判断两个立方体是否相同
完整代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class cube
{
public:
void setLength(int l)
{
m_l = l;
}
int getLength()
{
return m_l;
}
void setWidth(int w)
{
m_w = w;
}
int getWidth()
{
return m_w;
}
void setHeight(int h)
{
m_h = h;
}
int getHeight()
{
return m_h;
}
int calculateSquare()
{
return 2 * m_h * m_l + 2 * m_h * m_w + 2 * m_l * m_w;
}
int calculateVolume()
{
return m_h * m_l * m_w;
}
//利用类内函数判断是否相同
bool isSamebyClass(cube& c)
{
if (c.getHeight() == m_h && c.getWidth() == m_w && c.getLength() == m_l)
return true;
else
return false;
}
private:
int m_l;
int m_w;
int m_h;
};
//利用全局函数判断是否相同
bool isSame(cube& c1, cube& c2)
{
if (c1.getHeight() == c2.getHeight() && c1.getWidth() == c2.getWidth() && c1.getLength() == c2.getLength())
return true;
else
return false;
}
int main()
{
cube c1;
c1.setLength(10);
c1.setWidth(11);
c1.setHeight(10);
cout << "Square is " << c1.calculateSquare() << endl;
cout << "Volume is " << c1.calculateVolume() << endl;
cube c2;
c2.setLength(10);
c2.setWidth(10);
c2.setHeight(10);
bool result1 = isSame(c1, c2);
if (result1 == 1)
cout << "相同" << endl;
else
cout << "不同" << endl;
bool result2 = c1.isSamebyClass(c2);
if (result2 == 1)
cout << "相同" << endl;
else
cout << "不同" << endl;
system("pause");
return 0;
}
不足:
判断方式不完善,长宽高可能长度相同,但名称不同,导致最后判断结果不同。优化很简单,但是懒得敲了。
例题2:点与圆
创建点类和圆类,并判断点与圆的位置关系
要点:
1.一个类可以作为另一个类的属性
本题中就将point类作为circle类pirvate中的一个属性
class circle
{
public:
void setR(int r);
int getR();
void setCenter(point center);
point getCenter();
private:
int m_r;
point m_center;
};
2.类的声明
在头文件中
只保留属性 和 函数的声明(舍弃函数的实现,并补齐;)
在源文件中
要引用头文件#include“头文件名.h”
只保留类中的函数,其余本部分不要
要在函数名之前加上 类名:: 以声明该函数所属的类 e.g. void point::setx(int x)
完整代码:
头文件point.h
#pragma once
#include<iostream>
using namespace std;
//点类
class point
{
public:
void setx(int x);
int getx();
void sety(int y);
int gety();
private:
int m_x;
int m_y;
};
//圆类
class circle
{
public:
void setR(int r);
int getR();
void setCenter(point center);
point getCenter();
private:
int m_r;
point m_center;
};
源文件
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"point.h"
//点类
void point::setx(int x)
{
m_x = x;
}
int point::getx()
{
return m_x;
}
void point::sety(int y)
{
m_y = y;
}
int point::gety()
{
return m_y;
}
//圆类
void circle::setR(int r)
{
m_r = r;
}
int circle::getR()
{
return m_r;
}
void circle::setCenter(point center)
{
m_center = center;
}
point circle::getCenter()
{
return m_center;
}
//判断点和圆的关系
void isinCircle(point p, circle c)
{
//两点之间的距离
int distance = ((c.getCenter().getx() - p.getx())*( c.getCenter().getx() - p.getx()))+ ((c.getCenter().gety()- p.gety()) * (c.getCenter().gety() - p.gety()));
//半径的平方
int r_distance = c.getR() * c.getR();
if (distance == r_distance)
{
cout << "圆上" << endl;
}
else if (distance > r_distance)
{
cout << "圆外" << endl;
}
else if(distance < r_distance)
{
cout << "圆内" << endl;
}
}
int main()
{
point p;
p.setx(11);
p.sety(10);
circle c;
c.setR(10);
point center;
center.setx(0);
center.sety(0);
c.setCenter(center);
isinCircle(p,c);
system("pause");
return 0;
}