C++ day2 作业

#include <iostream>

using namespace std;

class Rec
{
private:
    int length;
    int width;

public:
    void set_length(int l)
    {
        length = l;
    }

    void set_width(int w)
    {
        width = w;
    }

    int get_length()
    {
        return length;
    }

    int get_width()
    {
        return width;
    }

    void show()
    {
        int perimeter = 2 * (length + width);
        int area = length * width;

        cout << "Perimeter: " << perimeter << endl;
        cout << "Area: " << area << endl;
    }
};

int main()
{
    Rec r1;
    r1.set_length(5);
    r1.set_width(3);

    cout << "Length: " << r1.get_length() << endl;
    cout << "Width: " << r1.get_width() << endl;

    r1.show();

    return 0;
}
#include <iostream>
#include <string>

using namespace std;

class Circle 
{
private:
    double r;

public:
    void set_r(double radius) 
    {
        r = radius;
    }

    void show(double PI = 3.14)
    {
        double circumference = 2 * PI * r;
        double area = PI * r * r;

        cout << "Perimeter: " << circumference << std::endl;
        cout << "Area: " << area << std::endl;
    }
};

class Car 
{
public:
    string brand; 
    string color; 
    int speed; 
};

int main() 
{
    Circle circle;
    circle.set_r(5.0); 

    cout << "Circle:" << endl;
    circle.show(); 
    circle.show(3.14159); 

    Car car;
    car.brand = "Toyota";
    car.color = "Red";
    car.speed = 60;

    cout << "\nCar:" << endl;
    cout << "Brand: " << car.brand << endl;
    cout << "Color: " << car.color << endl;
    cout << "Speed: " << car.speed << " km/h" << endl;

    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值