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;
}
相关推荐
小刘同学32117 分钟前
C++11 特性
c++·c11新特性
真的想上岸啊31 分钟前
学习C++、QT---18(C++ 记事本项目的stylesheet)
开发语言·c++·学习
m0_5522008235 分钟前
《UE5_C++多人TPS完整教程》学习笔记40 ——《P41 装备(武器)姿势(Equipped Pose)》
c++·游戏·ue5
丁劲犇1 小时前
用 Turbo Vision 2 为 Qt 6 控制台应用创建 TUI 字符 MainFrame
开发语言·c++·qt·tui·字符界面·curse
charlie1145141912 小时前
深入理解Qt的SetWindowsFlags函数
开发语言·c++·qt·原理分析
呜喵王阿尔萨斯2 小时前
编程中的英语
c语言·c++
whoarethenext3 小时前
使用 C++/Faiss 加速海量 MFCC 特征的相似性搜索
开发语言·c++·faiss
向阳@向远方4 小时前
第二章 简单程序设计
开发语言·c++·算法
Mr_Xuhhh5 小时前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
liulilittle5 小时前
VGW 虚拟网关用户手册 (PPP PRIVATE NETWORK 基础设施)
开发语言·网络·c++·网关·智能路由器·路由器·通信