C++ day3

复制代码
#include <iostream>

using namespace std;
class rec
{
    const int length;
    int width;
public:
    rec (int length):length(length){};
    void set_width(int W);
    int get_length();
    int get_width();
    void show();
};

void rec::set_width(int W)
{
    width=W;
}
int rec::get_length()
{
    return length;
}
int rec::get_width()
{
    return width;
}
void rec::show()
{
    int l,s;
    l=2*(length+width);
    s=length*width;
    cout<<"周长:"<<l<<endl;
    cout<<"面积:"<<s<<endl;
}
int main()
{
    rec s(6);
    s.set_width(5);

    int a=s.get_width();
    int b=s.get_length();
    cout<<a<<endl;
    cout<<b<<endl;
    s.show();
    return 0;
}
复制代码
#include <iostream>

using namespace std;
class y
{
    int &R;
public:
    y(int &R):R(R){};
    void show();
};

void y::show()
{
    double PI=3.14;

    double l,s;
    l=2*PI*R;
    s=PI*R*R;
    cout<<"周长:"<<l<<endl;
    cout<<"面积:"<<s<<endl;
}
int main()
{
    int r=4;
    y s(r);
    s.show();
    return 0;
}
复制代码
#include <iostream>

using namespace std;
class car
{
    string se;
    string brand;
    int speed;
public:
    car(string a,string b,int c):se(a),brand(b),speed(c){};
    void display();
    void acc(int a);
};

void car::display()
{
    cout<<se<<endl;
    cout<<brand<<endl;
    cout<<speed<<endl;
}
void car::acc(int a)
{
    speed=a;
}
int main()
{

    string a,b;
    getline(cin,a);
    getline(cin,b);
    car s(a,b,50);
    s.display();
    s.acc(80);
    return 0;
}
相关推荐
爱写代码的小朋友5 小时前
从零开始学 Win32 API:C++ 窗口编程实战(VS Code + MinGW-w64 命令行详解)
开发语言·c++
果汁华5 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
小小晓.5 小时前
C++:语句和作用域
开发语言·c++
wanderist.6 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
海天鹰7 小时前
PHP上传文件
android·开发语言·php
Yeauty8 小时前
渲染成图再 CLI 拼接,还是进程内直推?Rust 帧到视频的两条路
开发语言·rust·音视频
geovindu9 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
库克克9 小时前
【C++】set 与multiset
开发语言·c++
Wang's Blog9 小时前
Go-Zero项目开发34: 微服务超时控制与重试机制实践
开发语言·微服务·golang·go-zero
2zcode10 小时前
项目文档:基于MATLAB神经网络的心力衰竭预测与临床辅助决策系统研究
开发语言·神经网络·matlab·心力衰竭预测\