基类和派生类文件链接
文件Item_base.hpp
程序代码:
class Item_base{ public: Item_base(const std::string &book="",double sales_price=0.0):isbn(book),price(sales_price){ } std::string book()const{ return isbn; } virtual double net_price(std::size_t n)const { return n*price; } virtual ~Item_base(){ } private: std::string isbn; protected: double price; }; class Bulk_item:public Item_base{ public : Bulk_item(const std::string &book="",double sales_price=0.0,size_t qty,double dis): Item_base(book,price),min_qty(qty),discount(dis){ } double net_price(std::size_t)const; private: std::size_t min_qty; double discount; }; double Bulk_item::net_price(size_t cnt)const{ if(cnt>=min_qty) return cnt*(1-discount)*price; else return cnt*price; };
源文件:
程序代码:
#include<iostream> #include<string> #include<vector> #include"Item_base.hpp" using namespace std; int main(){ vector<Item_base>itemvec; string isbn; double price,qty,discount; cout<<"Enter some Bulk_item objects"<<endl; while(cin>>isbn>>price>>qty>>discount){ itemvec.push_back(Bulk_item(isbn,price,qty,discount)); } double sum=0.0; for(vector<Item_base>::iterator iter=itemvec.begin(); iter!=itemvec.end();++iter) sum+=iter->net_price(100); cout<<sum<<endl; return 0; }
为什么输入数据后 会输出 NAN 。。
;另外派生类 和基类 文件怎么链接