1.用类的方法操作个人信息
#include <iostream>
#include <cstdio>
#include<string.h>
using namespace std;
class Person{
private:
char mName[28];
int mAge;
public:
void init(char *name,int age)
{
strcpy(mName,name);
if(age>=0 && age<=100)
{
mAge = age;
}
else
{
cout<<"年龄超出范围"<<endl;
return;
}
}
void setname(char *name)
{
strcpy(mName,name);
return;
}
void getname()
{
printf("%s",mName);
return;
}
void setage(int age)
{
if(age>=0 && age<=100)
{
mAge = age;
}
else
{
cout<<"年龄超出范围"<<endl;
return;
}
return;
}
void getage()
{
cout<<mAge<<endl;
return;
}
void printPerson()
{
cout<<"name:"<<mName<<endl<<"mAge:"<<mAge<<endl;
return;
}
};
int main(int argc, char *argv[])
{
Person ob1;
ob1.init("john",30);
ob1.printPerson();
return 0;
}
2.用类的方法构建立方体,判断俩立方体是否相等
#include <iostream>
#include <cstdio>
#include<string.h>
using namespace std;
class Cube{
private:
int a;
int b;
int c;
public:
void init(int chang,int kuan,int gao)
{
a = chang;
b =kuan;
c = gao;
}
void setchang(int chang)
{
a = chang;
return;
}
int getchang()
{
return a;
}
void setkuan(int kuan)
{
b = kuan;
return;
}
int getkuan()
{
return b;
}
void setgao(int gao)
{
c = gao;
return;
}
int getgao()
{
return c;
}
int area()
{
return 2*(a*b+a*c+b*c);
}
int volume()
{
return a*b*c;
}
};
bool compareCube(Cube &ob1,Cube &ob2)
{
if((ob1.area()==ob2.area())&&(ob1.volume()==ob2.volume()))
{
return true;
}
else
{
return false;
}
}
int main(int argc, char *argv[])
{
Cube ob1;
Cube ob2;
ob1.init(3,4,5);
ob2.init(3,4,5);
if(compareCube(ob1,ob2))
{
cout<<"两个立方体相等"<<endl;
}
else
{
cout<<"两个立方体不相等"<<endl;
}
return 0;
}
3.点与圆的关系
#include <iostream>
#include <cstdio>
#include<string.h>
using namespace std;
enum Compare{ON,OUT,IN};
class Point{
private:
int mX;
int mY;
public:
void setX(int x)
{
mX = x;
}
int getX()
{
return mX;
}
void setY(int y)
{
mY = y;
}
int getY()
{
return mY;
}
};
class Circle{
private:
Point mP;//定义圆心
int mR;
public:
void setPoint(int x,int y)
{
mP.setX(x);
mP.setY(y);
}
Point getPoint()
{
return mP;
}
void setR(int r)
{
mR = r;
}
int getR()
{
return mR;
}
};
int main(int argc, char *argv[])
{
Point p1;
Circle o1;
p1.setX(1);
p1.setY(1);
o1.setPoint(2,2);
o1.setR(1);
int px1 = p1.getX();
int py1 = p1.getY();
int px2 = o1.getPoint().getX();
int py2 = o1.getPoint().getY();
int mR = o1.getR();
if(((px1-px2)*(px1-px2)+(py1-py2)*(py1-py2))>mR*mR)
{
cout<<"在圆外"<<endl;
}
else if(((px1-px2)*(px1-px2)+(py1-py2)*(py1-py2))<mR*mR)
{
cout<<"在圆内"<<endl;
}
else
{
cout<<"在圆上"<<endl;
}
return 0;
}