定义一个类Book, 用来描述新书, 具有以下功能:
查看当前价格.
查看当前的书号
定义一个类SellBook, 用来表示促销的书籍, 要求继承自Book类
具有以下功能:
- 查看当前折扣
- 设置当前折扣
查看当前的促销价格
下面是我自编的代码
Book类
.h
#pragma once
#include <string>
using namespace std;
class Book
{
public:
Book();
~Book();
int getPrice();
string getIsbn();
protected:
int price;
string isbn;
};
.cpp
#include "Book.h"
Book::Book()
{
}
Book::~Book()
{
}
int Book::getPrice()
{
return price;
}
string Book::getIsbn()
{
return isbn;
}
ShellBook类
.h
#pragma once
#include "Book.h"
using namespace std;
class Shellbook :
public Book
{
public:
Shellbook(int price, string isbn, float discout);
float getDiscout();
float setDiscout(float discout);
int getPrice();
private:
float discout = 1.0;
};
.cpp
#include "Shellbook.h"
Shellbook::Shellbook(int price, string isbn, float discout)
{
this->price = price;
this->isbn = isbn;
this->discout = discout;
}
float Shellbook::getDiscout()
{
return discout;
}
float Shellbook::setDiscout(float discout)
{
return discout;
}
int Shellbook::getPrice()
{
this->price = price * discout / 10;
return price;
}
main
.cpp
#include "Book.h"
#include "Shellbook.h"
#include <iostream>
using namespace std;
int main(void) {
Book book;
Shellbook book1(98, "123456",5);
cout << "价格:" << book1.getPrice() << " ISBN:" << book1.getIsbn() << endl;
system("pause");
return 0;
}
标签:float,int,31,练习,C++,discout,Book,Shellbook,price
From: https://blog.csdn.net/m0_57667919/article/details/143207413