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;
}
相关推荐
CoderCodingNo11 小时前
【GESP】C++六级考试大纲知识点梳理, (7) 栈与队列
开发语言·c++
超级大福宝11 小时前
【力扣200. 岛屿数量】的一种错误解法(BFS)
数据结构·c++·算法·leetcode·广度优先
Frank_refuel12 小时前
C++之继承
开发语言·c++
哪有时间简史12 小时前
C++程序设计
c++
%xiao Q13 小时前
GESP C++四级-216
java·开发语言·c++
tianyuanwo13 小时前
深入浅出SWIG:从C/C++到Python的无缝桥梁
c语言·c++·python·swig
初次见面我叫泰隆14 小时前
Qt——2、信号和槽
开发语言·c++·qt
D_evil__14 小时前
【Effective Modern C++】第二章 auto:5. 优先使用 auto,而非显式类型声明
c++
玖釉-14 小时前
[Vulkan 学习之路] 26 - 图像视图与采样器 (Image View and Sampler)
c++·windows·图形渲染
一颗青果14 小时前
C++的锁 | RAII管理锁 | 死锁避免
java·开发语言·c++