Skip to content

Commit

Permalink
finish ch15
Browse files Browse the repository at this point in the history
  • Loading branch information
applenob committed Feb 24, 2019
1 parent 4d91253 commit 16572fe
Show file tree
Hide file tree
Showing 32 changed files with 1,876 additions and 16 deletions.
13 changes: 13 additions & 0 deletions cpp_source/ch15/ex_15_26/bulk_quote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "bulk_quote.h"

double Bulk_quote::net_price(std::size_t n) const
{
return n * price * ( n >= quantity ? 1 - discount : 1);
}

void Bulk_quote::debug() const
{
std::cout //<< "data members of this class:\n"
<< "min_qty= " << quantity << " "
<< "discount= " << this->discount<< " \n";
}
54 changes: 54 additions & 0 deletions cpp_source/ch15/ex_15_26/bulk_quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef BULK_QUOTE_H
#define BULK_QUOTE_H
#include "disc_quote.h"

class Bulk_quote : public Disc_quote
{

public:
Bulk_quote() { std::cout << "default constructing Bulk_quote\n"; }
Bulk_quote(const std::string& b, double p, std::size_t q, double disc) :
Disc_quote(b, p, q, disc) { std::cout << "Bulk_quote : constructor taking 4 parameters\n"; }

// copy constructor
Bulk_quote(const Bulk_quote& bq) : Disc_quote(bq)
{ std::cout << "Bulk_quote : copy constructor\n"; }

// move constructor
//page 535, " In a constructor, noexcept appears between the parameter list and the : that begins the constructor initializer list"
Bulk_quote(Bulk_quote&& bq) noexcept : Disc_quote(std::move(bq))
{
std::cout << "Bulk_quote : move constructor\n";
}

// copy =()
Bulk_quote& operator =(const Bulk_quote& rhs)
{
Disc_quote::operator =(rhs);
std::cout << "Bulk_quote : copy =()\n";

return *this;
}


// move =()
Bulk_quote& operator =(Bulk_quote&& rhs) noexcept
{
Disc_quote::operator =(std::move(rhs));
std::cout << "Bulk_quote : move =()\n";

return *this;
}

double net_price(std::size_t n) const override;
void debug() const override;

~Bulk_quote() override
{
std::cout << "destructing Bulk_quote\n";
}
};



#endif // BULK_QUOTE_H
1 change: 1 addition & 0 deletions cpp_source/ch15/ex_15_26/disc_quote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "disc_quote.h"
79 changes: 79 additions & 0 deletions cpp_source/ch15/ex_15_26/disc_quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef DISC_QUOTE_H
#define DISC_QUOTE_H

#include "quote.h"
class Disc_quote : public Quote
{
friend bool operator !=(const Disc_quote& lhs, const Disc_quote& rhs);
public:
Disc_quote() { std::cout << "default constructing Disc_quote\n"; }

Disc_quote(const std::string& b, double p, std::size_t q, double d) :
Quote(b, p), quantity(q), discount(d)
{
std::cout << "Disc_quote : constructor taking 4 parameters.\n";
}

// copy constructor
Disc_quote(const Disc_quote& dq) :
Quote(dq), quantity(dq.quantity), discount(dq.discount)
{
std::cout << "Disc_quote : copy constructor.\n";
}

// move constructor
Disc_quote(Disc_quote&& dq) noexcept :
Quote(std::move(dq)), quantity(std::move(dq.quantity)), discount(std::move(dq.discount))
{
std::cout << "Disc_quote : move constructor.\n";
}

// copy =()
Disc_quote& operator =(const Disc_quote& rhs)
{
Quote::operator =(rhs);
this->quantity = rhs.quantity;
this->discount = rhs.discount;

std::cout << "Disc_quote : copy =()\n";

return *this;
}

// move =()
Disc_quote& operator =(Disc_quote&& rhs) noexcept
{
if (*this != rhs)
{
Quote::operator =(std::move(rhs));
this->quantity = std::move(rhs.quantity);
this->discount = std::move(rhs.discount);
}
std::cout << "Disc_quote : move =()\n";

return *this;
}

virtual double net_price(std::size_t n) const override = 0;

~Disc_quote()
{
std::cout << "destructing Dis_quote\n";
}

protected:
std::size_t quantity = 3;
double discount = 0.0;
};

