C++day2

1.

#include <iostream>
#include <iomanip>

using namespace std;

class RECTANGLE
{
    int length;
    int width;
public:
    void set_length(int l);
    void set_width(int w);
    int get_length(void);
    int get_width(void);
    void show(void);
};

void RECTANGLE::set_length(int l)
{
    length=l;
}
void RECTANGLE::set_width(int w)
{
    width=w;
}
int RECTANGLE::get_length(void)
{
    return length;
}
int RECTANGLE::get_width(void)
{
    return width;
}
void RECTANGLE::show(void)
{
    int c;
    int s;
    c=(length+width)*2;
    s=length*width;
    cout << setw(5) << left << "c=" << c << setw(5) << "s=" << s << endl;
}


int main()
{
    cout << "Hello World!" << endl;
    RECTANGLE r1;
    r1.set_length(5);
    r1.set_width(6);
    cout << "length=" << r1.get_length() << endl;
    cout << "length=" << r1.get_width() << endl;
    r1.show();

    return 0;
}

2.

#include <iostream>

using namespace std;

class CIRCLE
{
    int radius;
public:
    void set_r(int r);
    void show(double pi=3.14);
};

void CIRCLE::set_r(int r)
{
    radius=r;
}
void CIRCLE::show(double pi)
{
    double c,s;
    c=2*pi*radius;
    s=pi*radius*radius;
    cout << "c=" << c << endl;
    cout << "s=" << s << endl;
}

int main()
{
    CIRCLE r1;
    r1.set_r(3);
    r1.show();
    cout << "Hello World!" << endl;
    return 0;
}

3.

#include <iostream>

using namespace std;

class CAR
{
    string brand;
    string color;
    int speed;
public:
    void display(int n);
    void accelerate(int amount,int size);
    void set(string b,string c,int s);
};
void CAR::display(int n)
{
    if(n==0)
    {
        cout << "汽车品牌:" << brand << endl;
        cout << "汽车颜色:" << color << endl;
        cout << "汽车速度:" << speed << endl;
    }
    else if(n==1)
    {
        cout << "汽车品牌:" << brand << endl;
    }
    else if(n==2)
    {
        cout << "汽车颜色:" << color << endl;
    }
    else if(n==3)
    {
         cout << "汽车速度:" << speed << endl;
    }
}
void CAR::accelerate(int amount,int size)
{
    int i;
    for(i=1;i<=amount;i++)
    {
        speed+=size;
    }
}
void CAR::set(string b,string c,int s)
{
    brand=b;
    color=c;
    speed=s;
}

int main()
{
    CAR c1;
    c1.set("大众","黑色",40);
    c1.display(0);
    c1.accelerate(1,10);
    c1.display(3);
    cout << "Hello World!" << endl;
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值