【C++】封装、继承、多态(基础题)

1 声明一个长方体类Box,该类有长(length)、宽(width)、高(length)三个数据成员,类中有获取及修改长、宽、高的函数,还有计算长方体表面积和体积的函数。请按上述要求声明该长方体类并在main函数中定义该类的一个对象,调用对象的各函数进行测试。

#include <iostream>
using namespace std;

class Box {
private:
    int length;
    int width;
    int height;

public:
    // Constructor
    Box(int l, int w, int h) {
        length = l;
        width = w;
        height = h;
    }

    // Getters
    int getLength() {
        return length;
    }

    int getWidth() {
        return width;
    }

    int getHeight() {
        return height;
    }

    // Setters
    void setLength(int l) {
        length = l;
    }

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

    void setHeight(int h) {
        height = h;
    }

    // Calculate surface area
    int calculateSurfaceArea() {
        return 2 * (length * width + length * height + width * height);
    }

    // Calculate volume
    int calculateVolume() {
        return length * width * height;
    }
};

int main() {
    // Create an object of the Box class
    Box box(5, 3, 4);

    // Test the functions
    cout << "Length: " << box.getLength() << endl;
    cout << "Width: " << box.getWidth() << endl;
    cout << "Height: " << box.getHeight() << endl;

    box.setLength(6);
    box.setWidth(4);
    box.setHeight(5);

    cout << "New Length: " << box.getLength() << endl;
    cout << "New Width: " << box.getWidth() << endl;
    cout << "New Height: " << box.getHeight() << endl;

    cout << "Surface Area: " << box.calculateSurfaceArea() << endl;
    cout << "Volume: " << box.calculateVolume() << endl;

    return 0;
}

2 已给商品类及其多层的派生类。以商品类为基类。第一层派生出服装类、家电类、车辆类。第二层派生出衬衣类、外衣类、帽子类、鞋子类;空调类、电视类、音响类;自行车类、轿车类、摩托车类。请给出商品类及其多层派生类的基本属性和派生过程中增加的属性。

#include <iostream>
using namespace std;
class Goods
{
public:
    int price;

protected:
    void printGoods();

private:
    int name;
};
void Goods::printGoods()
{
    cout << "Goods()" << endl;
}
class Hat : public Goods
{
public:
    string color;
    void printHat();
};
void Hat::printHat()
{
    printGoods(); // 这里可以调用 printGoods
    cout << "Hat()" << endl;
}
int main()
{
    Hat hat;
    hat.color = "green";
    hat.price = 100;
    hat.printHat();
    // hat.printGoods();  // 其他类只能访问到公共权限

    return 0;
}

