日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

【C++】容器与继承

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C++】容器与继承 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

容器與繼承

Exercise 15.28:

Define a vector to hold Quote objects but put Bulk_quote objects into that vector. Compute the total net_price of all the elements in the vector.

Exercise 15.29:

Repeat your program, but this time store shared_ptrs to objects of type Quote. Explain any discrepancy in the sum generated by the this version and the previous program.

由于前面練習中的vector包含對象,因此不存在多態性在調用虛函數net_price時發生。從本質上講,對象被推回去的Bulk_quote對象的Quote主題,因此,調用的虛擬net_price函數是Quote::net_price。作為一個結果,沒有任何折扣。結果是9090
本練習的對象是指向Quote對象的智能指針。在這個在這種情況下,多態性如期發生。被調用的實際虛函Bulk_quote::net_price確保折扣被應用。因此,結果是6363。可以發現,在價格的計算中采用了30%的折扣。如果沒有矛盾,解釋為什么沒有矛盾。
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include "quote.h"
#include "bulk_quote.h"
#include "limit_quote.h"
#include "disc_quote.h"
int main()
{/*** @brief ex15.28   outcome == 9090*/std::vector<Quote> v;for(unsigned i = 1; i != 10; ++i)v.push_back(Bulk_quote("sss", i * 10.1, 10, 0.3));double total = 0;for (const auto& b : v){total += b.net_price(20);}std::cout << total << std::endl;std::cout << "======================\n\n";
	 /*** @brief ex15.29   outccome == 6363*/std::vector<std::shared_ptr<Quote>> pv;for(unsigned i =1; i != 10; ++i)pv.push_back(std::make_shared<Bulk_quote>(Bulk_quote("sss", i * 10.1, 10, 0.3)));double total_p = 0;for (auto p : pv){total_p +=  p->net_price(20);}std::cout << total_p << std::endl;return 0;
}

basket類

Exercise 15.30: 編寫自己的Basket類版本,并使用它計算在前面練習中使用的相同交易的價格。

#ifndef BASKET_H
#define BASKET_H#include "quote.h"
#include <set>
#include <memory>// a basket of objects from Quote hierachy, using smart pointers.
class Basket
{
public:// copy verisonvoid add_item(const Quote& sale){ items.insert(std::shared_ptr<Quote>(sale.clone())); }// move versionvoid add_item(Quote&& sale){ items.insert(std::shared_ptr<Quote>(std::move(sale).clone())); }double total_receipt(std::ostream& os) const;private:// function to compare needed by the multiset memberstatic bool compare(const std::shared_ptr<Quote>& lhs,const std::shared_ptr<Quote>& rhs){ return lhs->isbn() < rhs->isbn(); }// hold multiple quotes, ordered by the compare memberstd::multiset<std::shared_ptr<Quote>, decltype(compare)*>items{ compare };
};#endif // BASKET_H
#include "basket.h"
double Basket::total_receipt(std::ostream &os) const
{double sum = 0.0;for(auto iter = items.cbegin(); iter != items.cend();iter = items.upper_bound(*iter))//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^// @note   this increment moves iter to the first element with key//         greater than  *iter.{sum += print_total(os, **iter, items.count(*iter));}                                   // ^^^^^^^^^^^^^ using count to fetch// the number of the same book.os << "Total Sale: " << sum << std::endl;return sum;
}
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <fstream>
#include "quote.h"
#include "bulk_quote.h"
#include "limit_quote.h"
#include "disc_quote.h"
#include "basket.h"int main()
{Basket basket;for (unsigned i = 0; i != 10; ++i)basket.add_item(Bulk_quote("Bible", 20.6, 20, 0.3));for (unsigned i = 0; i != 10; ++i)basket.add_item(Bulk_quote("C++Primer", 30.9, 5, 0.4));for (unsigned i = 0; i != 10; ++i)basket.add_item(Quote("CLRS", 40.1));std::ofstream log("log.txt", std::ios_base::app|std::ios_base::out);basket.total_receipt(log);return 0;
}

總結

以上是生活随笔為你收集整理的【C++】容器与继承的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。