在这里插入代码片:
#include<iostream>
using namespace std;
#include <string>
class Cube
{
private:
int m_l;
int m_w;
int m_h;
public:
void setL(int L)
{
m_l=L;
}
int getL()
{
return m_l;
}
void setH(int H)
{
m_h=H;
}
int getH()
{
return m_h;
}
void setW(int W)
{
m_w=W;
}
int getW()
{
return m_w;
}
public:
int calculateS()
{
return 2*m_l*m_w+2*m_w*m_h+2*m_l*m_h;
}
int calculateV()
{
return m_l*m_w*m_h;
}
};
bool isSame(Cube &c1,Cube &c2)
{
if(c1.calculateS() == c2.calculateS()&&c1.calculateV()==c2.calculateV())
{
return true;
}
else
{
return false;
}
}
void main()
{
Cube c1;
c1.setL(10);
c1.setH(10);
c1.setW(10);
cout << "C1的面积:" << c1.calculateS() << endl;
cout << "C1的体积:" << c1.calculateV() << endl;
Cube c2;
c2.setL(10);
c2.setH(10);
c2.setW(10);
cout << "C2的面积:" << c2.calculateS() << endl;
cout << "C2的体积:" << c2.calculateV() << endl;
bool ret = isSame(c1,c2);
if(ret==1)
{
cout <<"c1和c2是相等的"<< endl;
}
else
{
cout <<"c1和c2是不相等的"<< endl;
}
}
2.案例二
在这里插入代码片:
#include<iostream>
using namespace std;
#include <string>
class Point
{
private:
int m_x;
int m_y;
public:
void setx(int x)
{
m_x = x;
}
int getx()
{
return m_x;
}
void sety(int y)
{
m_y = y;
}
int gety()
{
return m_y;
}
};
class Circle
{
private:
int m_r;
Point m_Center;
public:
void setr(int r)
{
m_r = r;
}
int getr()
{
return m_r;
}
void setCenter(Point Center)
{
m_Center = Center;
}
Point getCenter()
{
return m_Center;
}
};
void isInCircle(Circle &c ,Point &p )
{
int distance = (c.getCenter().getx() - p.getx())*(c.getCenter().getx()-p.getx())+
(c.getCenter().gety() - p.gety())*(c.getCenter().gety()-p.gety());
int rdistance= c.getr()*c.getr();
if(distance == rdistance )
{
cout << "点在圆上" << endl;
}
else if(distance < rdistance)
{
cout << "点在圆外" << endl;
}
else
{
cout << "点在圆内" << endl;
}
}
void main()
{
Circle c;
c.setr(10);
Point center;
center.setx(10);
center.sety(0);
c.setCenter(center);
Point p;
p.setx(10);
p.sety(10);
isInCircle(c ,p );
}
|