bool inline
operator !=(const Disc_quote& lhs, const Disc_quote& rhs)
{
return Quote(lhs) != Quote(rhs)
&&
lhs.quantity != rhs.quantity
&&
lhs.discount != rhs.discount;
}

#endif // DISC_QUOTE_H
9 changes: 9 additions & 0 deletions cpp_source/ch15/ex_15_26/limit_quote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "limit_quote.h"


void Limit_quote::debug() const
{
std::cout //<< "data members of this class:\n"
<< "max_qty= " << this->quantity << " "
<< "discount= " << this->discount<< " \n";
}
19 changes: 19 additions & 0 deletions cpp_source/ch15/ex_15_26/limit_quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef LIMIT_QUOTE_H
#define LIMIT_QUOTE_H

#include "disc_quote.h"

class Limit_quote : public Disc_quote
{
public:
Limit_quote() = default;
Limit_quote(const std::string& b, double p, std::size_t max, double disc):
Disc_quote(b, p, max, disc) { }

double net_price(std::size_t n) const override
{ return n * price * (n < quantity ? 1 - discount : 1 ); }

void debug() const override;
};

#endif // LIMIT_QUOTE_H
18 changes: 18 additions & 0 deletions cpp_source/ch15/ex_15_26/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <string>

#include "quote.h"
#include "bulk_quote.h"
#include "limit_quote.h"
#include "disc_quote.h"


int main()
{
Bulk_quote bq1;
Bulk_quote bq2("ss", 2.05, 12, 0.3);
bq2 = std::move(bq2);


return 0;
}
8 changes: 8 additions & 0 deletions cpp_source/ch15/ex_15_26/quote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "quote.h"

void Quote::debug() const
{
std::cout //<< "data members of this class:\n"
<< "bookNo= " <<this->bookNo << " "
<< "price= " <<this->price<< " \n";
}
73 changes: 73 additions & 0 deletions cpp_source/ch15/ex_15_26/quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef QUOTE_H
#define QUOTE_H

#include <string>
#include <iostream>

class Quote
{
friend bool operator !=(const Quote& lhs, const Quote& rhs);
public:
Quote() { std::cout << "default constructing Quote\n"; }
Quote(const std::string &b, double p) :
bookNo(b), price(p) { std::cout << "Quote : constructor taking 2 parameters\n"; }

// copy constructor
Quote(const Quote& q) : bookNo(q.bookNo), price(q.price)
{ std::cout << "Quote: copy constructing\n"; }

// move constructor
Quote(Quote&& q) noexcept : bookNo(std::move(q.bookNo)), price(std::move(q.price))
{ std::cout << "Quote: move constructing\n"; }

// copy =
Quote& operator =(const Quote& rhs)
{
if(*this != rhs)
{
bookNo = rhs.bookNo;
price = rhs.price;
}
std::cout << "Quote: copy =() \n";

return *this;
}

// move =
Quote& operator =(Quote&& rhs) noexcept
{
if(*this != rhs)
{
bookNo = std::move(rhs.bookNo);
price = std::move(rhs.price);
}
std::cout << "Quote: move =!!!!!!!!! \n";

return *this;
}

std::string isbn() const { return bookNo; }
virtual double net_price(std::size_t n) const { return n * price; }
virtual void debug() const;

virtual ~Quote()
{
std::cout << "destructing Quote\n";
}

private:
std::string bookNo;

protected:
double price = 10.0;
};

bool inline
operator !=(const Quote& lhs, const Quote& rhs)
{
return lhs.bookNo != rhs.bookNo
&&
lhs.price != rhs.price;
}

#endif // QUOTE_H
Loading

0 comments on commit 16572fe

Please sign in to comment.