Packed goods have fixed prices.The price of groceries sold by weight is calculated by multiplying the weight by the current price per kilo.. Develop the classes needed to represent the p
Trang 1Exercise 3
A supermarket chain has asked you to develop an automatic checkout system All products are identifiable by means of a barcode and the product name Groceries are either sold in packages or by weight Packed goods have fixed prices.The price of groceries sold by weight is calculated by multiplying the weight by the current price per kilo.
Develop the classes needed to represent the products first and organize them hierarchically.The Productclass, which contains generic information on all products (barcode, name, etc.), can be used as a base class.
■ TheProductclass contains two data members of type longused for storing barcodes and the product name Define a constructor with
parameters for both data members.Add default values for the para-meters to provide a default constructor for the class In addition to the access methods setCode()andgetCode(), also define the methods
scanner()andprinter() For test purposes, these methods will simply output product data on screen or read the data of a product from the keyboard.
■ The next step involves developing special cases of the Productclass Define two classes derived from Product, PrepackedFoodand Fresh-Food In addition to the product data, the PrepackedFoodclass should contain the unit price and the FreshFoodclass should contain a weight and a price per kilo as data members.
In both classes define a constructor with parameters providing default-values for all data members Use both the base and member ini-tializer.
Define the access methods needed for the new data members.Also redefine the methods scanner()andprinter()to take the new data members into consideration.
■ Test the various classes in a mainfunction that creates two objects each
of the types Product,PrepackedFoodandFreshFood One object of each type is fully initialized in the object definition Use the default con-structor to create the other object.Test the getandsetmethods and thescanner()method and display the products on screen.
Trang 2■ SOLUTIONS
Exercise 1
// -// Car.h : Defines the base class Car and
// the derived classes PassCar and Truck //
-#ifndef _CAR_H_
#define _CAR_H_
#include <iostream>
#include <string>
using namespace std;
class Car {
// See previous definition in this chapter };
class PassCar : public Car {
// See previous definition in this chapter };
class Truck : public Car {
private:
int axles;
double tons;
public:
Truck( int a, double t, int n, const string& hs);
~Truck();
void setAxles(int l){ axles = l;}
int getAxles() const { return axles; } void setCapacity( double t) { tons = t;}
double getCapacity() const { return tons; } void display() const;
};
#endif
Trang 3//
-// car.cpp
// Implements the methods of Car, PassCar, and Truck
//
-#include "car.h"
//
-// The methods of base class Car:
Car::Car( long n, const string& prod)
{
cout << "Creating an object of type Car." << endl;
nr = n; producer = prod;
}
Car::~Car()
{
cout << "Destroying an object of type Car" << endl;
}
void Car::display() const
{
cout << "\n - "
<< "\nCar number: " << nr
<< "\nProducer: " << producer
<< endl;
}
// -// The methods of the derived class PassCar:
PassCar::PassCar(const string& tp, bool sd, int n,
const string& hs)
: Car( n, hs), PassCarTyp( tp ), sunRoof( sd )
{
cout << "I create an object of type PassCar." << endl;
}
PassCar::~PassCar()
{
cout << "\nDestroying an object of type PassCar"
<< endl;
}
void PassCar::display( void) const
{
Car::display(); // Base class method
cout << "Type: " << passCarType
<< "\nSunroof: ";
if(sunRoof)
cout << "yes "<< endl;
else
cout << "no " << endl;
}
Trang 4// -// The methods of the derived class Truck:
Truck::Truck( int a, double t, int n, const string& hs)
: Car( n, hs), axles(a), tons(t) {
cout << "Creating an object of type Truck." << endl; }
Truck::~Truck() {
cout << "\nDestroying an object of type Truck\n"; }
void Truck::display() const {
Car::display();
cout << "Axles: " << axles
<< "\nCapacity: " << tons << " long tons\n"; }
// -// Car_t.cpp : Tests the base class Car and
// the derived classes PassCar and Truck //
-#include "car.h"
int main() {
Truck toy(5, 7.5, 1111, "Volvo");
toy.display();
char c;
cout << "\nDo you want to create an object of type "
<< " PassCar? (y/n) "; cin >> c;
if( c == 'y' || c == 'Y') {
const PassCar beetle("Beetle", false, 3421, "VW"); beetle.display();
} cout << "\nDo you want to create an object "
<< " of type car? (y/n) "; cin >> c;
if( c == 'y' || c == 'Y') {
const Car oldy(3421, "Rolls Royce");
oldy.display();
} return 0;
}
Trang 5Exercise 2
//
-// account.h:
// Defines the classes Account, DepAcc, and SavAcc
//
-#ifndef _ACCOUNT_H
#define _ACCOUNT_H
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Account
{
private:
string name; unsigned long nr; double balance;
public:
Account(const string& s="X", unsigned long n
= 1111111L, double st = 0.0) : name(s), nr(n), balance(st)
{ }
const string& getName() const { return name; }
void setName(const string& n) { name = n;}
unsigned long getNr() const { return nr; }
void setNr(unsigned long n) { nr = n; }
double getBalance() const { return balance; }
void setBalance(double st){ balance = st; }
void display()
{ cout << fixed << setprecision(2)
<< " -\n"
<< "Account holder: " << name << endl
<< "Account number: " << nr << endl
<< "Balance of the account:" << balance <<endl; }
};
class DepAcc : public Account
{
private:
double limit; // Overdraft limit
double interest; // Interest
public:
DepAcc(const string& s = "X",
unsigned long n = 1111111L, double st = 0.0,
double li = 0.0, double ra = 0.0)
: Account(s, n, st), limit(li), interest(ra)
{ }
Trang 6// Access methods:
double getLimit() const { return limit; } void setLimit(double lt){ limit = lt; } double getInterest() const { return interest; } void setInterest(double sl){ interest = sl; } void display()
{ Account::display();
cout << fixed << setprecision(2)
<< "Overdraft limit: " << limit << endl
<< "Interest rate: " << interest << endl
<< " -\n"
<< endl << endl;
} };
class SavAcc: public Account {
private:
double interest; // compound interest public:
SavAcc(const string& s = "X",
unsigned long n = 1111111L, double st = 0.0, double in = 0.0)
: Account(s, n, st), interest(in) { }
// Access methods
double getInterest() const { return interest; } void setInterest(double in){ interest = in; } void display()
{ Account::display();
cout << fixed << setprecision(2)
<< "Interest rate: " << interest << endl
<< " -\n"
<< endl << endl;
} };
#endif
Trang 7// -// account_t.cpp
// Tests the classes DepAcc and SavAcc
// derived from class Account
//
-#include "account.h"
int main()
{
string s;
double db;
SavAcc mickey("Mickey Mouse", 1234567,
2.40, 3.5);
mickey.display();
cout << "New name: "; getline(cin, s);
cout << "New interest rate: "; cin >> db;
mickey.setName(s);
mickey.setInterest(db);
mickey.display();
DepAcc dag("Donald Duck", 7654321,
-1245.56, 10000, 12.9);
dag.display();
cout << "New limit: "; cin >> db;
dag.setLimit(db);
dag.display();
return 0;
}
Trang 8Exercise 3
// -// product.h : Defines the classes
// Product, PrepackedFood, and FreshFood //
-#ifndef _PRODUCT_H
#define _PRODUCT_H
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Product {
private:
long bar;
string name;
public:
Product(long b = 0L, const string& s = "") : bar(b), name(s)
{ } void setCode(long b) { bar = b; } long getCode() const { return bar; } void setName(const string& s){ name = s; } const string& getName() const { return name; } void scanner()
{ cout << "\nBarcode: "; cin >> bar;
cout << "Name: "; cin >> name;
cin.sync(); cin.clear();
} void printer() const {
cout << "\n -"
<< "\nBarcode: " << bar
<< "\nName: " << name
<< endl;
} };
class PrepackedFood : public Product {
private:
double pce_price;
Trang 9PrepackedFood(double p = 0.0,long b = 0L,
const string& s = "") : Product(b, s), pce_price(p)
{}
void setPrice(double p){ pce_price = p;}
double getPrice()const { return pce_price; }
void scanner()
{ Product::scanner();
cout << "Price per piece: "; cin >> pce_price;
}
void printer() const
{ Product::printer();
cout << fixed << setprecision(2)
<< "Price per piece: " << pce_price << endl; }
};
class FreshFood : public Product
{
private:
double wght;
double lbs_price;
public:
FreshFood(double g = 0.0, double p = 0.0,
long b = 0L, const string& s = "") : Product(b, s), wght(g), lbs_price(p) {}
void setWght(double g) { wght = g;}
double getWght()const { return wght; }
void setPrice(double p) { lbs_price = p;}
double getPrice()const { return lbs_price; }
void scanner()
{ Product::scanner();
cout << "Weight(lbs): "; cin >> wght;
cout << "Price/lbs: "; cin >> lbs_price;
cin.sync(); cin.clear();
}
void printer() const
{
Product::printer();
cout << fixed << setprecision(2)
<< "Price per Lbs: " << lbs_price
<< "\nWeight: " << wght
<< "\nTotal: " << lbs_price * wght
<< endl;
}
};
#endif
Trang 10// -// product_t.cpp
// Tests classes Product, PrepackedFood, and FreshFood //
-#include "product.h"
int main() {
Product p1(12345L, "Flour"), p2;
p1.printer(); // Output the first product p2.setName("Sugar"); // Set the data members p2.setCode(543221);
p2.printer(); // Output the second product
// Prepacked products:
PrepackedFood pf1(0.49, 23456, "Salt"), pf2;
pf1.printer(); // Output the first
// prepacked product cout << "\n Input data of a prepacked product: "; pf2.scanner(); // Input and output pf2.printer(); // data of 2nd product FreshFood pu1(1.5, 1.69, 98765, "Grapes"), pu2;
pu1.printer(); // Output first item
// fresh food cout <<"\n Input data for a prepacked product: "; pu2.scanner(); // Input and output pu2.printer(); // data of 2nd product
cout << "\n -"
<< "\n -"
<< "\nAgain in detail: \n"
<< fixed << setprecision(2)
<< "\nBarcode: " << pu2.getCode()
<< "\nName: " << pu2.getName()
<< "\nPrice per Lbs: " << pu2.getPrice()
<< "\nWeight: " << pu2.getWght()
<< "\nEnd price: " << pu2.getPrice()
* pu2.getWght()
<< endl;
return 0;
}