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;
}
相关推荐
OneQ6664 小时前
C++讲解---创建日期类
开发语言·c++·算法
Coding小公仔6 小时前
C++ bitset 模板类
开发语言·c++
菜鸟看点7 小时前
自定义Cereal XML输出容器节点
c++·qt
悲伤小伞8 小时前
linux_git的使用
linux·c语言·c++·git
ysa0510308 小时前
数论基础知识和模板
数据结构·c++·笔记·算法
小小小小王王王11 小时前
求猪肉价格最大值
数据结构·c++·算法
岁忧11 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
码农编程录13 小时前
【c/c++3】类和对象,vector容器,类继承和多态,systemd,std&boost
c++
??tobenewyorker14 小时前
力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
数据结构·c++·算法·leetcode
oioihoii15 小时前
C++11 forward_list 从基础到精通:原理、实践与性能优化
c++·性能优化·list