C++ day2

cpp 复制代码
#include <iostream>
using namespace std;
class Rec
{
    int length;
    int width;
public:
    void set_length(int l);  //设置长度
    void set_width(int 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()
{
    return length;
}
int Rec::get_width()
{
    return width;
}
void Rec::show()
{
    cout << "周长=" << get_length()+get_width() <<endl;
    cout << "面积=" << get_length()*get_width() <<endl;
}
int main()
{
    Rec p;
    p.set_length(6);
    p.set_width(5);
    p.show();
    return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;
class Yuan
{   double PI=3.14;
    int r;
public:
    void set_r(int sum);  //设置半径
    void show();           //输出周长和面积
};
void Yuan::set_r(int sum)
{
    r=sum;
}
void Yuan::show()
{
    cout << "周长=" << 2*PI*r << endl;
    cout << "面积=" << PI*r*r <<endl;
}
int main()
{
    Yuan p;
    p.set_r(5);
    p.show();
    return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;
class Car
{
    string brand;
    string color;
    int speed;
public:
    void display();
    void accelerate(int amount);
    void set (string b,string c,int s);
};
void Car::display()
{
    cout << "品牌:" << brand <<endl;
    cout << "颜色:" << color <<endl;
    cout << "速度:" << speed <<endl;
}
void Car::accelerate(int amount)
{
    speed=speed+amount;
}
void Car::set(string b, string c, int s)
{
    brand=b;
    color=c;
    speed=s;
}
int main()
{
    Car p;
    p.set("Volvo","black",100);
    p.display();
    p.accelerate(20);
    p.display();
    return 0;
}
相关推荐
C++ 老炮儿的技术栈2 小时前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl2 小时前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
Liu628883 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
波特率1152003 小时前
const关键字与函数的重载
开发语言·c++·函数重载
干啥啥不行,秃头第一名4 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
2301_807367194 小时前
C++中的解释器模式变体
开发语言·c++·算法
2301_819414306 小时前
C++与区块链智能合约
开发语言·c++·算法
不想看见4046 小时前
Valid Parentheses栈和队列--力扣101算法题解笔记
开发语言·数据结构·c++
老约家的可汗6 小时前
C/C++内存管理探秘:从内存分布到new/delete的底层原理
c语言·c++
天赐学c语言6 小时前
Linux - 应用层自定义协议与序列/反序列化
linux·服务器·网络·c++