3 设计一个雇员类Employee,存储雇员的编号、姓名和生日等信息,要求该类使用日期类作为成员对象,雇员类的使用如下:
//定义一个雇员,其雇员号为10,生日为1980年11月20日,姓名为Tom
Employee Tom (“Tom”, 10, 1980, 11, 20)
Date today (1980, 11, 20);
if (Tom.IsBirthday (today)//判断今天是否为Tom的生日
//…

#include <iostream>
using namespace std;

class Date
{
public:
    int year;
    int month;
    int day;
    Date(int year, int month, int day)
    {
        this->year = year;
        this->month = month;
        this->day = day;
    }
};
class Employee : public Date
{
public:
    string name;
    Employee(string name, int year, int month, int day) : Date(year, month, day), name(name) {}
    bool isBirthday(Date d)
    {
        if (d.year == year && d.month == month && d.day == day)
        {
            return true;
        }
        return false;
    }
};
int main()
{
    Employee Tom("Tom", 1, 2, 1);
    Date date(1, 2, 1);
    if (Tom.isBirthday(date))
    {
        cout << "Tom's birthday is today" << endl;
    }
    return 0;
}

或者换一种写法:

#include <iostream>
using namespace std;

class Date
{
public:
    int year;
    int month;
    int day;
    Date(int year, int month, int day)
    {
        this->year = year;
        this->month = month;
        this->day = day;
    }
    Date(){}
};
class Employee : public Date
{
public:
    string name;
    Employee(string name, int year, int month, int day)
    {
        this->name = name;
        this->year = year;
        this->month = month;
        this->day = day;
    }
    bool isBirthday(Date d)
    {
        if (d.year == year && d.month == month && d.day == day)
        {
            return true;
        }
        return false;
    }
};
int main()
{
    Employee Tom("Tom", 1, 2, 1);
    Date date(1, 2, 1);
    if (Tom.isBirthday(date))
    {
        cout << "Tom's birthday is today" << endl;
    }
    return 0;
}

4 设计一个基类 Base 为抽象类,其中包含 settitle 和 showtitle 两个成员函数,另有一个纯虚函数IsGood,由该类派生图书类Book和杂志类Journal,分别实现纯虚函数IsGood.对于前者,如果每月图书销售量超过500,则返回true;对于后者,如果每月杂志销售量超过2500,则返回true。设计这 3 个类并在main函数中测试之。

#include <iostream>
using namespace std;
class Base
{
public:
    void settitle()
    {
        cout << "settitle" << endl;
    }
    void showtitle()
    {
        cout << "showtitle" << endl;
    }
    virtual bool isGood() = 0;
};
class Book : public Base
{
public:
    int number = 600;
    bool isGood()
    {
        if (number > 500)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};
class Journal : public Base
{
public:
    int number = 100;
    bool isGood()
    {
        if (number > 500)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};
int main()
{
    Base *base = new Book;
    cout << "Book->isGood() = " << base->isGood() << endl;
    Journal journal;
    Base &base1 = journal;
    cout << "Journal->isGood() = " << base1.isGood() << endl;
    return 0;
}

5 编写一个程序实现小型公司的工资管理。该公司雇员(employee)包括经理(manager)、技术人员(technician)、销售员(salesman)和销售部经理(salesmanager)。要求存储这些人员的编号和月工资,计算月工资并显示全部信息。月工资计算办法是:经理拿固定月薪 8000元,技术人员按每小时 20 元领取月薪,销售员按该当月销售4‰提成,销售经理既拿固定月工资也领取销售提成,固定月工资为5000元,销售提成为所管辖部门当月销售额的5‰。

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

// 基类Employee
class Employee
{
public:
    Employee(int id) : id(id), salary(0) {}

    virtual void calculateSalary() = 0;
    virtual void displayInfo() = 0;

protected:
    int id;
    float salary;
};

// 经理类Manager
class Manager : public Employee
{
public:
    Manager(int id) : Employee(id) {}

    void calculateSalary() override
    {
        salary = 8000;
    }

    void displayInfo() override
    {
        cout << "经理,ID:" << id << ",月工资:" << salary << "元" << endl;
    }
};

// 技术人员类Technician
class Technician : public Employee
{
public:
    Technician(int id, float hours) : Employee(id), hours(hours) {}

    void calculateSalary() override
    {
        salary = 20 * hours;
    }

    void displayInfo() override
    {
        cout << "技术人员,ID:" << id << ",月工资:" << salary << "元" << endl;
    }

private:
    float hours;
};

// 销售员类Salesman
class Salesman : public Employee
{
public:
    Salesman(int id, float sales) : Employee(id), sales(sales) {}

    void calculateSalary() override
    {
        salary = 0.004 * sales;
    }

    void displayInfo() override
    {
        cout << "销售员,ID:" << id << ",月工资:" << salary << "元" << endl;
    }

private:
    float sales;
};

// 销售部经理类SalesManager
class SalesManager : public Employee
{
public:
    SalesManager(int id, float sales) : Employee(id), sales(sales) {}

    void calculateSalary() override
    {
        salary = 5000 + 0.005 * sales;
    }

    void displayInfo() override
    {
        cout << "销售部经理,ID:" << id << ",月工资:" << salary << "元" << endl;
    }

private:
    float sales;
};

int main()
{
    vector<Employee *> employees;

    // 添加雇员信息
    employees.push_back(new Manager(1001));
    employees.push_back(new Technician(1002, 160));
    employees.push_back(new Salesman(1003, 200000));
    employees.push_back(new SalesManager(1004, 300000));

    // 计算月工资并显示信息
    for (Employee *emp : employees)
    {
        emp->calculateSalary();
        emp->displayInfo();
    }

    // 释放内存
    for (Employee *emp : employees)
    {
        delete emp;
    }

    return 0;
}

image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秀秀_heo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值