// File name: WHEELSUL.CPP
//
// A simple set of rental classes
//
#include <iostream.h>
#include <string.h>
// Base class
class Vehicle
{
public:
Vehicle(long Mileage, const char * Model); // Constructor
void Print() const;
void Rent(const char * ReturnDate);
protected:
long mileage;
char model[21];
char returnDate[9];
};
class Car : public Vehicle // Car is dervied from Vehicle
{
public:
// Constructor
Car(long Mileage, const char * Model,char Category);
void Print() const; // Override Vehicle::Print
protected:
char category; // Only in Car not Vehicle
};
class Truck: public Vehicle // Truck is derived from Vehicle
{
public:
// Constructor
Truck(long Mileage, const char * Model,float MaxLoad);
void Print() const; // Override Vehicle::Print
protected:
float maxLoad; // Only in Truck
};
// Global function
void GetRentalDate(char * date)
{
cout << "When are you returning the vehicle?";
cin.getline(date,9);
}
//------------------------------------------------------------
// Main is here!
//------------------------------------------------------------
void main()
{
char type;
char rentalDate[9];
// Ask what type of rental
cout << "Rent car (c) or truck (t)? ";
cin >> type;
cin.ignore();
if (type == 't' || type == 'T')
{
// truck rental
// Make a truck (for simplicity - hard coded)
Truck truck(4000,"Dodge",3.5F);
// Normal function
GetRentalDate(rentalDate);
// Truck does not have Rent - calls Vehicle::Rent
truck.Rent(rentalDate);
// Truck does have Print - calls Truck::Print
truck.Print();
}
else
{
// Car rental
Car car(2500,"Buick",'B'); // W.U.L. only has 1 car!
GetRentalDate(rentalDate);
// Car does not have Rent - calls Vehicle::Rent
car.Rent(rentalDate);
// Car has Print - calls Car::Print
car.Print();
}
}
//------------------------------------------------------------
// Vehicle class functions
//
Vehicle::Vehicle(long Mileage, const char * Model)
{
mileage = Mileage; // could have used intialization list
strcpy(model,Model);
returnDate[0] = '\0'; // zero length string
}
void Vehicle::Print() const
{
cout << "Model : " << model << endl;
cout << "Mileage: " << mileage << endl;
cout << "Rented till: " ;
if (returnDate[0]) // a test to see if first char is
// a terminator
cout << returnDate;
else
cout << "Not rented";
cout << endl;
}
void Vehicle::Rent(const char * RentalDate)
{
strcpy(returnDate,RentalDate);
}
//------------------------------------------------------------
// Car class functions
//
Car::Car(long Mileage, const char * Model, char Category)
: Vehicle(Mileage,Model) // calls base constructor
{
category = Category;
}
void Car::Print() const // Overrides Vehicle::Print()
{
cout << "Car Rental" << endl;
Vehicle::Print(); // Calls base class Print
cout << "Category : " << category << endl;
}
//------------------------------------------------------------
// Car class functions
//
Truck::Truck(long Mileage, const char * Model, float MaxLoad)
: Vehicle(Mileage,Model)
{
maxLoad = MaxLoad;
}
void Truck::Print() const
{
cout << "Truck Rental" << endl;
Vehicle::Print();
cout << "Max load : " << maxLoad << endl;
}
===============================
// File name: WHEELSVL.CPP
//
// A simple set of rental classes using virtuals
//
#include <iostream.h>
#include <string.h>
// Base class
class Vehicle
{
public:
virtual ~Vehicle() // Virtual destructor
{} // does nothing but safe
void Print() const; // Never overridden
virtual void Rent(const char * ReturnDate); // Could be
protected:
Vehicle(long Mileage, const char * Model); // Not needed public
virtual const char * GetRentalType() const = 0;
virtual void PrintAdditionalDetails() const = 0;
private:
long mileage;
char model[21];
char returnDate[9];
};
class Car : public Vehicle // Car is derived from Vehicle
{
public:
Car(long Mileage, const char * Model,char Category);
protected:
const char * GetRentalType() const // Still virtual
{
return "Car";
}
void PrintAdditionalDetails() const; // still virtual
private:
char category; // Only in Car not Vehicle
};
class Truck: public Vehicle // Truck is derived from Vehicle
{
public:
Truck(long Mileage, const char * Model,float MaxLoad);
protected:
virtual const char * GetRentalType() const
{
return "Truck";
}
virtual void PrintAdditionalDetails() const;
private:
float maxLoad; // Only in Truck
};
// Global function
void GetRentalDate(char * date)
{
cout << "When are you returning the vehicle?";
cin.getline(date,9);
}
//------------------------------------------------------------
// Main is here!
//------------------------------------------------------------
void main()
{
char type;
char rentalDate[9];
Vehicle * vehicle;
// Ask what type of rental
cout << "Rent car (c) or truck (t)? ";
cin >> type;
cin.ignore(80,'\n');
if (type == 't' || type == 'T')
// truck rental
vehicle = new Truck(4000,"Dodge",3.5F);
else
// Car rental
vehicle = new Car(2500,"Buick",'B');
// Normal function
GetRentalDate(rentalDate);
vehicle->Rent(rentalDate); // Not overridden
vehicle->Print();
delete vehicle;
}
//------------------------------------------------------------
// Vehicle class functions
//
Vehicle::Vehicle(long Mileage, const char * Model)
{
mileage = Mileage; // could have used intialization list
strcpy(model,Model);
returnDate[0] = '\0'; // zero length string
}
void Vehicle::Print() const
{
cout << GetRentalType() << " Rental" << endl;
cout << "Model : " << model << endl;
cout << "Mileage: " << mileage << endl;
cout << "Rented till: " ;
if (returnDate[0]) // a test to see if first char is
// a terminator
cout << returnDate;
else
cout << "Not rented";
cout << endl;
PrintAdditionalDetails();
}
void Vehicle::Rent(const char * RentalDate)
{
strcpy(returnDate,RentalDate);
}
//------------------------------------------------------------
// Car class functions
//
Car::Car(long Mileage, const char * Model, char Category)
: Vehicle(Mileage,Model) // calls base constructor
{
category = Category;
}
void Car::PrintAdditionalDetails() const
{
cout << "Category : " << category << endl;
}
//------------------------------------------------------------
// Turck class functions
//
Truck::Truck(long Mileage, const char * Model, float MaxLoad)
: Vehicle(Mileage,Model)
{
maxLoad = MaxLoad;
}
void Truck::PrintAdditionalDetails() const
{
cout << "Max load : " << maxLoad << endl;
}
====================================
// Filename: WR1.CPP
// Writes names to a file
#include <fstream.h>
void main()
{ //Create or append to a file
ofstream fp("C:\\NAMES.DAT", ios::app);
if (!fp) // Exit on error
return;
fp << "Kim Spencer" << endl;
fp << "Michael Spencer" << endl;
fp << "Derek Spencer" << endl;
fp << "Jane Spencer" << endl;
fp << "---------------" << endl;
} // Close file automatically
===========================
// Filename: WR2.CPP
// Writes 1 to 100 to a disk file
#include <fstream.h>
void main()
{
int ctr;
ofstream fp;
fp.open("C:\\NUMS.1", ios::out); // Creates a new file
if (!fp) // Checks for error
{ cout << "Error opening file." << endl; }
else
{
for (ctr = 1; ctr < 101; ctr++)
{ fp << ctr << " "; }
}
fp.close();
return;
}
Tidak ada komentar:
Posting Komentar