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;
}
分类:
C++基础第一卷
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探