I have been learning C++ by myself and have made significant progress. I need some insight/help however relating to the use of pointers in the application outlined here. With respect to the design of a program for a video store's transactions, I wrote two classes,
videoListType and
customerListType, that are each derived from a class template,
linkedListType<
class parameter>, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class videoListType: public linkedListType<videoType>
{
.
.
.
};
class customerListType: public linkedListType<customerType>
{
.
.
.
};
|
Please note that the parameters
videoType and
customerType are themselves classes while classes
videoListType and
customerListType are not class templates!
I wrote a non-class function
createVideoList(ifstream& infile, videoListType& videoList) to load video data from a text file, successfully created a linked-list object
videoList in the process, and was able to print its contents. Since each customer of the video store could rent one or more videos, I wrote class customerType(derived from another class
personType) in such a way that it has a pointer of type videoListType as a data member, as sketchily shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class customerType: public personType
{
public:
setCustomerInfo(newString fName, newString lName, newString acctNo, videoListType* rVideoList);
.
.
.
customerType(newString fName = "", newString lName = "", newString acctNo = "", rvideoList = NULL);
private:
newString custAcctNo;
videoListType* rentedVideoList;
};
|
With the backdrop above, here are my questions:
#1. If after all programming protocols are observed, the code segment below worked for object
videoList in function
main():
1 2 3 4 5
|
ifstream infile;
videoListType videoList;
createVideoList (infile, videoList);
videoList.print();
|
Question: How come it's not working when I used instead a declared pointer (i.e.
videoListPtr) to the object as shown (after making all required changes to declarations, formal parameters, etc, of course):
1 2 3 4
|
videoListType* videoListPtr;
createVideoList(infile, videoListPtr);
videoListPtr->print();
|
#2. Please consider the following codes I wrote relating to the non-class function
createCustomerList(
ifstream& infile, customerListType customerList):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
void createCustomerList(ifstream& infile, customerListType& customerList)
{
newString FirstName;
newString LastName;
newString AcctNo;
char ch;
videoListType* q = NULL;
customerType newCustomer;
cout<<"Inside fn createCustomerList!"<<endl<<endl;
infile>>FirstName;
while (infile)
{
infile>>LastName;
infile>>AcctNo;
infile.get(ch); //mopping up the newline character after last read
loadCustomerRentedVideoList(infile, q);
newCustomer.setCustomerInfo(FirstName, LastName, AcctNo, q);
customerList.insertLast(newCustomer); //function inherited from base class linkedListType
infile>>FirstName;
}//end while
cout<<"After completing the whole reading in fn createCustomerList!"<<endl;
cout<<"pointer q in fn is = "<<q<<endl<<endl;
}
void loadCustomerRentedVideoList(ifstream& infile, videoListType*& p_VideoList)
{
char Title[50];
char Star1[50];
char Star2[50];
char Producer[50];
char Director[50];
char ProductionCo[70];
char ch, ch1, ch2;
int InStock;
videoType newVideo;
videoListType videoList;
cout<<"Inside fn loadCustomerRentedVideoList!"<<endl<<endl;
infile.get(ch1);
ch2 = infile.peek();
while (isalnum(ch1) && isalnum(ch2))
{
infile.putback(ch1);
infile.get(Title, 50);
infile.get(ch); //for the newline character of preceding input data
infile.get(Star1, 50);
infile.get(ch);
infile.get(Star2, 50);
infile.get(ch);
infile.get(Producer, 50);
infile.get(ch);
infile.get(Director, 50);
infile.get(ch);
infile.get(ProductionCo, 70);
infile.get(ch);
infile>>InStock; //different from the rest since InStock is an integer
infile.get(ch);
newVideo.setVideoInfo(Title, Star1, Star2, Producer, Director, ProductionCo, InStock);
videoList.insertLast(newVideo); //function inherited from base class linkedListType
infile.get(ch); //to lick up the newline character btw video info blocks
infile.get(ch1);
ch2 = infile.peek();
}//end while
p_VideoList = &videoList;
cout<<"Just before returning from fn loadCustomerRentedVideoList!"<<endl;
cout<<"p_VideoList in fn = "<<p_VideoList<<endl<<endl;
}
|
The output yielded by the two functions above puzzled me a lot! Here it is:
Inside fn createCustomerList!
Inside fn loadCustomerRentedVideoList!
Just before returning from fn loadCustomerRentedVideoList!
p_VideoList in fn = 0x28f7a0
Inside fn loadCustomerRentedVideoList!
Just before returning from fn loadCustomerRentedVideoList!
p_VideoList in fn = 0x28f7a0
Inside fn loadCustomerRentedVideoList!
Just before returning from fn loadCustomerRentedVideoList!
p_VideoList in fn = 0x28f7a0
After completing the whole reading in fn createCustomerList!
pointer q in fn is = 0x28f7a0 |
Question: Why are the values of the pointers the same especially since each call to function
loadCustomerRentedVideoList() is for a
different customer??!! Each trip to function load...() successfully read the videos associated with each customer; but why are they all lumped together apparently since the pointer values returned are all the same?? I later observed I could not print the information of the customer list; the first name, last name, and account number of the first customer print, then the program freezes up and aborts!!
Please, help guys! I am at a crossroads!!