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

相关推荐
道不尽世间的沧桑29 分钟前
第17篇:网络请求与Axios集成
开发语言·前端·javascript
久绊A36 分钟前
Python 基本语法的详细解释
开发语言·windows·python
软件黑马王子4 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫4 小时前
go orm GORM
开发语言·后端·golang
计算机小白一个5 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^5 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
黑不溜秋的5 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
李白同学6 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?7 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农7 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx