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;
}
相关推荐
_codemonster1 分钟前
计算机视觉入门到实战系列(八)Harris角点检测算法
python·算法·计算机视觉
Snow_day.6 分钟前
有关排列排列组合(1)
数据结构·算法·贪心算法·动态规划·图论
随丶芯6 分钟前
IDEA安装leetcode-editor插件
java·开发语言
Ccjf酷儿20 分钟前
C++语言程序设计 (郑莉)第六章 数组、指针和字符串
开发语言·c++
禹曦a21 分钟前
Java实战:Spring Boot 构建电商订单管理系统RESTful API
java·开发语言·spring boot·后端·restful
superman超哥22 分钟前
精确大小迭代器(ExactSizeIterator):Rust性能优化的隐藏利器
开发语言·后端·rust·编程语言·rust性能优化·精确大小迭代器
芒克芒克22 分钟前
虚拟机类加载机制
java·开发语言·jvm
陌路2023 分钟前
C++28 STL容器--array
开发语言·c++
dora27 分钟前
【开发火星地平线辅助】智商不够,编程来凑
算法
im_AMBER30 分钟前
Leetcode 100 在链表中插入最大公约数
数据结构·c++·笔记·学习·算法·leetcode·链表