C++day3

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

cpp 复制代码
#include <iostream>
#include <cstring>

using namespace std;

class Car
{
    string color;
    string brond;
    double speed;
public:
    Car(string c,string b,double s):color("black"),brond("Benz"),speed(180.9)
    {
        color = c;
        brond = b;
        speed = s;
        cout << "Car的有参构造" << endl;
    }
    //无参时使用默认参数
    Car():color("black"),brond("Benz"),speed(180.9){}

      void display();
      void acc(int a);
};


void Car::display()
{
    cout << "汽车品牌:" << brond << endl;
    cout << "汽车颜色:" << color << endl;
    cout << "速度:" << speed << "km/h" << endl;
}

void Car::acc(int a)
{
    speed += a;
}
int main()
{
    //有参构造函数
    Car c1("white","buick",50.5);

    c1.display();
    c1.acc(5);
    
    c1.display();
    Car c3;
    c3.display();


    return 0;
}
cpp 复制代码
#include <iostream>

using namespace std;
class Curcle
{
    int radius;
public:
    Curcle(int r):radius(5)
    {
        radius = r;
    }
    Curcle():radius(5){}
    void show(double PI=3.14);
};


void Curcle::show(double PI)
{

    double perimeter = 2 * PI * radius;
    double area = PI * radius * radius;
    cout << "圆的周长 = " << perimeter << endl;
    cout << "圆的面积 = " << area << endl;
}

int main()
{
    Curcle s1(3);
    s1.show();

    Curcle s2;
    s2.show();
    return 0;
}
cpp 复制代码
#include <iostream>

using namespace std;
class Rec
{
    int length;
    int width;
public:
//    void set_length(int l);
//    void set_width(int w);
    Rec(int l,int w)
    {
        length = l;
        width = w;
    }
    int get_length();
    int get_width();
    void show();
};

//void Rec::set_length(int l)
//{
//    length = l;
//}

//void Rec::set_width(int w)
//{
//    width = w;
//}

int Rec::get_length()
{
    cout << "length = " << length << endl;
    return length;
}

int Rec::get_width()
{
    cout << "width = " << width << endl;
    return width;
}

void Rec::show()
{
    cout << "s = " << length * width << endl;
}

int main()
{
    Rec s1(5,4);

//    s1.set_length(5);
//    s1.set_width(4);
    int l = s1.get_length();
    int w = s1.get_width();
    s1.show();

    return 0;
}

Xmind

相关推荐
cvby4 分钟前
C++11--右值引用和移动语义
开发语言·c++
pen-ai16 分钟前
【优化方法】最小二乘:从误差平方到线性与非线性拟合
人工智能·算法·机器学习
en.en..17 分钟前
TCP/UDP 收发流程(网络字节序---函数讲解)
开发语言·php
名字还没想好☜29 分钟前
Go 用 json.Decoder 流式解析大 JSON:边读边处理不爆内存
开发语言·后端·golang·go·json
xiangyun6132 分钟前
【408数据结构 03】线性表与顺序表:C++手写SeqList
开发语言·数据结构·c++
Lsir10110_1 小时前
从按钮“点不动“讲起——深入理解 Qt 信号槽机制
开发语言·qt·信号处理
君科程序定做1 小时前
用 IDL 处理高分一号(GF-1 / GF-1B/C/D)数据
c语言·开发语言
敲代码的嘎仔1 小时前
用 Redis 合并写 + DelayQueue 延迟检测,把视频播放进度写库频率降到 1/60
java·开发语言·数据库·redis·缓存·音视频·高并发
Q26433650231 小时前
【有源码】基于机器学习的电信网络诈骗话术语义识别与可视化分析系统 XGBoost算法+Hadoop+Spark
大数据·hadoop·算法·机器学习·spark·毕业设计·课程设计
不会就选b1 小时前
算法日常・每日刷题--贪心<11>
算法·leetcode·职场和发展