Extending a class - struct referencing problem. I have an assignment I'm doing where I have an aeroplane journey from a starting point to an ending point with the distance. It is extended by a generic journey that needs to have an unlimited amount of 'stoppages' along the way, so it extends the class. My base class works ok, and my derived class compiles as well, but i get errors when testing it, i think it has to do with my derived class accessing the base class pointers to the start structure (see in blue). Here is the code. Any ideas? PlaneJourney.h
/ File: planeJourney.h
// Declaration of class planeJourney.
// Member functions are defined in planeJourney.cpp
// prevent multiple inclusions of header file
#ifndef PLANEJOURNEY_H
#define PLANEJOURNEY_H
// planeJourney abstract data type (ADT) definition
class planeJourney {
typedef struct townRecord{
string townName;
double distance;
townRecord *Next;
}townRecord;
public:
planeJourney();
~planeJourney() {};
void setPlaneJourney( string, string, double );
void setEndTown( string, double );
void setStartTown( string );
string getStartTownName();
string getEndTownName();
void getTownsVisited();
void getDistanceTravelled();
//protected:
townRecord* startTownPtr;
townRecord* endTownPtr;
}; // end class planeJourney
#endif PlaneJourney.cpp
// File: planeJourney.cpp
// Member-function definitions for class planeJourney
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <iomanip>
typedef struct townRecord{
string townName;
double distance;
townRecord *Next;
}townRecord;
// include definition of class planeJourney from planeJourney.h
#include "planeJourney.h"
// planeJourney default constructor initializes start and
// end town names to NULL.
planeJourney::planeJourney()
{
startTownPtr=NULL;
endTownPtr=NULL;
} // end planeJourney constructor
// Set new Plane Journey using two town names and the distance
// from the departure point to the destination point.
void planeJourney::setPlaneJourney( string strtTown, string destTown, double dist )
{
setStartTown( strtTown );
setEndTown( destTown, dist );
} // end function setplaneJourney
void planeJourney::setStartTown( string strtTown )
{
startTownPtr=new townRecord;
startTownPtr->townName=strtTown;
startTownPtr->distance=0;
startTownPtr->Next=NULL;
}
void planeJourney::setEndTown( string destTown, double dist )
{
endTownPtr=new townRecord;
endTownPtr->townName=destTown;
endTownPtr->distance=dist;
endTownPtr->Next=NULL;
}
string planeJourney::getStartTownName()
{
return startTownPtr->townName;
}
string planeJourney::getEndTownName()
{
return endTownPtr->townName;
}
void planeJourney::getTownsVisited()
{
cout << "Departure from: " << startTownPtr->townName
<< "\nArrival at: " << endTownPtr->townName
<< endl;
}
void planeJourney::getDistanceTravelled()
{
cout << "Distance of " << endTownPtr->distance
<< " kilometres." << endl;
} Journey.h
// File: Journey.h
// Declaration of class Journey extends planeJourney.
// Member functions are defined in Journey.cpp
// prevent multiple inclusions of header file
#ifndef JOURNEY_H
#define JOURNEY_H
#include "planeJourney.h" // planeJourney class definition
// Journey abstract data type (ADT) definition
class Journey : public planeJourney {
public:
Journey();
~Journey() {};
void addTown( string, double );
void getTownsVisited();
void getDistanceTravelled();
private:
townRecord *currentTownPtr;
}; // end class Journey
#endif Journey.cpp
// File: Journey.cpp
// Member-function definitions for class Journey
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <iomanip>
// include definition of class Journey from Journey.h
#include "Journey.h"
// Journey default constructor initializes start and
// end town pointers to point to NULL.
Journey::Journey()
: planeJourney()
{ currentTownPtr=startTownPtr;
} // end Journey constructor
// Add another town to the current Journey and its distance.
void Journey::addTown( string nextTown, double dist )
{
currentTownPtr->Next=new townRecord;
currentTownPtr->Next->townName=nextTown;
currentTownPtr->Next->distance=dist;
currentTownPtr=currentTownPtr->Next;
currentTownPtr->Next=NULL;
} // end function addTown
void Journey::getTownsVisited()
{
cout << planeJourney::getStartTownName() << endl;
}
void Journey::getDistanceTravelled()
{
cout << "Etc" << endl;
} journeyTest.cpp
// File: journeyTest.cpp
// Program to test class planeJourney.
// NOTE: Compiled with Journey.cpp
// and planeJourney.cpp
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
// include class definitions
#include "planeJourney.h"
#include "Journey.h"
void main(void)
{
int displayMainMenu();
void newPlaneJourney();
void newJourney();
void invalidInput();
void exitProgram();
int option = displayMainMenu();
do {
switch (option) {
case 1 : newPlaneJourney(); break;
case 2 : newJourney(); break;
case 0 : exitProgram(); break;
default: invalidInput(); break;
};
option = displayMainMenu();
} while (option != 0);
exitProgram();
}; // end main
void newPlaneJourney() {
string depart;
string arrive;
double theDistance;
planeJourney journey; // instantiate object journey of class planeJourney
cout << "Enter town for departure: ";
cin >> depart;
cout << "Enter town of destination: ";
cin >> arrive;
cout << "Enter distance of journey: ";
cin >> theDistance;
journey.setPlaneJourney( depart, arrive, theDistance );
journey.getTownsVisited();
journey.getDistanceTravelled();
cout << "Start Town:" << journey.getStartTownName() << endl;
cout << "End Town:" <<journey.getEndTownName() << endl;
cin >> theDistance;
cin.get();
}
void newJourney() {
string depart;
string arrive;
double theDistance;
Journey journey; // instantiate object journey of class Journey
cout << "Enter town for departure: ";
cin >> depart;
cout << "Enter town of destination: ";
cin >> arrive;
cout << "Enter distance of journey: ";
cin >> theDistance;
journey.setStartTown( "Bathurst" );
journey.addTown( arrive, theDistance );
journey.addTown( "Lithgow", 55 );
journey.addTown( "Wagga", 56 );
journey.addTown( "Dubbo", 80 );
journey.getTownsVisited();
journey.getDistanceTravelled();
cin.get();
}
int displayMainMenu() {
int i = 0;
system("cls");
cout << "\n\n********** - JOURNEY LISTINGS MAIN MENU - **********\n";
cout << "* *\n";
cout << "* [ 1 ] Create a new Plane Journey *\n";
cout << "* [ 2 ] Create a new Journey *\n";
cout << "* [ 0 ] Exit Journey Program *\n";
cout << "*****************************************************\n";
cout << "Enter Option: ";
cin >> i;
return i;
};
void exitProgram() {
system("cls");
cout << "\n\nExiting Journey Listings\nThankyou for using Journey Listings\n\n";
};
void invalidInput() {
cout << "Invalid Input - Expected Digit from Menu Options" << endl;
};
Last edited by Biguns : 09-Nov-2003 01:04 AM.
|