There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop mac malware motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound trojan usb virus vista vista 32-bit windows windows vista windows xp winxp wireless wmp
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Extending a class - struct referencing problem.


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
Biguns's Avatar
Junior Member with 5 posts.
 
Join Date: Oct 2003
09-Nov-2003, 12:43 AM #1
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.
Biguns's Avatar
Junior Member with 5 posts.
 
Join Date: Oct 2003
09-Nov-2003, 02:53 AM #2
On further inspection, it appears that the currentTownPtr pointing to startTownPtr is ok, but when I try to change the details of startTownPtr using currentTownPtr, there are access violations causing the error. Does anyone know how to fix this?
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:33 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.