Datasheet

~AirlineTicket();
int calculatePriceInDollars();
std::string getPassengerName();
void setPassengerName(std::string inName);
int getNumberOfMiles();
void setNumberOfMiles(int inMiles);
bool getHasEliteSuperRewardsStatus();
void setHasEliteSuperRewardsStatus(bool inStatus);
private:
std::string mPassengerName;
int mNumberOfMiles;
bool fHasEliteSuperRewardsStatus;
};
The method that has the same name of the class with no return type is a constructor. It is automatically
called when an object of the class is created. The method with a tilde (~) character followed by the class
name is a destructor. It is automatically called when the object is destroyed.
The sample program that follows makes use of the class declared in the previous example. This example
shows the creation of a stack-based
AirlineTicket object as well as a heap-based object.
// AirlineTicketTest.cpp
#include <iostream>
#include “AirlineTicket.h”
using namespace std;
int main(int argc, char** argv)
{
AirlineTicket myTicket; // Stack-based AirlineTicket
myTicket.setPassengerName(“Sherman T. Socketwrench”);
myTicket.setNumberOfMiles(700);
int cost = myTicket.calculatePriceInDollars();
cout << “This ticket will cost $” << cost << endl;
AirlineTicket* myTicket2; // Heap-based AirlineTicket
myTicket2 = new AirlineTicket(); // Allocate a new object
myTicket2->setPassengerName(“Laudimore M. Hallidue”);
myTicket2->setNumberOfMiles(2000);
myTicket2->setHasEliteSuperRewardsStatus(true);
int cost2 = myTicket2->calculatePriceInDollars();
cout << “This other ticket will cost $” << cost2 << endl;
delete myTicket2;
return 0;
}
27
A Crash Course in C++
04_574841 ch01.qxd 12/15/04 3:39 PM Page 27