笑死,和宝哥同时生病了
一,封装-案例
1.0 立方体类
#include<iostream>//分别用全局函数和成员函数判定立方体是否相等
using namespace std;
class Cube
{
public:
int m_area;
int m_vol;
int geth(){return m_h;}
int getl() { return m_l; }
int getw() { return m_w; }
void setcube(int h,int l,int w)
{
m_h = h;
m_l = l;
m_w = w;
}
int getarea(int h, int l, int w)
{
int area = 2 * (h * l + h * w + l * w);
return area;
}
int getvolume(int h, int l, int w)
{
int vol = h * l * w;
return vol;
}
int ifsamehlw(Cube c1, Cube c2)
{
if ((c1.m_h + c1.m_l + c1.m_w) == (c2.m_h + c2.m_l + c2.m_w))//和相等
{
if (c1.m_h != c2.m_h || c1.m_h != c2.m_l || c1.m_h != c2.m_w)//一条边相等
{
if (c1.m_l != c2.m_h || c1.m_l != c2.m_l || c1.m_l != c2.m_w)//两条边相等
{
return 1;
}
}
}
return 0;
}
int ifsamehlwtt(Cube &c2)//
{
if ((m_h + m_l + m_w) == (c2.m_h + c2.m_l + c2.m_w))//和相等
{
if (m_h != c2.m_h || m_h != c2.m_l || m_h != c2.m_w)//一条边相等
{
if (m_l != c2.m_h || m_l != c2.m_l || m_l != c2.m_w)//两条边相等
{
return 1;
}
}
}
return 0;
}
private:
int m_h;
int m_l;
int m_w;
};
bool ifsame(Cube& c1, Cube& c2);//不能放在类的前面!显示未设定的标识符
int main()
{
Cube c1, c2;
c1.setcube(3, 5, 6);
c2.setcube(6, 5, 3);
c1.m_area = c1.getarea(c1.geth(), c1.getl(), c1.getw());
c1.m_vol= c1.getvolume(c1.geth(), c1.getl(), c1.getw());
cout << "C1面积" << c1.m_area << "\tC1体积" << c1.m_vol << endl;
c2.m_area = c2.getarea(c2.geth(), c2.getl(), c2.getw());
c2.m_vol = c2.getvolume(c2.geth(), c2.getl(), c2.getw());
cout << "C2面积" << c1.m_area << "\tC2体积" << c1.m_vol << endl;
if (c1.ifsamehlw(c1, c2))//可以用,但是有点奇怪
{
cout << "xiangdneg" << endl;
}
if (c1.ifsamehlwtt(c2))//正常了,笑死
{
cout << "xiangdneg" << endl;
}
if (ifsame(c1, c2))//int &c1=c1;变量传入参数,用引用的方式接受,看不见的赋值运算
{
cout << "xiangdneg" << endl;
}
return 0;
system("pause");
}
//bool逻辑
bool ifsame(Cube& c1, Cube& c2)//引用的方式传递,不会再拷贝一份数据了
{
if (c1.geth() + c1.getl() + c1.getw() == c2.geth() + c2.getl() + c2.getw())//和相等
{
if (c1.geth() != c2.geth() || c1.geth() != c2.getl() || c1.geth() != c2.getw())//一条边相等
{
if (c1.getl() != c2.geth() || c1.getl() != c2.getl() || c1.getl() != c2.getw())//两条边相等
{
return true;
}
}
}
return false;
}
1.1 点和⚪关系
0.1 没有嵌套
#include<iostream>//判断点和圆的位置关系
using namespace std;
class Circle
{
int c_x;
int c_y;
int c_r;
public:
void setc(int x, int y, int r)
{
c_x = x;
c_y = y;
c_r = r;
}
int getcx() { return c_x; };
int getcy() { return c_y; };
int getcr() { return c_r; };
};
class Point
{
int p_x;
int p_y;
public:
void setp(int x, int y)
{
p_x = x;
p_y = y;
}
int getpx() { return p_x; };
int getpy() { return p_y; };
};
void relaction(Circle& c, Point& p);
int main()
{
Circle c;
Point p,p1;
c.setc(0, 0, 10);
p.setp(10, 0);
p1.setp(11, 0);
relaction(c, p);
relaction(c, p1);
return 0;
system("pause");
}
void relaction(Circle &c, Point &p)
{
int distance = (c.getcx() - p.getpx()) * (c.getcx() - p.getpx()) +
(c.getcy() - p.getpy()) * (c.getcy() - p.getpy());
int rdistance = c.getcr() * c.getcr();
if (distance == rdistance)
{
cout << "点("<<p.getpx()<<","<<p.getpy()<<")在圆上" << endl;
}
else if (distance > rdistance)
{
cout << "点(" << p.getpx() << "," << p.getpy() << ")在圆外" << endl;
}
else
{
cout << "点(" << p.getpx() << "," << p.getpy() << ")在圆内" << endl;
}
}
0.2 嵌套类
#include<iostream>//判断点和圆的位置关系
using namespace std;
class Point
{
int p_x;
int p_y;
public:
void setp(int x, int y)
{
p_x = x;
p_y = y;
}
int getpx() { return p_x; };
int getpy() { return p_y; };
};
class Circle
{
Point c_center;
int c_r;
public:
void setcr( int r)
{
c_r = r;
}
void setcenter(Point ¢er)
{
c_center = center;//两个点类相等就行
}
Point getcenter()
{
return c_center;
}
/*不对—不能直接访问点C_CENTER,只能通过行为访问
int getcx() { return c_center.getpx(); };
int getcy() { return c_center.getpy(); };*/
int getcr() { return c_r; };
};
void relaction(Circle& c, Point& p);
int main()
{
Circle c;
Point p,p1,p2,center;
//c.c_center.setp(0, 0);不可访问
center.setp(0, 0);
p.setp(10, 0);
p1.setp(9, 0);
p2.setp(12, 0);
//初始化圆类
c.setcr(10);
c.setcenter(center);
relaction(c, p);
relaction(c, p1);
relaction(c, p2);
return 0;
system("pause");
}
void relaction(Circle &c, Point &p)
{
int distance = (c.getcenter().getpx() - p.getpx()) * (c.getcenter().getpx() - p.getpx()) +
(c.getcenter().getpy() - p.getpy()) * (c.getcenter().getpy() - p.getpy());
int rdistance = c.getcr() * c.getcr();
if (distance == rdistance)
{
cout << "点("<<p.getpx()<<","<<p.getpy()<<")在圆上" << endl;
}
else if (distance > rdistance)
{
cout << "点(" << p.getpx() << "," << p.getpy() << ")在圆外" << endl;
}
else
{
cout << "点(" << p.getpx() << "," << p.getpy() << ")在圆内" << endl;
}
}
0.3 嵌套且分文件
调试的时候发现,全局函数运行的时候,每到一个局部函数就会跳到类里面执行,就还蛮好玩的乐 ,今天就学这么多吧,噶
#pragma once//防止重复包含
#include <iostream>
using namespace std;
//头文件只保存声明
class Point
{
int p_x;
int p_y;
public:
void setp(int x, int y);
int getpx();
int getpy();
};
#pragma once
#include <iostream>
#include "point.h"
using namespace std;
class Circle
{
Point c_center;
int c_r;
public:
void setcr(int r);
void setcenter(Point& center);
Point getcenter();
int getcr() ;
};
#include "point.h"
//只保留行为语句,函数的实现
void Point::setp(int x, int y)//加上作用域,不加上就是全局函数
{
Point::p_x = x;
Point::p_y = y;
}
int Point::getpx() { return Point::p_x; }
int Point::getpy() { return Point::p_y; }
#include "point.h"
#include "circle.h"
void Circle::setcr(int r)
{
Circle::c_r = r;
}
void Circle::setcenter(Point& center)
{
Circle::c_center = center;//两个点类相等就行
}
Point Circle::getcenter()
{
return Circle::c_center;
}
int Circle::getcr(){ return Circle::c_r; }
#include<iostream>//判断点和圆的位置关系
using namespace std;
#include "point.h"
#include "circle.h"
void relaction(Circle& c, Point& p);
int main()
{
Circle c;
Point p,p1,p2,center;
//c.c_center.setp(0, 0);不可访问
center.setp(0, 0);
p.setp(10, 0);
p1.setp(9, 0);
p2.setp(12, 0);
//初始化圆类
c.setcr(10);
c.setcenter(center);
relaction(c, p);
relaction(c, p1);
relaction(c, p2);
return 0;
system("pause");
}
void relaction(Circle &c, Point &p)
{
int distance = (c.getcenter().getpx() - p.getpx()) * (c.getcenter().getpx() - p.getpx()) +
(c.getcenter().getpy() - p.getpy()) * (c.getcenter().getpy() - p.getpy());
int rdistance = c.getcr() * c.getcr();
if (distance == rdistance)
{
cout << "点("<<p.getpx()<<","<<p.getpy()<<")在圆上" << endl;
}
else if (distance > rdistance)
{
cout << "点(" << p.getpx() << "," << p.getpy() << ")在圆外" << endl;
}
else
{
cout << "点(" << p.getpx() << "," << p.getpy() << ")在圆内" << endl;
}
}