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;
}
相关推荐
爱和冰阔落7 分钟前
【C++STL上】栈和队列模拟实现 容器适配器 力扣经典算法秘籍
数据结构·c++·算法·leetcode·广度优先
一叶落4388 分钟前
LeetCode 300. 最长递增子序列(LIS)详解(C语言 | DP + 二分优化)
c语言·数据结构·c++·算法·leetcode
Darkwanderor9 分钟前
数据结构——trie(字典)树
数据结构·c++·字典树·trie树
一匹电信狗10 分钟前
【LeetCode面试题17.04】消失的数字
c语言·开发语言·数据结构·c++·算法·leetcode·stl
j_xxx404_10 分钟前
从 O(N) 到 O(log N):LCR 173 点名问题的五种解法与最优推导
开发语言·c++·算法
仰泳的熊猫14 分钟前
题目2265:蓝桥杯2015年第六届真题-移动距离
开发语言·数据结构·c++·算法·蓝桥杯
十年编程老舅19 分钟前
C++ 原子操作实战:实现无锁数据结构
linux·c++·c++11·原子操作·无锁队列
米码收割机24 分钟前
【AI】OpenClaw问题排查
开发语言·数据库·c++·python
CppBlock35 分钟前
HPX vs TBB vs OpenMP:并行任务模型对比
c++·算法
17(无规则自律)36 分钟前
Leetcode第六题:用 C++ 解决三数之和
c++·算法·leetcode