data doesn't pass from a class to another

i was trying to do a inheritance class whereby it's similar below, but why, whenever a class end, the value returns to it's original value?? Is there anyway that can fix??

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <windows.h>
using namespace std;
class Ticket
{
      public:

      int date1,date2,time1,time2,tax,passenger;
      int money=1200;
      int charge=250;
};
class p:public Ticket
{
	public:
		p()
		{
			cout << "date1:";
			cin  >> date1;
			cout << "date2:";
			cin  >> date2;
			cout << "Time1:";
			cin  >> time1;
			cout << "Time2:";
			cin  >> time2;
			
			tax=(money*passenger)/10;
			
		}
};
class PrintTicket:public Ticket
{

      public:
      PrintTicket()
      {
                  
				  
                 ofstream ofil("Itinerary.txt",ios::out);

                 ofil<<"-----------------------------------"<<endl;

                 ofil<<"Your Flight Itinerary:-"<<endl<<endl;

                 ofil<<"KUL-HND:-"<<endl;

                 ofil<<"Date:"<<date1<<" Nov 2013,"<<endl;

                 cout<<date1<<endl;

                 ofil<<"Time:"<<time1<<endl<<endl;

                 cout<<time1<<endl;

                 ofil<<"HND-KUL:_"<<endl;

                 ofil<<"Date:"<<date2<<" Nov 2013,"<<endl;

                 cout<<date2<<endl;

                 ofil<<"Time:"<<time2<<endl<<endl;

                 cout<<time2<<endl;

                 ofil<<"-----------------------------------"<<endl;

                 ofil<<"Your Ticket Price:-"<<endl;

                 ofil<<"-----------------------------------"<<endl;

                 ofil<<passenger<<" Adult(s) :"<<money<<endl;

                 cout<<passenger<<endl<<money<<endl;

                 ofil<<"Subcharge   :"<<charge<<endl;

                 cout<<charge<<endl;

                 ofil<<"Tax    :"<<tax<<endl;

                 cout<<tax<<endl;

                 ofil<<"Total=Price For Passenger + Subcharge + Tax:"<<(money*passenger)+charge+tax<<endl;

                 cout<<"Printing Iternerary"<<endl;

                 Sleep(3000);Beep(1500,1000);

                 cout<<"Printing In Progress"<<endl;

                 Sleep(3000);Beep(1000,1000);

                 cout<<"Printing Successfully!"<<endl;

                 Sleep(3000);Beep(500,1000);



      }

};

using namespace std;
int main(int argc, char** argv) 
{
	p pa;
	PrintTicket jj;
	system("pause");
	return 0;
}
Your question is analogous to following:
1
2
3
int a = 7;
int b = 42;
// Why is a still 7 and not 42? 


The only thing common between your variables 'pp' and 'jj' is that both are Tickets. However, they are separate tickets.
You're printing uninitialized values (-858993460)

Line 28: passenger is uninitialized.

-----------------------------------
Your Flight Itinerary:-

KUL-HND:-
Date:-858993460 Nov 2013,
Time:-858993460

HND-KUL:_
Date:-858993460 Nov 2013,
Time:-858993460

-----------------------------------
Your Ticket Price:-
-----------------------------------
-858993460 Adult(s) :1200
Subcharge :250
Tax :-858993460
Total=Price For Passenger + Subcharge + Tax:-858994170


The reason for this is your inheiritance structure.
PrintTicket inherits from Ticket.
Ticket never initializes it's values.
Your program defines both pa and jj.
pa's constructor prompts for input from the user.
You then try to print using jj which was never initialized since p is not in it's inheiritance tree.

Change PrintTicket to inherit from p and get rid of pa.



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <windows.h>
using namespace std;
class Ticket
{
      public:

      int date1,date2,time1,time2,tax,passenger;
      int money=1200;
      int charge=250;
};
class p:public Ticket
{
	public:
		p()
		{
			cout << "date1:";
			cin  >> date1;
			cout << "date2:";
			cin  >> date2;
			cout << "Time1:";
			cin  >> time1;
			cout << "Time2:";
			cin  >> time2;
			
			tax=(money*passenger)/10;
			
		}
};
class PrintTicket:public p
{

      public:
      PrintTicket()
      {
                  
				  
                 ofstream ofil("Itinerary.txt",ios::out);

                 ofil<<"-----------------------------------"<<endl;

                 ofil<<"Your Flight Itinerary:-"<<endl<<endl;

                 ofil<<"KUL-HND:-"<<endl;

                 ofil<<"Date:"<<date1<<" Nov 2013,"<<endl;

                 cout<<date1<<endl;

                 ofil<<"Time:"<<time1<<endl<<endl;

                 cout<<time1<<endl;

                 ofil<<"HND-KUL:_"<<endl;

                 ofil<<"Date:"<<date2<<" Nov 2013,"<<endl;

                 cout<<date2<<endl;

                 ofil<<"Time:"<<time2<<endl<<endl;

                 cout<<time2<<endl;

                 ofil<<"-----------------------------------"<<endl;

                 ofil<<"Your Ticket Price:-"<<endl;

                 ofil<<"-----------------------------------"<<endl;

                 ofil<<passenger<<" Adult(s) :"<<money<<endl;

                 cout<<passenger<<endl<<money<<endl;

                 ofil<<"Subcharge   :"<<charge<<endl;

                 cout<<charge<<endl;

                 ofil<<"Tax    :"<<tax<<endl;

                 cout<<tax<<endl;

                 ofil<<"Total=Price For Passenger + Subcharge + Tax:"<<(money*passenger)+charge+tax<<endl;

                 cout<<"Printing Iternerary"<<endl;

                 Sleep(3000);Beep(1500,1000);

                 cout<<"Printing In Progress"<<endl;

                 Sleep(3000);Beep(1000,1000);

                 cout<<"Printing Successfully!"<<endl;

                 Sleep(3000);Beep(500,1000);



      }

};

using namespace std;
int main(int argc, char** argv) 
{
	p pa;
	PrintTicket jj;
	system("pause");
	return 0;


What u mean is by this right?? But my problem is...I will get second time input for class p when the class printticket is compiling...
And I can't delete my class p and merge it into class printticket bcause my question don't allow me to do so...must going to hv both class p and printticket
Thats why I create another class and inherited from the base class...but my base class value is not initialize...any other way to fix it?
Please, explain in your own words what you think the class inheritance to mean. What you have shown so far does imply that you have misunderstood the concept.
I will get second time input for class p

Read my earlier response. Get rid of pa (line 107).
At least now you have the inheirtance right Ticket<-p<-PrintTicket

pa and jj are two different instances which both include class p.
Therefore p's constructor gets called for both pa and jj.

As noted earlier, passenger is still uninitialized at line 28.
Therefore your calculation of tax is bogus.
Lines 72, 74 and 84 will also output nonsense as a result.

must going to hv

Don't know what that means.

my base class value is not initialize...any other way to fix it?

Any reason you can't create a constructor for Ticket to initialize it?

Last edited on
eddytan95 wrote:
must going to hv
AA wrote:
Don't know what that means.

The (yet not described) homework apparently requires "classes".
Last edited on
Topic archived. No new replies allowed.