Skip to content

Instantly share code, notes, and snippets.

@paranoiacblack
Last active December 11, 2015 11:28
Show Gist options
  • Save paranoiacblack/4593372 to your computer and use it in GitHub Desktop.
Save paranoiacblack/4593372 to your computer and use it in GitHub Desktop.
Class solutions to SI Lab Session 2 for CS12 Winter 2013
These are the solutions to the lab on Friday, Jan 18th.
#include "book.h"
#include <iostream>
using namespace std;
Book::Book()
: number_of_pages(0), current_page(0), ISBN(0), is_hardcover(false) {
}
Book::Book(const string& title, const string& author, unsigned number_of_pages,
long int ISBN)
: number_of_pages(number_of_pages), current_page(0), title(title),
author(author), ISBN(ISBN), is_hardcover(false) {
}
Book::Book(const string& title, const string& author, unsigned number_of_pages,
long int ISBN, const string& genre, bool is_hardcover)
: number_of_pages(number_of_pages), current_page(0), title(title),
author(author), ISBN(ISBN), genre(genre), is_hardcover(is_hardcover) {
}
unsigned Book::get_number_of_pages() const {
return number_of_pages;
}
unsigned Book::get_current_page() const {
return current_page;
}
string Book::get_title() const {
return title;
}
string Book::get_author() const {
return author;
}
long int Book::get_isbn() const {
return ISBN;
}
string Book::get_genre() const {
return genre;
}
bool Book::has_hardcover() const {
return is_hardcover;
}
void Book::set_number_of_pages(unsigned number_of_pages) {
this->number_of_pages = number_of_pages;
}
void Book::set_current_page(unsigned current_page) {
this->current_page = current_page;
}
void Book::set_title(const string& title) {
this->title = title;
}
void Book::set_author(const string& author) {
this->author = author;
}
void Book::set_isbn(long int ISBN) {
this->ISBN = ISBN;
}
void Book::set_genre(const string& genre) {
this->genre = genre;
}
void Book::set_hardcover(bool is_hardcover) {
this->is_hardcover = is_hardcover;
}
void Book::print() const {
cout << title << " by " << author << " (ISBN #): " << ISBN << endl;
if (genre.size() != 0) {
cout << "Genre: " << genre << " | ";
}
cout << "Book length: " << number_of_pages << " page(s)" << endl;
if (is_hardcover) {
cout << "This book has a hardcover." << endl;
} else {
cout << "This book does not have a hardcover." << endl;
}
}
void Book::turn_page() {
// Book will turn back to beginning turn page is called on last page.
set_current_page((current_page + 1) % number_of_pages);
}
void Book::turn_to_page(unsigned page) {
if (page > number_of_pages) {
cout << "Looks like you tried to turn to a page not in this book. So we set"
"you to the very last page" << endl;
set_current_page(number_of_pages);
} else {
set_current_page(page);
}
}
#ifndef __BOOK_H__
#define __BOOK_H__
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
unsigned number_of_pages;
unsigned current_page;
string title;
string author;
long int ISBN;
string genre;
bool is_hardcover;
public:
Book();
Book(const string& title, const string& author, unsigned number_of_pages,
long int ISBN);
Book(const string& title, const string& author, unsigned number_of_pages,
long int ISBN, const string& genre, bool is_hardcover);
unsigned get_number_of_pages() const;
unsigned get_current_page() const;
string get_title() const;
string get_author() const;
long int get_isbn() const;
string get_genre() const;
bool has_hardcover() const;
void set_number_of_pages(unsigned);
void set_current_page(unsigned);
void set_title(const string&);
void set_author(const string&);
void set_isbn(long int);
void set_genre(const string&);
void set_hardcover(bool);
void print() const;
void turn_page();
void turn_to_page(unsigned page);
};
#endif
#include "library.h"
Library::Library() {
}
Library::Library(const vector<Book>& books)
: books(books) {
}
unsigned Library::size() const {
return books.size();
}
Book Library::first() const {
return books.at(0);
}
Book Library::at(unsigned i) const {
return books.at(i);
}
bool Library::book_exists(const string& title) const {
for (unsigned i = 0; i < books.size(); ++i) {
if (books.at(i).get_title() == title) {
return true;
}
}
return false;
}
void Library::add_book(const Book& book) {
books.push_back(book);
}
void Library::remove_book(const string& title) {
if (book_exists(title)) {
for (unsigned i = 0; i < books.size(); ++i) {
if (books.at(i).get_title() == title) {
swap(books.at(i), books.at(books.size() - 1));
break;
}
}
}
}
void Library::print() const {
for (unsigned i = 0; i < books.size(); ++i) {
cout << "=====================================" << endl;
books.at(i).print();
cout << "=====================================" << endl;
}
}
#ifndef __LIBRARY_H__
#define __LIBRARY_H__
#include "book.h"
#include <vector>
using namespace std;
class Library {
private:
vector<Book> books;
public:
Library();
Library(const vector<Book>& books);
unsigned size() const;
Book first() const;
Book at(unsigned i) const;
bool book_exists(const string& title) const;
void add_book(const Book& book);
void remove_book(const string& title);
void print() const;
};
#endif
#include "library.h"
#include "rectangle.h"
int main() {
Point lower_left(5, 4);
Rectangle rect(lower_left, 3.5, 6);
Book hamlet("Hamlet", "William Shakespeare", 432, 9780140707342, "Drama",
false);
Library personal_library;
personal_library.add_book(hamlet);
personal_library.print();
return 0;
}
CXX=g++
CXXFLAGS=-g -W -Wall -Werror
OBJECTS=book.o library.o point.o rectangle.o
MAIN=main.cpp
all: $(OBJECTS)
$(CXX) $(CXXFLAGS) $(MAIN) $(OBJECTS)
rectangle.o: point.o
$(CXX) $(CXXFLAGS) -c rectangle.cpp -o rectangle.o
point.o:
$(CXX) $(CXXFLAGS) -c point.cpp -o point.o
library.o: book.o
$(CXX) $(CXXFLAGS) -c library.cpp -o library.o
book.o:
$(CXX) $(CXXFLAGS) -c book.cpp -o book.o
clean:
rm -rf *.o a.out
#include "point.h"
#include <iostream>
// Always a good idea to define your functions in the order they are declared.
// It makes it really easy to navigate for people looking for implementation.
// Constructors
Point::Point()
: x(0.0), y(0.0) {
}
Point::Point(double x, double y)
: x(x), y(y) {
}
// Accessors
double Point::get_x() const {
return x;
}
double Point::get_y() const {
return y;
}
// Mutators
void Point::set_x(double x) {
this->x = x;
}
void Point::set_y(double y) {
this->y = y;
}
// Conveniences
void Point::pretty_print() const {
std::cout << "(" << x << "," << y << ")" << std::endl;
}
#ifndef __POINT_H__
#define __POINT_H__
// Always include header guards. They allow the compiler to not accidentally
// include the same file over and over again, which would cause many errors.
class Point {
private:
double x;
double y;
public:
// Constructors and Destructors
Point();
Point(double x, double y);
// Accessors
double get_x() const;
double get_y() const;
// Mutators
void set_x(double x);
void set_y(double y);
// Conveniences
void pretty_print() const;
};
#endif
#include "rectangle.h"
// Constructors
// Point already has it's own default constructor, so it will be called when
// lower_left is constructed.
Rectangle::Rectangle()
: width(0.0), height(0.0) {
}
Rectangle::Rectangle(const Point& lower_left, double width, double height)
: lower_left(lower_left), width(width), height(height) {
}
// We can call the Point constructor, btw.
Rectangle::Rectangle(double x, double y, double width, double height)
: lower_left(Point(x,y)), width(width), height(height) {
}
// Accessors
Point Rectangle::get_lower_left() const {
return lower_left;
}
double Rectangle::get_width() const {
return width;
}
double Rectangle::get_height() const {
return height;
}
// Mutators
void Rectangle::set_lower_left(const Point& lower_left) {
this->lower_left = lower_left;
}
void Rectangle::set_width(double width) {
this->width = width;
}
void Rectangle::set_height(double height) {
this->height = height;
}
#ifndef __RECTANGLE_H__
#define __RECTANGLE_H__
#include "point.h"
// This Rectangle is represented as a lower left point with an accompanying
// width and height. It is simpler than storing four distinct points because
// doing calculations like checking if objects collide are easier.
class Rectangle {
private:
// We made a Point type, might as use it.
Point lower_left;
double width;
double height;
public:
// Constructors
Rectangle();
// Remember, in function declarations, we don't need variable names.
Rectangle(const Point&, double, double);
Rectangle(double x, double y, double width, double height);
// Accessors
// New types aren't any different than known types, for the most part.
// For instance, you can return them from any functions.
Point get_lower_left() const;
double get_width() const;
double get_height() const;
// Mutators
// However, realize that user defined types, because they are composed of
// other types, are sometimes significantly larger than ints or double.
// They could have multiple large vectors and strings as member data. So we
// always pass our defined datatypes by reference.
void set_lower_left(const Point&);
// Doesn't make sense to pass double by const& since it doesn't save anything.
void set_width(double);
void set_height(double);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment