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;
}
相关推荐
乌萨奇也要立志学C++3 分钟前
【LeetCode】set和map相关算法题 前K个高频单词、随机链表的复制、两个数组的交集、环形链表
算法·leetcode·链表
爱吃生蚝的于勒9 分钟前
一文学会c++继承 组合
java·c语言·开发语言·数据结构·c++·算法·蓝桥杯
WSSWWWSSW9 分钟前
Numpy科学计算与数据分析:Numpy数据分析基础之统计函数应用
开发语言·python·数据挖掘·数据分析·numpy
西猫雷婶27 分钟前
python学智能算法(三十四)|SVM-KKT条件回顾
开发语言·人工智能·python·算法·机器学习·支持向量机
愿天堂没有C++27 分钟前
剑指offer第2版——面试题1:赋值运算符函数
c++·面试
zgc124536735 分钟前
Linux学习-数据结构(链表)
linux·开发语言·数据结构·vscode·链表
OEC小胖胖37 分钟前
幕后英雄 —— Background Scripts (Service Worker)
开发语言·前端·javascript·浏览器·web·扩展
红鲤鱼遇绿鲤鱼1 小时前
cf Educational Codeforces Round 177 C. Disappearing Permutation
java·c语言·算法
欧阳小猜2 小时前
机器学习④【算法详解:从决策树到随机森林】
算法·决策树·机器学习
2501_924731472 小时前
复杂路况下车牌识别准确率↑19%:陌讯动态特征融合算法实战解析
人工智能·算法·目标检测·计算机视觉·目标跟踪