C++核心day3作业

发布于:2024-12-18 ⋅ 阅读:(83) ⋅ 点赞:(0)

作业:

1.整理思维导图

2.整理课上代码

3.把课上类的三个练习题的构造函数写出来

函数全部类内声明,类外定义

  1. 定义一个矩形类Rec,包含私有属性length、width,包含公有成员方法:

    1. void set_length(int l); //设置长度
    2. void set_width(int w); //设置宽度
    3. int get_length(); //获取长度,将长度的值返回给调用处
    4. int get_width(); //获取宽度,将宽度的值返回给调用处
    5. void show(); //输出周长和面积
#include <iostream>
using namespace std;

class Rec {
private:
    int length;  // 私有属性:长度
    int width;   // 私有属性:宽度
public:
    void setLength(int l);  // 设置长度
    void setWidth(int w);   // 设置宽度
    int getLength();        // 获取长度
    int getWidth();         // 获取宽度
    void show();            // 输出周长和面积
};

void Rec::setLength(int l) {
    length = l;
}

void Rec::setWidth(int w) {
    width = w;
}

int Rec::getLength() {
    return length;
}

int Rec::getWidth() {
    return width;
}

void Rec::show() {
    cout << "周长: " << 2 * (length + width) << endl;
    cout << "面积: " << length * width << endl;
}

int main() {
    Rec a;
    int l, w;
    cout << "输入长度:";
    cin >> l;
    a.setLength(l);
    cout << "输入宽度:";
    cin >> w;
    a.setWidth(w);
    cout << "矩形的周长和面积为:" << endl;
    a.show();
    return 0;
}
  1. 定义一个圆类,包含私有属性半径r,公有成员方法:

    1. void set_r(int r); //获取半径
    2. void show //输出周长和面积,show函数中需要一个提供圆周率的参数PI,该参数有默认值3.14
#include <iostream>
#include <iomanip> // 用于设置输出格式
using namespace std;

class Circle {
private:
    double radius; // 私有属性:半径
public:
    void setR(double &r);       // 设置圆的半径
    void show(double PI = 3.14); // 输出周长和面积
};

void Circle::setR(double &r) {
    radius = r;
}

void Circle::show(double PI) {
    double circumference = 2 * PI * radius; // 计算周长
    double area = PI * radius * radius;     // 计算面积
    cout << "周长: " << fixed << setprecision(2) << circumference << endl;
    cout << "面积: " << fixed << setprecision(2) << area << endl;
}

int main() {
    Circle c;     // 创建圆类的对象 c
    double r;
    cout << "输入圆的半径: ";
    cin >> r;
    c.setR(r); // 设置半径
    cout << "圆的周长和面积为:" << endl;
    c.show();
    return 0;
}
  1. 定义一个Car类,包含私有属性,颜色color,品牌brand,速度speed,包含公有成员方法:

    1. void display(); //显示汽车的品牌,颜色和速度
    2. void acc(int a); //加速汽车
    3. set函数,设置类中的私有属性
#include <iostream>
#include <string> // 引入字符串库
using namespace std;

class Car {
private:
    string color; // 汽车颜色
    string brand; // 汽车品牌
    int speed;    // 汽车速度
public:
    void set(string c, string b, int s); // 设置汽车的属性
    void display();                     // 显示汽车信息
    void acc(int a);                    // 加速汽车
};

void Car::set(string c, string b, int s) {
    color = c;
    brand = b;
    speed = s;
}

void Car::display() {
    cout << "品牌: " << brand << ", 颜色: " << color << ", 速度: " << speed << " km/h" << endl;
}

void Car::acc(int a) {
    speed += a; // 增加速度
    cout << "加速 " << a << " km/h, 当前速度: " << speed << " km/h" << endl;
}

int main() {
    Car myCar; // 创建Car类的对象myCar
    myCar.set("红色", "奥迪", 0); // 设置汽车的初始属性
    cout << "初始状态:" << endl;
    myCar.display();
    myCar.acc(50); // 加速50 km/h
    myCar.acc(30); // 再加速30 km/h
    return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到