c++day2

 矩形类

#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(){
    cout<<"请输入长度:";
    cin>>length;
    return length;
}
int Rec::get_width(){
    cout<<"请输入宽度:";
    cin>>width;
    return width;
}
void Rec::show(){
    cout<<"周长:"<<(length+width)*2<<endl;
    cout<<"面积:"<<length*width<<endl;
}

int main()
{
    Rec a;
    a.set_length(a.get_length());
    a.set_width(a.get_width());
    a.show();
    return 0;
}

 

 圆类

#include <iostream>

using namespace std;
class circle
{
    int r;
//    double PI=3.14;
public:
    int set_r(int l);
    void show(int r,double PI=3.14);
};
int circle::set_r(int l){
    r=l;
    return r;
}
void circle::show(int r, double PI){
    cout<<"周长:"<<2*r*PI<<endl;
    cout<<"面积:"<<PI*r*r<<endl;
}
int main()
{
    circle a;
    a.show(a.set_r(3));
    return 0;
}

 Car类

#include <iostream>

using namespace std;
class Car{
private:
    string brand;
    string color;
    int speed;
public:
    void display();
    void accelerate(int amount);
    void set(string brand,string color,int speed);
};
void Car::display(){
    cout<<"brand:"<<brand<<endl;
    cout<<"color:"<<color<<endl;
    cout<<"speed:"<<speed<<endl;
}
void Car::accelerate(int amount){
    speed+=amount;
}
void Car::set(string brand, string color, int speed){
    Car::brand=brand;
    this->color=color;
    this->speed=speed;
}
int main()
{
    Car car;
    car.set("geely","white",10);
    car.display();
    car.accelerate(10);
    car.display();
    return 0;
}

 

相关推荐

  1. MSc CDA Take-Home

    2024-04-25 12:44:05       60 阅读
  2. CDA一级备考策略分享

    2024-04-25 12:44:05       32 阅读
  3. CDA-LevelⅡ【考题整理-带答案】

    2024-04-25 12:44:05       50 阅读
  4. Spring Data访问Elasticsearch----CDI集成

    2024-04-25 12:44:05       36 阅读
  5. CDA Level Ⅰ 2023认证考试大纲

    2024-04-25 12:44:05       78 阅读
  6. web server apache tomcat11-33-CDI

    2024-04-25 12:44:05       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-25 12:44:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-25 12:44:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-25 12:44:05       82 阅读
  4. Python语言-面向对象

    2024-04-25 12:44:05       91 阅读

热门阅读

  1. Linux系统编程_文件编程

    2024-04-25 12:44:05       28 阅读
  2. 添加修改ubuntu中环境变量(PATH)

    2024-04-25 12:44:05       36 阅读
  3. 【ARMv9 DSU-120 系列 3 -- 系统控制寄存器】

    2024-04-25 12:44:05       29 阅读
  4. Flink 作业管理器:核心功能、角色与责任详解

    2024-04-25 12:44:05       37 阅读
  5. 在Linux中eth0旁边的lo是什么

    2024-04-25 12:44:05       30 阅读
  6. vue2使用elementUI报错

    2024-04-25 12:44:05       114 阅读
  7. 学习笔记-微服务高级(黑马程序员)

    2024-04-25 12:44:05       33 阅读
  8. 4月24日,每日信息差

    2024-04-25 12:44:05       34 阅读
  9. python元组与列表的区别

    2024-04-25 12:44:05       99 阅读
  10. Okapi Framework

    2024-04-25 12:44:05       39 阅读
  11. Android配置环境

    2024-04-25 12:44:05       40 阅读
  12. C++读写二进制文件

    2024-04-25 12:44:05       32 阅读
  13. MacBook系统升级导致idea无法打开

    2024-04-25 12:44:05       34 阅读