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

相关推荐
xiaoxiaoxiaolll20 小时前
金属结构疲劳寿命预测与健康监测技术
人工智能·算法·机器学习
故事和你9120 小时前
洛谷-【图论2-1】树4
开发语言·数据结构·c++·算法·动态规划·图论
故事和你9120 小时前
洛谷-【图论2-1】树1
开发语言·数据结构·c++·算法·深度优先·动态规划·图论
段ヤシ.20 小时前
回顾Java知识点,面试题汇总Day5(持续更新)
java·开发语言
不会C语言的男孩20 小时前
C++ SLTL编程
java·开发语言·c++
java修仙传20 小时前
Java 实习日记:从业务表关系到节点价格分析接口改造
java·开发语言·实习
qq_4523962320 小时前
第十四篇:《JMeter插件扩展:自定义函数与第三方插件》
开发语言·python·jmeter
敲代码的嘎仔20 小时前
力扣高频SQL基础50题详解
开发语言·数据库·笔记·sql·算法·leetcode·后端开发
码农-阿杰20 小时前
Java 线程等待唤醒机制深度解析:synchronized、ReentrantLock、LockSupport 底层实现对比
java·开发语言·c++
赤水无泪20 小时前
Qt 全模块汇总列表
开发语言·qt