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;
}
相关推荐
lly20240618 分钟前
AJAX JSON 实例
开发语言
QiZhang | UESTC29 分钟前
JAVA算法练习题day27
java·开发语言·c++·算法·leetcode·hot100
坚持就完事了31 分钟前
2-C语言中的数据类型
c语言·开发语言
灵性花火1 小时前
记录Qt的多个bug
qt·bug
ss2731 小时前
手写MyBatis第96弹:异常断点精准捕获MyBatis深层BUG
java·开发语言·bug·mybatis
Stanford_11061 小时前
关于嵌入式硬件需要了解的基础知识
开发语言·c++·嵌入式硬件·微信小程序·微信公众平台·twitter·微信开放平台
白水先森1 小时前
Python 运算符与列表(list)
java·开发语言
小政同学2 小时前
【Python】小练习-考察变量作用域问题
开发语言·python
是那盏灯塔2 小时前
16.C++三大重要特性之多态
开发语言·c++
lsx2024062 小时前
网站验证:确保在线安全与用户体验的关键步骤
开发语言