Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created September 26, 2015 18:50
Show Gist options
  • Save krysseltillada/773a87d825b768cf64a2 to your computer and use it in GitHub Desktop.
Save krysseltillada/773a87d825b768cf64a2 to your computer and use it in GitHub Desktop.
Class headers and implementations
#include "Author.h"
std::ostream& operator << (std::ostream &os, const Author &a) {
os << a.Email << " " << a.Name << " " << a.Gender;
return os;
}
Author::Author (const std::string &n, const std::string &e, const char &g) :
Name (n), Email (e), Gender (g) { }
Author::~Author () {
}
std::string Author::getName () const {
return this->Name;
}
std::string Author::getEmail () const {
return this->Email;
}
char Author::getGender () const {
return this->Gender;
}
void Author::setEmail (const std::string &e) {
this->Email = e;
}
#ifndef AUTHOR_HEADER
#define AUTHOR_HEADER
#include <iostream>
class Author {
friend std::ostream& operator << (std::ostream &, const Author &);
public:
Author (const std::string &, const std::string &, const char &);
~Author ();
std::string getName () const;
std::string getEmail () const;
char getGender () const;
void setEmail (const std::string &);
private:
std::string Name;
std::string Email;
char Gender;
};
#endif // AUTHOR_HEADER
#include "Book.h"
std::ostream &operator << (std::ostream &os, const Book &b) {
os << b.Name << " " << b.author.getName() << " " << b.getPrice() << " " << b.getQtyInStock();
return os;
}
Book::Book (const std::string &n, const Author &a, const double &p) :
Name (n), author (a.getName(), a.getEmail (), a.getGender ()), price (p) { }
Book::Book (const std::string &n, const Author &a, const double &p,
const int &qty) : Name (n), author (a.getName (), a.getEmail (), a.getGender ()), price (p), qtyInStock (qty) { }
Book::~Book () {
}
std::string Book::getName () const {
return this->Name;
}
std::string Book::getAuthor () const {
return this->author.getName ();
}
double Book::getPrice () const {
return this->price;
}
int Book::getQtyInStock () const {
return this->qtyInStock;
}
void Book::setPrice (const double &d) {
this->price = d;
}
void Book::setQtyStock (const int &qty) {
this->qtyInStock = qty;
}
#ifndef BOOK_HEADER
#define BOOK_HEADER
#include "Author.h"
class Book {
friend std::ostream &operator << (std::ostream &, const Book &);
public:
Book (const std::string &, const Author &, const double &);
Book (const std::string &, const Author &, const double &,
const int &);
~Book ();
std::string getName () const;
std::string getAuthor () const;
double getPrice () const;
int getQtyInStock () const;
void setPrice (const double &);
void setQtyStock (const int &);
private:
std::string Name;
Author author;
double price;
int qtyInStock = 0;
};
#endif // BOOK_HEADER
#include "Circle.hpp"
Circle::Circle (const double &r, const std::string &c, const bool &f) :
Shape (c, f), radius (r) { }
double Circle::getRadius () {
return this->radius;
}
double Circle::getArea () {
return this->PI * (this->radius * this->radius);
}
double Circle::getPerimeter () {
return 2 * this->PI * this->radius;
}
void Circle::setRadius (const double &r) {
this->radius = r;
}
#ifndef CIRCLE_HEADER
#define CIRCLE_HEADER
#include "Shape.hpp"
class Circle : protected Shape {
public:
Circle () = default;
Circle (const double &, const std::string &, const bool &);
double getRadius ();
double getArea ();
double getPerimeter ();
void setRadius (const double &);
private:
double radius;
static constexpr double PI = 3.14159265359;
};
#endif // CIRCLE_HEADER
#include <memory>
#include "Book.h"
#include "MyPoint.hpp"
#include "Circle.hpp"
#include "Rectangle.hpp"
#include "Square.hpp"
void free (std::initializer_list <MyPoint *> mem) {
for (std::initializer_list <MyPoint *>::iterator it = mem.begin (); it != mem.end (); ++it)
delete *it;
}
int main ()
{
/*
Book *aBook = new Book ("c++ primer", Author ("stanley lippman", "[email protected]", 'm'), 19.95, 1000);
std::cout << aBook->getName () << std::endl
<< aBook->getAuthor() << std::endl
<< aBook->getPrice() << std::endl
<< aBook->getQtyInStock() << std::endl;
delete aBook;
*/
/*
MyPoint *p1 = new MyPoint (), *p2 = new MyPoint ();
p1->setX(4);
p1->setY(222);
p2->setX(23);
p2->setY(42);
std::cout << p1->Distance(*p2) << std::endl;
free ({p1, p2});
*/
/*
std::shared_ptr <Circle> circle_sp = std::make_shared <Circle> (20, "blue", true);
std::cout << circle_sp->getPerimeter () << std::endl;
*/
/*
std::shared_ptr <Rectangle> box_sp = std::make_shared <Rectangle> (20, 10, "blue", true);
std::cout << box_sp->getArea () << std::endl
<< box_sp->getPerimeter () << std::endl;
*/
std::unique_ptr <Square> crate_sp (new Square (20, "Red", true));
std::cout << crate_sp->getArea () << std::endl
<< crate_sp->getPerimeter () << std::endl
<< crate_sp->getSide () << std::endl
<< crate_sp->getLength () << std::endl
<< crate_sp->getWidth () << std::endl;
return 0;
}
#include "MyPoint.hpp"
MyPoint::MyPoint (const int &ix, const int &iy) :
x (ix), y (iy) { }
int MyPoint::getX () const {
return this->x;
}
int MyPoint::getY () const {
return this->y;
}
void MyPoint::setX (const int &px) {
this->x = px;
}
void MyPoint::setY (const int &py) {
this->y = py;
}
void MyPoint::setXY (const int &px, const int &py) {
this->x = px;
this->y = py;
}
double MyPoint::Distance (const int &px, const int &py) {
MyPoint point1;
point1.x = this->x - px;
point1.y = this->y - py;
return sqrt ( ( point1.x * point1.x ) + ( point1.y * point1.y) );
}
double MyPoint::Distance (const MyPoint &p) {
MyPoint point2;
point2.x = this->x - p.x;
point2.y = this->y - p.y;
return sqrt ( ( point2.x * point2.x ) + ( point2.y * point2.y ) );
}
#ifndef MYPOINT_HEADER
#define MYPOINT_HEADER
#include <cmath>
class MyPoint {
public:
MyPoint () = default;
MyPoint (const int &, const int &);
int getX () const;
int getY () const;
void setX (const int &);
void setY (const int &);
void setXY (const int &, const int &);
double Distance (const int &, const int &);
double Distance (const MyPoint &);
private:
int x;
int y;
};
#endif // MYPOINT_HEADER
#include "Rectangle.hpp"
Rectangle::Rectangle (const double &l, const double &w) :
length (l), width (w) { }
Rectangle::Rectangle (const double &l, const double &w, const std::string &c,
const bool &f) : Shape (c, f), length (l), width (w) { }
double Rectangle::getWidth () const {
return this->width;
}
double Rectangle::getLength () const {
return this->length;
}
double Rectangle::getArea () { ///
return this->length * this->width;
}
double Rectangle::getPerimeter () {
return 2 * (this->length + this->width);
}
void Rectangle::setLength (const double &l) {
this->length = l;
}
void Rectangle::setWidth (const double &w) {
this->width = w;
}
#ifndef RECTANGLE_HEADER
#define RECTANGLE_HEADER
#include "Shape.hpp"
class Rectangle : protected Shape {
public:
Rectangle () = default;
Rectangle (const double &, const double &);
Rectangle (const double &, const double &, const std::string &,
const bool &);
double getWidth () const;
double getLength () const;
virtual double getArea ();
virtual double getPerimeter ();
virtual void setWidth (const double &);
virtual void setLength (const double &);
protected:
double length;
double width;
};
#endif // RECTANGLE_HEADER
#include "Shape.hpp"
Shape::Shape () :
color ("red"), filled (true) { }
Shape::Shape (const std::string &c, const bool &f) :
color (c), filled (f) { }
std::string Shape::getColor () const {
return this->color;
}
void Shape::setColor (const std::string &c) {
this->color = c;
}
void Shape::setFilled (const bool &f) {
this->filled = f;
}
bool Shape::isFilled () const {
return this->filled;
}
#ifndef SHAPE_HEADER
#define SHAPE_HEADER
#include <iostream>
class Shape {
public:
Shape ();
Shape (const std::string &, const bool &);
virtual ~Shape () = default;
std::string getColor () const;
void setColor (const std::string &);
void setFilled (const bool &);
bool isFilled () const;
protected:
std::string color;
bool filled;
};
#endif // SHAPE_HEADER
#include "Square.hpp"
/*
class Square : protected Rectangle {
public:
Square () = default;
Square (const double &);
Square (const double &, const std::string &, const bool &);
double getSide () const;
double getArea () override;
double getPerimeter () override;
virtual void setWidth (const double &) override;
virtual void setLength (const double &) override;
void setSide (const double &);
};
*/
Square::Square (const double &s) :
Rectangle (s, s), Side (s) { }
Square::Square (const double &s, const std::string &c, const bool &f) :
Rectangle (s, s, c, f), Side (s) { }
double Square::getSide () {
return Side;
}
double Square::getArea () {
return this->getSide () * this->getSide ();
}
double Square::getPerimeter () {
return 4 * Side;
}
void Square::setWidth (const double &w) {
this->width = w;
}
void Square::setLength (const double &l) {
this->length = l;
}
void Square::setSide (const double &s) {
Rectangle (s, s);
this->Side = s;
}
#ifndef SQUARE_HEADER
#define SQUARE_HEADER
#include "Rectangle.hpp"
class Square : public Rectangle {
public:
Square () = default;
Square (const double &);
Square (const double &, const std::string &, const bool &);
double getSide ();
double getArea () override;
double getPerimeter () override;
void setWidth (const double &) override;
void setLength (const double &) override;
void setSide (const double &);
private:
double Side;
};
#endif // SQUARE_HEADER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment