I need help creating a C++ class for: A customer buys a book. ?
I am totally new to C++ classes and I need some help.
I want to create a simple program where A customer chooses a book and buys the book for a certain value. But I need to use classes. I am not really concern with the main program itself I am just more concern with creating the appropriate classes correctly. Can anyone help me?
Here’s a quick demo of what the interface could possibly look like. This isn’t quite for beginners….. but this is something to keep and look at down the road. This is one of those snippets that won’t be too much help rite now, but will be a life saver later. Trust me.
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class BookClass
{
private:
float bookValue;
float AmountPaid;
bool BookPaidInFull;
string BookName;
public:
BookClass()
{
bookValue = 39.99;
BookName = "George orwell’s 1984";
}
float GetBookValue();
float GetAmountPaid();
bool IsBookPaidFor();
string GetBookName();
}bookclass;
class BookClass *_BuyBook = &bookclass;
float BookClass::GetBookValue()
{
return bookValue;
}
float BookClass::GetAmountPaid()
{
return AmountPaid;
}
bool BookClass::IsBookPaidFor()
{
return BookPaidInFull;
}
string BookClass::GetBookName()
{
return BookName;
}
int main()
{
char ch;
float cost;
bool tmpVar = 0;
cout << "Hello, would you like to buy: " << _BuyBook->GetBookName();
cout << "\n(y / n): ";
cin >> ch;
if( ch == ‘y’ ){
cout << "\n\nThe book is: " << _BuyBook->GetBookValue();
while( 1 )
{
if( cost != _BuyBook->GetBookValue() ){
if( tmpVar == 0 ){
cout << "\nPlease enter the price to purchse this product: ";
cin >> cost;
tmpVar = 1;
}else{
cout << "\nSorry, but your funds did not patch the book’s price.";
cout << "\nPlease re-enter your deposite:\n";
cin >> cost;
}
}
else
{
cout << "\n\nCongratulations! The book is yours!";
break;
}
}
getch();
}
else{
cout << "\nGoodbye!";
getch();
exit( 0×001 );
}
return 0xF;
}
Words of Wisdom,
– Hex
Here’s a quick demo of what the interface could possibly look like. This isn’t quite for beginners….. but this is something to keep and look at down the road. This is one of those snippets that won’t be too much help rite now, but will be a life saver later. Trust me.
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class BookClass
{
private:
float bookValue;
float AmountPaid;
bool BookPaidInFull;
string BookName;
public:
BookClass()
{
bookValue = 39.99;
BookName = "George orwell’s 1984";
}
float GetBookValue();
float GetAmountPaid();
bool IsBookPaidFor();
string GetBookName();
}bookclass;
class BookClass *_BuyBook = &bookclass;
float BookClass::GetBookValue()
{
return bookValue;
}
float BookClass::GetAmountPaid()
{
return AmountPaid;
}
bool BookClass::IsBookPaidFor()
{
return BookPaidInFull;
}
string BookClass::GetBookName()
{
return BookName;
}
int main()
{
char ch;
float cost;
bool tmpVar = 0;
cout << "Hello, would you like to buy: " << _BuyBook->GetBookName();
cout << "\n(y / n): ";
cin >> ch;
if( ch == ‘y’ ){
cout << "\n\nThe book is: " << _BuyBook->GetBookValue();
while( 1 )
{
if( cost != _BuyBook->GetBookValue() ){
if( tmpVar == 0 ){
cout << "\nPlease enter the price to purchse this product: ";
cin >> cost;
tmpVar = 1;
}else{
cout << "\nSorry, but your funds did not patch the book’s price.";
cout << "\nPlease re-enter your deposite:\n";
cin >> cost;
}
}
else
{
cout << "\n\nCongratulations! The book is yours!";
break;
}
}
getch();
}
else{
cout << "\nGoodbye!";
getch();
exit( 0×001 );
}
return 0xF;
}
Words of Wisdom,
– Hex
References :