标签:取出 class 部分 ide cout 多态 turn 图片 ant
Quote3.h
#ifndef __QUOTE3_H__
#define __QUOTE3_H__
#include <iostream>
#include <memory>
class Quote{
public:
Quote() = default;
Quote(const std::string& book, double pri)
:bookNo(book), price(pri){}
std::string isbn() const{return bookNo;}
virtual double net_price(std::size_t n)const{
return n * price;
}
virtual void debug()const{
std::cout << bookNo << " " << price << std::endl;
}
virtual ~Quote() = default;
private:
std::string bookNo;
protected:
double price = 0.0;
};
class Disc_quote : public Quote{
public:
Disc_quote() = default;
Disc_quote(const std::string& book, double price,
std::size_t qyn, double disc):Quote(book, price),
quantity(qyn), discount(disc){}
double net_price(std::size_t) const override = 0;
protected:
std::size_t quantity = 0;//折扣适用的数量
double discount = 0.0; //折扣率
};
class Bulk_quote : public Disc_quote{
public:
Bulk_quote() = default;
Bulk_quote(const std::string& book, double price,
std::size_t qyn, double disc)
:Disc_quote(book, price, qyn, disc){}
double net_price(std::size_t) const override;
};
class Min_quote : public Disc_quote{
public:
Min_quote() = default;
Min_quote(const std::string& book, double price,
std::size_t qyn, double disc)
:Disc_quote(book, price, qyn, disc){}
double net_price(std::size_t) const override;
};
#endif
Quote3.cpp
#include "Quote3.h"
double Bulk_quote::net_price(std::size_t cnt) const{
if(cnt >= quantity){
return cnt * (1 - discount) * price;
}
else{
return cnt * price;
}
}
double Min_quote::net_price(std::size_t cnt) const{
if(cnt < quantity){
return cnt * (1 - discount) * price;
}
else{
return cnt * price;
}
}
main.cpp
#include "Quote3.h"
#include <vector>
double print_total(std::ostream& os,
const Quote& item, size_t n){
double ret = item.net_price(n);
os << "ISBN: " << item.isbn()
<< " # sold: " << n << " total due: " << ret << std::endl;
return ret;
}
int main(){
std::vector<Quote> qv;
for(unsigned i = 0; i != 10; ++i){
qv.push_back(Bulk_quote("01", 100, 10, 0.1));
}
double total = 0;
for(const auto s : qv){
total += s.net_price(20);
}
std::cout << total << std::endl;
std::cout << "-------------------------" << std::endl;
std::vector<std::shared_ptr<Quote>> sv;
for(unsigned i = 0; i != 10; ++i){
sv.push_back(std::make_shared<Bulk_quote>("01", 100, 10, 0.1));
}
double total1 = 0;
for(const auto s : sv){
total1 += s->net_price(20);
}
std::cout << total1 << std::endl;
}
20000
-------------------------
18000
标签:取出 class 部分 ide cout 多态 turn 图片 ant
原文地址:https://www.cnblogs.com/xiaoshiwang/p/10217087.html