C++ day3——C++核心

作业:

整理思维导图

2、整理课上代码

3、把课上类的三个练习题的构造函数写出来

1、定义一个矩形类Rec
cpp 复制代码
#include <iostream>

using namespace std;
class Rec
{
   const int length;
   int width;
public:
   Rec(int length,int width):length(length),width(width)
   {
      cout<<"长度为:"<<length<<endl;
      cout<<"宽度为:"<<width<<endl;
   }
   void show();
};
void Rec::show()
{
    cout << "周长为:" << 2*(length+width) << endl;
    cout << "面积是:" << length*width << endl;
}
int main()
{
    int a,b;
    cout<<"请输入长度:"<<endl;
    cin>>a;
    cout<<"请输入宽度:"<<endl;
    cin>>b;
    Rec s1(a,b);
    s1.show();
    return 0;
}
2、定义一个圆类
cpp 复制代码
#include <iostream>
using namespace std;
class Cir
{
    int &r;
public:
    Cir (int &r):r(r)
    {cout<<"有参构造"<<endl;}
    void show(double PI=3.14);
};
void Cir::show(double PI)
{
    cout << "周长:" << 2*r*PI << endl;
    cout << "面积:" << r*r*PI << endl;
}
int main()
{
    int r;
    cout<<"请输入半径:"<<endl;
    cin>>r;
    Cir p1(r);
    p1.show();
    return 0;
}
3、定义一个Car类
cpp 复制代码
#include <iostream>
using namespace std;
class Car
{
    string color;
    string brand;
    int speed;
public:
    Car (string color,string brand,int speed):color(color),brand(brand),speed(speed)
    {
        cout<<"有参构造"<<endl;
        cout << "color: " << color << endl;
        cout << "brand: " << brand << endl;
        cout << "speed: " << speed << endl;
    }
    void acc(int a);
};
//给车加速
void Car::acc(int a)
{
    speed+=a;
    cout << "speed: " << speed << endl;
}
int main()
{
    string c,b;
    int s;
    cout << "color: " <<  endl;
    cin>>c;
    cout << "brand: " <<  endl;
    cin>>b;
    cout << "speed: " <<  endl;
    cin>>s;
    Car c1(c,b,s);

    int a;
    cout<<"车的加速度为"<<endl;
    cin>>a;
    c1.acc(a);
    return 0;
}
相关推荐
吃好睡好便好3 分钟前
在Matlab中绘制抛物三维曲面图
开发语言·人工智能·学习·算法·matlab·信息可视化
半步仙人5 分钟前
MATLAB的几种取整操作总结
开发语言·matlab
伯远医学11 分钟前
Nat. Methods | 邻近标记技术:活细胞中捕捉分子互作的新利器
java·开发语言·前端·javascript·人工智能·算法·eclipse
wjs202420 分钟前
Matplotlib 轴标签和标题
开发语言
wangjialelele23 分钟前
Linux SystemV 消息队列 + 责任链模式:实现客户端消息处理流水线
linux·服务器·c语言·网络·c++·责任链模式
刘永鑫Adam30 分钟前
Nature Microbiology | 基于TRACS算法的跨多界宏基因组数据菌株水平溯源推演
算法
小O的算法实验室32 分钟前
2026年SEVC,面向无人机辅助边缘计算的自适应群体智能算法,深度解析+性能实测
算法·边缘计算·智能算法·智能算法改进
XMYX-033 分钟前
27 - Go string 字符串处理与格式化:从底层原理到工程实践
开发语言·golang
赏金术士36 分钟前
Kotlin 协程面试题大全(Android 高频版)
android·开发语言·kotlin
高锰酸钾_38 分钟前
计算机网络-网络层-路由算法与路由协议
计算机网络·算法·智能路由器