print derived class object contents

This code compiles nicely - looking for print methods for current objects in the format shown below - looking for a function or method of calling the existing code objects to make them display like the below example.

Customer: Security
Telephone no#: (312)777-1111


Customer: Security
Telephone no#: (312)777-1111
No# phone lines: 10

___________________________________

Customer: Library
Telephone no#: (312)444-2222


Customer: Library
Telephone no#: (312)444-2222
No# phone lines: 8


Any solution i.e... pointers etc. would be better than not getting 'er' done. Please advise. Thanks ~LS




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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// W I P_2                   //Tnum.h interface 

#include <iostream>        
#include <string>
#include <cstring>
using namespace std;      

class Tnum {               

public:                
	string NPA, NXX, LINE, P_NUM;

	Tnum( ) {cout <<"\n Tnum default constructor proof\n";}

	Tnum( string a,string b,string c );

	void ph_num( );

   ~Tnum( );

	string GetNPA( ) const { return NPA; }
	void SetNPA( string g ) { NPA = g; }

	string GetNXX( ) const { return NXX; }
	void SetNXX( string h ) { NXX = h; }

	string GetLINE( ) const { return LINE; }
	void SetLINE( string i ) { LINE = i; } 
};

Tnum::Tnum( string a,string b,string c ) {

	NPA  = a;
	NXX  = b;
	LINE = c;
	P_NUM = NPA + NXX + LINE;  }

void Tnum::ph_num( ) {

	cout <<" \n Tnum parameterized constructor proof\n\n Type 3 digit area code then press 'ENTER' key  ";
	cin >> NPA;

	cout <<" \n Type 3 digit prefix then press 'ENTER' key  ";
	cin >> NXX;

	cout <<" \n Type 4 digit line number then press 'ENTER' key  ";
	cin >> LINE;

	P_NUM = NPA + NXX + LINE; cout <<" \n Telephone no#: "<< P_NUM <<"\n"; }       

Tnum::~Tnum( ) {

	cout <<" \n Tnum destructor fx flushed object containing variable value "<< P_NUM <<"\n\n_________________________________________________________________________"<<endl;}

//________________________________________________
                            
                               //WorTN.h interface                              

class WorTN : public Tnum { 

public:
   string CustName;    

   WorTN( ) {cout <<"\n WorTN default constructor proof\n";}

   WorTN( string d );

   void cust_num( );
             
   ~WorTN( );

   string GetCustName( ) const { return CustName; }
   void SetCustName( string j ) { CustName = j; }
};

WorTN::WorTN( string d ) { CustName = d; }
   
void WorTN::cust_num( ) {
	   
   cout << " \n WorTN parameterized constructor proof\n\n Type customer name then press 'ENTER' key  ";

   cin >> CustName; cout <<" \n Customer name: "<< CustName <<endl;  }

WorTN::~WorTN( ) {

   cout <<" \n WorTN destructor fx flushed object containing variable value "<< CustName <<"\n\n_________________________________________________________________________"<<endl;}


//____________________________________________________________________________
                            
                               //BTN.h interface                              

class BTN : public WorTN { 

public:
   int NumWrkLines;

   BTN( ) {cout <<"\n BTN default constructor proof\n";}
          
   BTN( int e );

   void cust_num_lines( );
    
  ~BTN( );

   int GetNumWrkLines( ) const { return NumWrkLines; }
   void SetNumWrkLines( int k ) { NumWrkLines = k; }
};

BTN::BTN( int e ) { NumWrkLines = e; }	 
 
void BTN::cust_num_lines( ) { 
	
   cout << " \n BTN parameterized constructor proof\n\n Type number of lines then press 'ENTER' key  ";
   cin >> NumWrkLines;

   cout <<" \n No# of lines: "<< NumWrkLines <<endl;  }

BTN::~BTN( ) {

   cout <<" \n BTN destructor fx flushed object containing variable value "<< NumWrkLines <<"\n\n_________________________________________________________________________"<<endl;}

//___________________________________________________________________________________

                       //Tnum_main.cpp utilization

ostream &operator<<(ostream &print1, Tnum m) {
   print1 << "\n Telephone no#: (" << m.NPA << ") ";
   print1 << m.NXX << "-" << m.LINE << "\n";   
   return print1; }

ostream &operator<<(ostream &print2, WorTN n) {
   print2 << "\n Customer: "<< n.CustName << "\n";
   return print2; }

ostream &operator<<(ostream &print3, BTN o) {
   print3 << "\n No# phone lines: "<< o.NumWrkLines << "\n"; 
   return print3; }

ostream& T( ostream& p ) { return p <<'\a'; }   

int main( ) {

   Tnum xx;     xx.ph_num( );          
   WorTN yy;    yy.cust_num( );       
   BTN zz;      zz.cust_num_lines( );  

   Tnum x( "(312) ","777-","1111" );
   Tnum y( "(312) ","444-","2222" );
   cout << x << y <<T<<T<<T<<T<<"\n__________________________\n";

   WorTN q( "Security" );
   WorTN r( "Library" );
   cout << q << r <<T<<T<<T<<T<<"\n__________________________\n";

   BTN s( 10 );
   BTN t( 8 );
   cout << s << t <<T<<T<<T<<T<<"\n__________________________\n";

system ("pause");      
return 0;              
}                     
Last edited on
This:

1
2
3
   Tnum xx;     xx.ph_num( );          xx.~Tnum( ); 
   WorTN yy;    yy.cust_num( );        yy.~WorTN( );
   BTN zz;      zz.cust_num_lines( );  zz.~BTN( );


No NOT explicitly call the destructor like that. The compiler will automatically call it. As it stands, your dtors would be called twice now.

add a display where WorTN q and Tnum x print together followed by WorTN q, Tnum x and BTN s printing together. Also, where WorTN r and Tnum y print together followed by WorTN r, Tnum y and BTN t printing together.


Anyway, you can just write a function.

1
2
3
4
void PrintPhoneInto(ostream& print,const Tnum& tnum,const WorTN& wortn,const BTN& btn)
{
  // output whatever you want here
}


good stuff 'Disch (1581)' - Let me give it a whirl - thanks
Topic archived. No new replies allowed.