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;
}
相关推荐
小白学大数据7 分钟前
如何避免爬虫因Cookie过期导致登录失效
开发语言·爬虫·python·scrapy
爱吃烤鸡翅的酸菜鱼1 小时前
【SpringMVC】概念引入与连接
java·开发语言·mysql
小白学大数据1 小时前
Python自动化解决滑块验证码的最佳实践
开发语言·python·自动化
碎梦归途1 小时前
23种设计模式-行为型模式之策略模式(Java版本)
java·开发语言·jvm·设计模式·策略模式·行为型模式
Albert Edison1 小时前
Python入门基础
开发语言·python
小余吃大鱼1 小时前
OpenStack私有云详细介绍
开发语言·php·openstack
画个大饼1 小时前
Swift:什么是Optional?其背后的机制是什么?什么是Unconditional Unwrapping?
开发语言·ios·swift
T0uken1 小时前
【Python】Matplotlib:立体永生花绘制
开发语言·python·matplotlib
sniper_fandc1 小时前
JVM(Java虚拟机)详解
java·开发语言·jvm
雾削木1 小时前
mAh 与 Wh:电量单位的深度解析
开发语言·c++·单片机·嵌入式硬件·算法·电脑