设计立方体类1.求出立方体的面积和体积2.分别用全局函数和成员函数判断两个立方体是否相等
#include <iostream>
//设计立方体类
//求出立方体的面积和体积
//分别用全局函数和成员函数判断两个立方体是否相等
using namespace std;
class Cube
{
public:
void setlength(int L)
{
length=L;
}//设置长
int getlength()
{
return length;
}//得到长
void setwidth(int W)
{
width=W;
}//设置宽
int getwidth()
{
return width;
}//得到宽
void setheight(int H)
{
height=H;
}//设置高
int getheight()
{
return height;
}//得到高
int S()//体积
{
return height*width*height;
}
int mianji()
{
return 2*height*width+2*width*height+2*length*height;
}
//前面说到.类里面的东西叫做成员 , 这个函数应该写在类里面
bool Same1(Cube &c)
{
if(height==c.getheight()&&length==c.getlength()&&width==c.getwidth())
{
return 1;
}
return 0;
}
private:
int length;
int width;
int height;
};
//利用全局函数 判断两个立方体是否相等
bool same(Cube &c1,Cube &c2)
{
if(c1.getheight()==c2.getheight()&&c1.getlength()==c2.getlength()&&c1.getwidth()==c2.getwidth())
{
return 1;
}
return 0;
}
int main()
{
Cube C1;//第一个C1
C1.setheight(20);
C1.setlength(10);
C1.setwidth(30);
Cube C2;//第二个
C2.setheight(20);
C2.setlength(10);
C2.setwidth(40);
bool R=same(C1,C2);//用R来接受返回的0或者1
if(R==1)
{
cout<<"c1和c2相等"<<endl;
}
cout<<"c1和c2不相等"<<endl;
bool R1=C1.Same1(C2);
if(R==1)
{
cout<<"c1和c2相等"<<endl;
}
cout<<"c1和c2不相等"<<endl;
}
2.点和圆的关系
设计一个圆形类,和一个点类,计算点和圆的关系
#include <iostream>
//点和圆的关系
//设计一个圆形类,和一个点类,计算点和圆的关系
using namespace std;
const double PI=3.14;
class point
{
public:
void setX(int X)
{
p_x=X;
}//设置X坐标
int getX()
{
return p_x;
}//得到X坐标
void setY(int Y)
{
p_y=Y;
}//设置Y坐标
int getY()
{
return p_y;
}//得到Y坐标
private:
int p_x;
int p_y;
};
class circle
{
public:
void setBanjing(int R)
{
c_r=R;
}//设置半径
int getBanjing()
{
return c_r;
}//获取半径
void setcenter(point center)
{
c_center=center;
}//设置圆心
point getcenter()
{
return c_center;
}//获取圆心
private:
int c_r;
point c_center;//圆心
};
void Panduan(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 R_2=(c.getBanjing()*c.getBanjing());//半径的平方
if(Distance==R_2)
{
cout<<"点在圆上"<<endl;
}
else if(Distance>R_2)
{
cout<<"点在圆外"<<endl;
}
else
{
cout<<"点在圆内"<<endl;
}
}
int main()
{
//定义一个圆
circle C1;
C1.setBanjing(10);
point center;
center.setX(10);
center.setY(0);
C1.setcenter(center);
//创建点
point P;
P.setX(10);
P.setY(10);
//判断关系
Panduan(C1,P);
}