day2C++作业

cpp 复制代码
#include <iostream>

using namespace std;

class Rec
{
    int lenth;
    int width;
public:
    void set_lenth(int l);
    void set_width(int w);
    int get_lenth();
    int get_width();
    void show();
};
void Rec::set_lenth(int l)
{
    lenth=l;
}
void Rec::set_width(int w)
{
    width=w;
}
int Rec::get_lenth()
{
    return lenth;
}
int Rec::get_width()
{
    return width;
}
void Rec::show()
{
    int c=lenth*2+width*2;
    int s=lenth*width;
    cout << "周长=" << c << " 面积=" << s <<endl;
}
int main()
{
    Rec p;
    p.set_lenth(8);
    p.set_width(9);
    cout<< "Rec_lenth=" <<p.get_lenth()<<endl;
    cout<< "Rec_width=" <<p.get_width()<<endl;
    p.show();
    return 0;
}
cpp 复制代码
#include <iostream>
#include <iomanip>
#include <cmath>


using namespace std;
class Circle
{
    int r;
public:
    void set_r(int m);
    void show(double PI=3.14);
};
void Circle::set_r(int m)
{
    r=m;
}
void Circle::show(double PI)
{
    float c=2*PI*r;
    cout << setprecision(4) << "圆的周长="<<c<<endl;
    float s=PI*pow(r,2);
    cout << setprecision(4) << "圆的面积="<<s<<endl;
}
int main()
{
    Circle p;
    p.set_r(5);
    p.show();

    return 0;
}
cpp 复制代码
#include <iostream>

using namespace std;
class Car
{
    string brand;
    string color;
    int speed;
public:
   void set(string b,string c,int s);
   void display();
   void accelerate(int amount);
};
void Car::set(string b,string c,int s)
{
    brand=b;
    color=c;
    speed=s;
}
void Car::display()
{
    cout << "brand:" << brand << " color:" << color << " speed:" << speed <<endl;
}
void Car:: accelerate(int amount)
{
    cout << "加速前speed=" << speed;
    speed+=amount;
    cout << " 加速后speed=" << speed << endl;
}
int main()
{
    Car mi;
    mi.set("xiaomi","blue",200);
    mi.display();
    mi.accelerate(100);

    return 0;
}
相关推荐
王璐WL4 分钟前
【C++】经典易错题(2)
c++
我不是懒洋洋6 分钟前
手写一个异步日志库:从printf到高性能无锁日志
java·c语言·开发语言·c++·visual studio
hetao17338379 分钟前
2026-05-28~06-02 hetao1733837 的刷题记录
c++·算法
wunaiqiezixin24 分钟前
如何在C++中实现一个单例模式?
c++·单例模式
一个爱编程的人28 分钟前
图的相关概念
c++·算法·图论
basketball6162 小时前
设计模式入门:2. 工厂模式详解 C++实现
开发语言·c++·设计模式
Lumbrologist2 小时前
【C++】零基础入门 · 第 16 节:智能指针
开发语言·c++
前进吧-程序员2 小时前
CRTP 与静态多态:不用虚函数也能多态
c++
basketball6162 小时前
设计模式入门:1. 单例模式详解 C++实现
c++·单例模式·设计模式
Brilliantwxx2 小时前
【C++】 红黑树封装 STL set/map 超详细解析
开发语言·c++