C++作业3(类)

1、思维导图

2、类中存在引用成员情况下有参构造

cpp 复制代码
#include <iostream>

using namespace std;
class Cy
{
    float &r;
public:
    Cy(float &r):r(r){
        cout<<"类中存在引用成员情况下有参构造"<<endl;
    };
    float  set_r(float);
    void show();
};
float Cy::set_r(float a)
{
    r=a;
    cout<<"半径r为"<<r<<endl;
    return r;
}

void Cy::show()
{
    float Pi=3.14;
    float S=Pi*r*r;
    cout<<"面积为"<<S<<endl;
    float C=2*Pi*r;
    cout<<"周长为"<<C<<endl;
}
int main()
{
    float r=2.0;
    Cy s1(r);
     s1.show();
    s1.set_r(1);
    s1.show();
    return 0;
}

3、类中存在const修饰成员情况下有参构造

cpp 复制代码
#include <iostream>
using namespace std;
class Rec
{
    const int length;
    int width;
public:
    //有参构造函数
    Rec(int length,int width):length(length),width(width){
        cout<<"类中存在const修饰成员情况下有参构造"<<endl;
    };
    void set_length(int );
    void set_width(int );
    int get_length();
    int get_width();
    void show();
};
void Rec::set_length(int a)
{
  //  length=a;
}
void Rec::set_width(int b)
{
    width=b;
}
int Rec::get_length()
{
    cout<<"长度为"<<length<<endl;
    return length;
}
int Rec::get_width()
{
     cout<<"宽度为"<<width<<endl;
    return width;
}
void Rec::show()
{
    int S=width*length;
    int C=2*width+2*length;
    cout<<"面积为"<<S<<endl;
    cout<<"周长为"<<C<<endl;
}
int main()
{
     Rec s1(4,3);
     //s1.set_length(3);
     s1.set_width(2);
     s1.get_length();
     s1.get_width();
     s1.show();
    return 0;
}

4、一般情况下有参构造

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;
    }
    void set(string,string,int);
    void display();
    void acc(int a);

};
void Car::set(string s1,string s2,int c )
{
    color=s1;
    brand=s2;
    speed=c;

}
void Car::display()
{
    cout<<"汽车颜色为"<<color<<endl;
    cout<<"汽车品牌为"<<brand<<endl;
    cout<<"汽车速度为"<<speed<<endl;
}
void Car::acc(int a)
{
    while(speed<=120)
    {
        speed+=a;
        cout<<"汽车加速中,当前速度为>>>"<<speed<<endl;
    }
    cout<<"汽车已到最高速度120>>>"<<endl;


}
int main()
{
    Car p1("red","奥迪",120);
    p1.display();

    p1.set("bule","五菱",100);
    p1.display();
    p1.acc(10);
    return 0;
}
相关推荐
爬虫程序猿1 小时前
用 Python 给京东商品详情做“全身 CT”——可量产、可扩展的爬虫实战
开发语言·爬虫·python
徐同保3 小时前
tailwindcss暗色主题切换
开发语言·前端·javascript
蓝纹绿茶3 小时前
bash:**:pip:***python: 错误的解释器: 没有那个文件或目录
开发语言·python·pip
AA陈超3 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-14 属性菜单 - 文本值行
c++·游戏·ue5·游戏引擎·虚幻
云知谷3 小时前
【经典书籍】C++ Primer 第15章类虚函数与多态 “友元、异常和其他高级特性” 精华讲解
c语言·开发语言·c++·软件工程·团队开发
START_GAME4 小时前
深度学习Diffusers:用 DiffusionPipeline 实现图像生成
开发语言·python·深度学习
不爱编程的小九九4 小时前
小九源码-springboot088-宾馆客房管理系统
java·开发语言·spring boot
weixin_582985184 小时前
OpenCV cv::Mat.type() 以及类型数据转换
c++·opencv·计算机视觉
Evand J5 小时前
【MATLAB例程】到达角度定位(AOA),平面环境多锚点定位(自适应基站数量),动态轨迹使用EKF滤波优化。附代码下载链接
开发语言·matlab·平面·滤波·aoa·到达角度
细节控菜鸡5 小时前
【2025最新】ArcGIS for JS 实现随着时间变化而变化的热力图
开发语言·javascript·arcgis