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

相关推荐
手握风云-4 分钟前
JavaEE初阶第十二期:解锁多线程,从 “单车道” 到 “高速公路” 的编程升级(十)
java·开发语言·java-ee
NicolasCage5 分钟前
C语言指针Pointers
c++·后端
Deng9452013146 分钟前
数独求解器与生成器(回溯算法实现)
算法·图形用户界面·matlab技术·数独谜题·求解器与生成器
淦暴尼10 分钟前
银行客户流失预测分析
python·深度学习·算法
「QT(C++)开发工程师」10 分钟前
Qt C++动态库SDK在Visual Studio 2022使用(C++/C#版本)
c++·qt·c#·visual studio
Swiler10 分钟前
数据结构第1问:什么是数据结构?
数据结构·算法
Eloudy21 分钟前
复矩阵与共轭转置矩阵乘积及其平方根矩阵
人工智能·算法·矩阵
m0_6313544524 分钟前
VTK开发day2:切片矩阵
人工智能·算法·矩阵
go546315846541 分钟前
在本地环境中运行 ‘dom-distiller‘ GitHub 库的完整指南
人工智能·深度学习·神经网络·算法·矩阵·github
awonw44 分钟前
[python][flask]Flask-Principal 使用详解
开发语言·python·flask