c++练习:类的基础练习

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值