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;
}
相关推荐
兩尛6 小时前
c++知识点2
开发语言·c++
fengfuyao9856 小时前
海浪PM谱及波形的Matlab仿真实现
开发语言·matlab
xiaoye-duck6 小时前
C++ string 底层原理深度解析 + 模拟实现(下)——面试 / 开发都适用
开发语言·c++·stl
Hx_Ma167 小时前
SpringMVC框架提供的转发和重定向
java·开发语言·servlet
期待のcode8 小时前
原子操作类LongAdder
java·开发语言
lly2024069 小时前
C 语言中的结构体
开发语言
JAVA+C语言9 小时前
如何优化 Java 多主机通信的性能?
java·开发语言·php
SilentSlot10 小时前
【QT-QML】5. 简单变换
qt·qml
青岑CTF10 小时前
攻防世界-Ics-05-胎教版wp
开发语言·安全·web安全·网络安全·php
Li emily10 小时前
如何通过外汇API平台快速实现实时数据接入?
开发语言·python·api·fastapi·美股