Created
September 22, 2015 15:09
-
-
Save krysseltillada/4e62f95557d051a91494 to your computer and use it in GitHub Desktop.
inherited constructors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Quote.h" | |
int main () | |
{ | |
Bulk_quote b1("null", 00, 22, 33); | |
std::cout << b1 << std::endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#ifndef QUOTE_H | |
#define QUOTE_H | |
#include <iostream> /// std::cout, std::ostream | |
#include <utility> /// std::pair, std::move | |
class Quote { | |
public: | |
Quote () = default; | |
Quote (const std::string &b, double d) : /// constructor that takes two arguments one is string and second is a double | |
bookNo (b), price (d) { } | |
Quote (const Quote &c_obj) : /// constructor that copies the same type of an object and copies it to the new object | |
bookNo (c_obj.bookNo), price (c_obj.price) { | |
std::cout << "quote copy constructor " << std::endl; | |
} | |
Quote (Quote &&m_obj) : /// constructor that moves the same type of an object and calls the destructor of the old object and moves the data into the new object | |
bookNo (m_obj.bookNo), price (m_obj.price) { /// which is *this object | |
std::cout << "quote move constructor " << std::endl; | |
} | |
virtual ~Quote () { } /// define as virtual to expects its derived classes to define their own destructors or override | |
Quote &operator = (const Quote &rho) { /// operator function that assigns an object to the newly object and returns *this | |
std::cout << "Quote copy assignment operator " << std::endl; | |
this->bookNo = rho.bookNo; | |
this->price = rho.price; | |
return *this; | |
} | |
Quote &operator = (Quote &&rho) { /// operator function that moves an object and to the new object and destroy's the old object | |
std::cout << "Quote move assignment operator " << std::endl; | |
if (this != &rho) { /// it check first if their not the same object if their the same we cancel the moving and returns *this | |
this->bookNo = rho.bookNo; | |
this->price = rho.price; | |
} | |
return *this; | |
} | |
std::string isbn () const { /// gets the bookNo member of the Quote class | |
return bookNo; | |
} | |
virtual double net_price (size_t n) const { /// defines it as virtual to expects its derived class to override and defines its own version | |
std::cout << "quote" << std::endl; | |
return price * n; | |
} | |
virtual void debug () const { /// just for debugging purposes | |
std::cout << "bookNo: " << bookNo << std::endl | |
<< "price : " << price << std::endl; | |
} | |
private: | |
std::string bookNo; /// subobject, data member of Quote | |
protected: | |
double price = 0.0; /// same | |
}; | |
class Disc_quote : public Quote { /// Disc_quote inherits public members of the base class which is Quote | |
public: | |
Disc_quote () = default; /// generates a synthesized version of a constructor | |
Disc_quote (const std::string &book, double price, /// sets an constructor that takes a string, double, unsigned, and double | |
std::size_t qty, double disc) : | |
Quote (book, price), quantity (qty), discount (disc) { | |
std::cout << "Disc_quote constructor" << std::endl; | |
} | |
Disc_quote (const Disc_quote &c_obj) : /// same operations as the Quote class copy constructor | |
Quote (c_obj), quantity (c_obj.quantity), discount (c_obj.discount) { | |
std::cout << "Disc_quote copy constructor " << std::endl; | |
} | |
Disc_quote (Disc_quote &&m_obj) : /// same operations as the Quote class move constructor | |
Quote (std::move (m_obj)), quantity (m_obj.quantity), discount (m_obj.discount) { | |
std::cout << "Disc_quote move constructor " << std::endl; | |
} | |
~Disc_quote () { } /// overrides the version of the destructor that inherited from Quote | |
Disc_quote &operator = (const Disc_quote &rho) { /// same operations as Quote::operator = () | |
std::cout << "Disc quote copy assignment operator " << std::endl; | |
Quote::operator = (rho); /// calls the Quote::operator = () in the base class because im too laze to write some code :p | |
this->quantity = rho.quantity; | |
this->discount = rho.discount; | |
return *this; | |
} | |
Disc_quote &operator = (Disc_quote &&rho) { /// same operations as Quote::operator = () but calls the Quote::operator = (&&) | |
std::cout << "Disc quote move assignment operator " << std::endl; | |
if (this != &rho) { | |
Quote::operator = (std::move (rho)); | |
this->quantity = rho.quantity; | |
this->discount = rho.discount; | |
} | |
return *this; | |
} | |
double net_price (std::size_t) const = 0; /// defines this class as abstract | |
std::pair <size_t, double> discount_policy () const { /// returns a pair of data name quantity and discount | |
return {quantity, discount}; | |
} | |
protected: | |
std::size_t quantity = 0; /// data members of Disc_quote class | |
double discount = 0.0; ///+++ | |
}; | |
class Bulk_quote : public Disc_quote { //// Bulk_quote inherited public members of Disc_quote | |
friend std::ostream &operator << (std::ostream &os, const Bulk_quote &rho) { /// defines a operator function that uses the std::ostream for printing purposes | |
os << rho.isbn () << std::endl | |
<< rho.price << std::endl | |
<< rho.quantity << std::endl | |
<< rho.discount << std::endl; | |
return os; /// and returns the state of the stream | |
} | |
public: | |
Bulk_quote () = default; /// synthesize a default constructor | |
using Disc_quote::Disc_quote; /// inherits all the Disc_quote constructor | |
~Bulk_quote () { } /// overrides a destructor | |
Bulk_quote (const Bulk_quote &c_obj) : /// same operations | |
Disc_quote (c_obj) { | |
std::cout << "bulk_quote copy constructor" << std::endl; | |
} | |
Bulk_quote (Bulk_quote &&m_obj) : /// same operations | |
Disc_quote (std::move (m_obj)) { | |
std::cout << "bulk_quote move constructor " << std::endl; | |
} | |
Bulk_quote &operator = (const Bulk_quote &rho) { /// same operations but we call the Disc_quote::operator = () | |
std::cout << "bulk quote copy assignment operator " << std::endl; | |
Disc_quote::operator = (rho); | |
return *this; | |
} | |
Bulk_quote &operator = (Bulk_quote &&rho) { /// same operations but we call the Disc_quote::operator = () | |
std::cout << "bulk quote move assignment operator " << std::endl; | |
if (this != &rho) { | |
Disc_quote::operator = (std::move (rho)); | |
} | |
return *this; | |
} | |
double net_price (std::size_t n ) const override { /// override the inherited function from Disc_quote and defines its own version | |
if (n >= quantity) | |
return n * (1 - discount) * price; | |
else | |
return n * price; | |
} | |
}; | |
#endif | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment