Revision on C++ Pointers

Download Report

Transcript Revision on C++ Pointers

Lecture 1 & 2 – Case Study
TCP1201: 2013/2014
Case Study – Book Store System
You are assigned to create a simple system for a
bookstore that keeps track of the stock of the books.
The system should be able to:



a)
b)
Store the following details; title, author (assume only one
author for each book), price and number-in-stock for a
particular book title.
Store the following details; name, email, gender for a
particular author.
Allow the details of the books and authors to be
retrieved.
Design the UML class diagrams for this system.
Write the C++ program for this system.
Case Study– Design of Class Diagrams
Author
Book
-title: string
-author: Author
-price: double
-qtyInStock:int = 0
+Book(name:string, author:Author,
price:double, qtyInStock:int)
+getName(): string
+getAuthor():Author
+getPrice():double
+setPrice(price:double):void
+getQtyInStock():int
+setQtyInStock(qtyInStock:int):void
+print():void
1
-name: string
-email:string
1 -gender:char
+Author(name:string, email:string,
char gender)
+setEmail(email:string):void
+getName():string
+getEmail():string
+getGender():string
+print():void
Case Study– Class Interfaces
class Author {
string name;
string email;
char gender;
public:
Author(string name, string email, char gender)
: name(name), email(email), gender(gender) {}
void setEmail(string email);
string getName();
string getEmail();
char getGender();
void print();
};
Case Study– Class Interfaces
class Book {
string title;
Author author;
double price;
int qtyInStock;
public:
Book(string title, Author author, double price, int qtyInStock):
title(title), author(author), price(price), qtyInStock(qtyInStock)
{ }
void setPrice(double price);
void setQtyInStock(int qtyInStock);
string getTitle();
Author getAuthor();
double getPrice();
int getQtyInStock();
void print();
};
Case Study– Class Implementations
void Book::setPrice(double price)
{
this->price = price;
}
void Book::setQtyInStock(int qtyInStock)
{
this->qtyInStock = qtyInStock;
}
string Book::getTitle()
double Book::getPrice()
{
{
return title;
return price;
}
}
int Book::getQtyInStock() {
return qtyInStock;
Author Book::getAuthor()
return author;
{
}
}
void Book::print() {
cout << "\nTitle\t: " << title << endl;
cout << "Author\t: " << author.getName() << endl;
cout << "Price\t: " << price << endl;
cout << "Quantity: " << qtyInStock << endl;
}
Case Study– Class Implementations
void Author::setEmail(string email)
this->email = email;
}
string Author::getName()
return name;
}
string Author::getEmail()
return email;
}
{
{
{
void Author::print() {
cout << "\n\nAuthor Details:" << endl << endl;
cout << "Name: " << name << endl;
cout << "Email: " << email << endl;
}
Case Study – Using the classes (Driver)
int main() {
vector<Book> books;
Author author1("Jane Austen","[email protected]",'F');
books.push_back(Book("Pride and Prejudice", author1, 39, 2));
books.push_back(Book("Sense and Sensibility", author1, 30, 3));
books.push_back(Book("Emma", author1, 50, 1));
Author author2("Charles Dickens","[email protected]",'M');
books.push_back(Book("Oliver Twist", author2, 25, 1));
books.push_back(Book("A Chirstmas Carol", author2, 35, 5));
for (int i=0; i<books.size(); i++)
books[i].print();
}
Case Study – Output
Case Study – Creating a Bookstore


Now, how do we use the Book and Author classes to
create a more complete Bookstore system?
What are the functions needed for a Bookstore system?
•
•
•
•
•
Add and delete books
Search for books by Title
Search for books by Author
Change the price for a book
Change the quantity in stock for a book
Can you create a
Bookstore system now
using the Book and Author
classes?