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;
}
相关推荐
郝学胜-神的一滴13 分钟前
干货版《算法导论》03:动态数组 × 链表的极致平衡艺术
java·数据结构·c++·python·算法·链表
li星野14 分钟前
栈与队列通关八题:从括号匹配到接雨水,手撕LeetCode高频题(Python + C++)
c++·python·leetcode
Byron Loong21 分钟前
【逆向】AT Hook 与 Inline Hook 对比
c语言·汇编·c++
tankeven1 小时前
C++ 算法类
c++
挨踢ren1 小时前
C++ std::function:万能函数包装器
c++
初願致夕霞2 小时前
Linux编程_应用层_HTTP与HTTPS协议
linux·c++·http·https
水云桐程序员2 小时前
C++在游戏领域的项目案例有哪些?
jvm·c++·游戏
叼烟扛炮2 小时前
C++第五讲:内存管理
c++·算法·面试·内存管理
Ricky_Theseus2 小时前
vector 与 list 区别 + 使用场景
c++
代码中介商2 小时前
C++ 异常处理完全指南
开发语言·c++