posts - 137,comments - 0,views - 40818

Book.h:

复制代码
#pragma once
#include <string>
using namespace std;
class Book
{
public:
    Book(const string& bookname, const string& isbn, double price);
    double getPrice();
    string getISBN();
    string getBookname();
protected:
    double price;
    string ISBN;
    string bookname;
};
复制代码

Book.cpp:

复制代码
#include "Book.h"

Book::Book(const string& bookname, const string& isbn, double price)
{
    this->bookname = bookname;
    this->ISBN = isbn;
    this->price = price;
}

double Book::getPrice()
{
    return price;
}

string Book::getISBN()
{
    return ISBN;
}

string Book::getBookname()
{
    return bookname;
}
复制代码

Sellbook.h:

复制代码
#pragma once
#include "Book.h"
#include <string>
using namespace std;
class Sellbook : public Book
{
public:
    Sellbook(string bookname, string isbn, double price, double discount = 10.0);
    void setDiscount(double discount);
    double getDiscount();
    double getPrice();
private:
    double discount;
};
复制代码

Sellbook.cpp:

复制代码
#include "Sellbook.h"

Sellbook::Sellbook(string bookname, string isbn, double price, double discount ) :Book(bookname,isbn,price)
{
    this->discount = discount;
}

void Sellbook::setDiscount(double discount)
{
    this->discount = discount;
}

double Sellbook::getDiscount()
{
    return discount;
}

double Sellbook::getPrice()
{
    return price * discount * 0.1;
}
复制代码

main.cpp:

复制代码
#include <iostream>
#include <string>
#include "Book.h"
#include "Sellbook.h"
using namespace std;
int main() {
    Book b1("C程序设计","02222",50);
    Sellbook b2("C++程序设计","300012",24);
    cout << b1.getBookname() << "的原价是:" << b1.getPrice() << ",书号是:" << b1.getISBN() << endl;
    cout << b2.getBookname() << "的原价是:" << b2.getPrice() << ",书号是:" << b2.getISBN() << endl;
    b2.setDiscount(5.0);
    cout << b2.getBookname() << "的折扣是:" << b2.getDiscount() << endl;
    cout << b2.getBookname() << "打折后的价格是:" << b2.getPrice() << ",书号是:" << b2.getISBN() << endl;

    system("pause");
    return 0;
}
复制代码

posted on   wshidaboss  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示