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;
}
相关推荐
派阿喵搞电子4 小时前
在UI界面内修改了对象名,在#include “ui_mainwindow.h“没更新
c++·qt·ubuntu·ui
C++ 老炮儿的技术栈5 小时前
UDP 与 TCP 的区别是什么?
开发语言·c++·windows·算法·visual studio
mochensage7 小时前
CSP信奥赛C++常用系统函数汇总
c++·信奥
mochensage7 小时前
C++信息学竞赛中常用函数的一般用法
java·c++·算法
fpcc7 小时前
跟我学c++中级篇——多线程中的文件处理
c++
5:008 小时前
云备份项目
linux·开发语言·c++
乄夜8 小时前
嵌入式面试高频(5)!!!C++语言(嵌入式八股文,嵌入式面经)
c语言·c++·单片机·嵌入式硬件·物联网·面试·职场和发展
YYDS3148 小时前
C++动态规划-01背包
开发语言·c++·动态规划
wydaicls9 小时前
十一.C++ 类 -- 面向对象思想
开发语言·c++
姜君竹10 小时前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构