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;
}
相关推荐
duapple1 小时前
Golang基于反射的ioctl实现
开发语言·后端·golang
Dxy12393102162 小时前
Python 条件语句详解
开发语言·python
勇闯逆流河3 小时前
【数据结构】堆
c语言·数据结构·算法
jjkkzzzz3 小时前
Linux下的c/c++开发之操作Redis数据库
数据库·c++·redis
prinrf('千寻)4 小时前
MyBatis-Plus 的 updateById 方法不更新 null 值属性的问题
java·开发语言·mybatis
pystraf4 小时前
LG P9844 [ICPC 2021 Nanjing R] Paimon Segment Tree Solution
数据结构·c++·算法·线段树·洛谷
m0_555762904 小时前
Qt缓动曲线详解
开发语言·qt
Funny-Boy4 小时前
菱形继承原理
c++
飞川撸码5 小时前
【LeetCode 热题100】739:每日温度(详细解析)(Go语言版)
算法·leetcode·golang
揽你·入怀5 小时前
数据结构:ArrayList简单实现与常见操作实例详解
java·开发语言