Verbesserungsvorschläge/ Frage zu Override



  • Hallo,
    meine Fragen beziehen sich beide auf der Member Funktion von advBulkQuote netPrice.
    1. Frage: Da ich ja einen Member verändere kann diese Funktion ja nicht const sein. Also kann ich auch nicht override hinter schreiben. Aber es funktioniert ja trotzdem ist das dann noch gut also vom Programmierstil?
    2. Frage: Die Aufgabe ist ja wenn ein Buch gekauft wird das bei einer bestimmten Anzahl es einen Rabatt gibt aber das nur für eine Limitierte Anzahl. Kann man das auch noch besser lösen? Wie würdet ihr das machen?

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    class Quote{
    public:
    	Quote()	= default;
    	Quote(const std::string &book, double salesPrice) : 
    				bookNo(book), price(salesPrice){}
    
    	virtual double netPrice(std::size_t n) const
    		{return n * price;}
    	virtual ~Quote() = default;
    	std::string getISBN() const
    		{return bookNo;}
    
    private:
    	std::string bookNo;
    protected:
    	double price = 0.0;
    };
    
    class bulkQuote : public Quote{
    public:
    	bulkQuote() = default;
    	bulkQuote(const std::string &book, double salesPrice, std::size_t min, double disc) :
    			Quote(book, salesPrice), minDisc(min), discount(disc){}
    
    	double netPrice(std::size_t n) const override{	
    		if(n >= minDisc)
    			return n * price * (1.0 - discount);
    		else
    			return n * price;
    	}
    
    protected:
    	double discount = 0.0;
    	std::size_t minDisc = 0;
    };
    
    class advBulkQuote: public bulkQuote{
    public:
    	advBulkQuote() = default;
    	advBulkQuote(const std::string &book, double salesPrice, std::size_t min, std::size_t max, double disc) :
    			bulkQuote(book, salesPrice, min, disc), maxDisc(max){}
    
    	double netPrice(std::size_t n){
    		std::size_t dis = std::min(n, maxDisc);
    		if(dis == n)
    			maxDisc -= n;
    		else
    			maxDisc = 0;
    
    		if(dis < minDisc){
    			return n * price;
    		} else {
    			std::size_t undis = n - dis;
    			return dis * (1 - discount) * price + 
    				undis * price;
    		}
    	}
    
    private:
    	std::size_t maxDisc = 0;
    };
    
    double printTotal(std::ostream &os, const Quote book, std::size_t n){
    	double total = book.netPrice(n);
    	std::cout << "ISBN: " << book.getISBN() << "\tsold " << n << " times total price: " << total << std::endl;
    	return total;;
    }
    
    int main()
    {
    	Quote book1;
    	bulkQuote bulk("Hello C++", 25, 3, 0.15);
    	Quote &book2 = bulk;
    	advBulkQuote book4("Advanced", 25, 3, 10, 0.2);
    
    	bulkQuote book3("Hello World", 25, 5, 0.10);
    
    	std::cout << book2.netPrice(4) << std::endl;
    	std::cout << book2.netPrice(5) << std::endl;
    	std::cout << book4.netPrice(5) << std::endl;
    	std::cout << book4.netPrice(10) << std::endl;
    	std::cout << book4.netPrice(5) << std::endl;
    	std::cout << book4.netPrice(5) << std::endl;
    
    	printTotal(std::cout, book2, 5);
    
    	return 0;
    }
    

    Würd mich freuen wenn ihr mir helfen könntet.


  • Mod

    Aber es funktioniert ja trotzdem

    Nein:

    §10.3/2 schrieb:

    If a virtual member function vf is declared in a class Base and in a class Derived , derived directly or indirectly from Base , a member function vf with the same name, parameter-type-list (8.3.5), cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf .

    Was du allerdings tun könntest ist maxDisc als mutable zu deklarieren - dann kannst du die Memberfunktion tatsächlich const machen. Ob das allerdings alles vom Design sinnvoll ist, ist eine andere Frage.



  • Ok Danke.


Anmelden zum Antworten