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;
}
相关推荐
我喜欢就喜欢1 小时前
2025技术成长复盘:解决问题的365天
c++·qt
神仙别闹1 小时前
基于QT(C++)+MySQL实现(窗体)学生信息管理系统
c++·qt·mysql
U-52184F692 小时前
C++ 实战:构建通用的层次化数据模型 (Hierarchical Data Model)
开发语言·c++
charlie1145141912 小时前
深入解构:MSVC 调试机制与 Visual Studio 调试器原理
c++·ide·windows·学习·visual studio·调试·现代c++
Trouvaille ~2 小时前
【C++篇】把混沌映射成秩序:哈希表的底层哲学与实现之道
数据结构·c++·stl·哈希算法·散列表·面向对象·基础入门
肆悟先生2 小时前
3.14 函数的参数传递
c++
wunianor2 小时前
[高并发服务器]DEBUG日志
linux·运维·服务器·c++
天赐学c语言3 小时前
12.19 - 买卖股票的最佳时机 && const的作用
c++·算法·leecode
.小墨迹4 小时前
C++学习之std::move 的用法与优缺点分析
linux·开发语言·c++·学习·算法·ubuntu
看见繁华4 小时前
C++ 设计模式&设计原则
java·c++·设计模式