By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,248 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,248 IT Pros & Developers. It's quick & easy.

calling derived class objects simultaneously

P: 4
"Close but no cigar" This code does everything great until the end where for many days I have been trying to maintain the current outputs but somehow 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. Something like:

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
Expand|Select|Wrap|Line Numbers
  1.  
  2. // W I P_2                   //Tnum.h interface 
  3.  
  4. #include <iostream>        
  5. #include <string>
  6. #include <cstring>
  7. using namespace std;      
  8.  
  9. class Tnum {               
  10.  
  11. public:                
  12.     string NPA, NXX, LINE, P_NUM;
  13.  
  14.     Tnum( ) {cout <<"\n Tnum default constructor proof\n";}
  15.  
  16.     Tnum( string a,string b,string c );
  17.  
  18.     void ph_num( );
  19.  
  20.    ~Tnum( );
  21.  
  22.     string GetNPA( ) const { return NPA; }
  23.     void SetNPA( string g ) { NPA = g; }
  24.  
  25.     string GetNXX( ) const { return NXX; }
  26.     void SetNXX( string h ) { NXX = h; }
  27.  
  28.     string GetLINE( ) const { return LINE; }
  29.     void SetLINE( string i ) { LINE = i; } 
  30. };
  31.  
  32. Tnum::Tnum( string a,string b,string c ) {
  33.  
  34.     NPA  = a;
  35.     NXX  = b;
  36.     LINE = c;
  37.     P_NUM = NPA + NXX + LINE;  }
  38.  
  39. void Tnum::ph_num( ) {
  40.  
  41.     cout <<" \n Tnum parameterized constructor proof\n\n Type 3 digit area code then press 'ENTER' key  ";
  42.     cin >> NPA;
  43.  
  44.     cout <<" \n Type 3 digit prefix then press 'ENTER' key  ";
  45.     cin >> NXX;
  46.  
  47.     cout <<" \n Type 4 digit line number then press 'ENTER' key  ";
  48.     cin >> LINE;
  49.  
  50.     P_NUM = NPA + NXX + LINE; cout <<" \n Telephone no#: "<< P_NUM <<"\n"; }       
  51.  
  52. Tnum::~Tnum( ) {
  53.  
  54.     cout <<" \n Tnum destructor fx flushed object containing variable value "<< P_NUM <<"\n\n_________________________________________________________________________"<<endl;}
  55.  
  56. //________________________________________________
  57.  
  58.                                //WorTN.h interface                              
  59.  
  60. class WorTN : public Tnum { 
  61.  
  62. public:
  63.    string CustName;    
  64.  
  65.    WorTN( ) {cout <<"\n WorTN default constructor proof\n";}
  66.  
  67.    WorTN( string d );
  68.  
  69.    void cust_num( );
  70.  
  71.    ~WorTN( );
  72.  
  73.    string GetCustName( ) const { return CustName; }
  74.    void SetCustName( string j ) { CustName = j; }
  75. };
  76.  
  77. WorTN::WorTN( string d ) { CustName = d; }
  78.  
  79. void WorTN::cust_num( ) {
  80.  
  81.    cout << " \n WorTN parameterized constructor proof\n\n Type customer name then press 'ENTER' key  ";
  82.  
  83.    cin >> CustName; cout <<" \n Customer name: "<< CustName <<endl;  }
  84.  
  85. WorTN::~WorTN( ) {
  86.  
  87.    cout <<" \n WorTN destructor fx flushed object containing variable value "<< CustName <<"\n\n_________________________________________________________________________"<<endl;}
  88.  
  89.  
  90. //____________________________________________________________________________
  91.  
  92.                                //BTN.h interface                              
  93.  
  94. class BTN : public WorTN { 
  95.  
  96. public:
  97.    int NumWrkLines;
  98.  
  99.    BTN( ) {cout <<"\n BTN default constructor proof\n";}
  100.  
  101.    BTN( int e );
  102.  
  103.    void cust_num_lines( );
  104.  
  105.   ~BTN( );
  106.  
  107.    int GetNumWrkLines( ) const { return NumWrkLines; }
  108.    void SetNumWrkLines( int k ) { NumWrkLines = k; }
  109. };
  110.  
  111. BTN::BTN( int e ) { NumWrkLines = e; }     
  112.  
  113. void BTN::cust_num_lines( ) { 
  114.  
  115.    cout << " \n BTN parameterized constructor proof\n\n Type number of lines then press 'ENTER' key  ";
  116.    cin >> NumWrkLines;
  117.  
  118.    cout <<" \n No# of lines: "<< NumWrkLines <<endl;  }
  119.  
  120. BTN::~BTN( ) {
  121.  
  122.    cout <<" \n BTN destructor fx flushed object containing variable value "<< NumWrkLines <<"\n\n_________________________________________________________________________"<<endl;}
  123.  
  124. //___________________________________________________________________________________
  125.  
  126.                        //Tnum_main.cpp utilization
  127.  
  128. ostream &operator<<(ostream &print1, Tnum m) {
  129.    print1 << "\n Telephone no#: (" << m.NPA << ") ";
  130.    print1 << m.NXX << "-" << m.LINE << "\n";   
  131.    return print1; }
  132.  
  133. ostream &operator<<(ostream &print2, WorTN n) {
  134.    print2 << "\n Customer: "<< n.CustName << "\n";
  135.    return print2; }
  136.  
  137. ostream &operator<<(ostream &print3, BTN o) {
  138.    print3 << "\n No# phone lines: "<< o.NumWrkLines << "\n"; 
  139.    return print3; }
  140.  
  141. ostream& T( ostream& p ) { return p <<'\a'; }   
  142.  
  143. int main( ) {
  144.  
  145.    Tnum xx;     xx.ph_num( );          xx.~Tnum( ); 
  146.    WorTN yy;    yy.cust_num( );        yy.~WorTN( );
  147.    BTN zz;      zz.cust_num_lines( );  zz.~BTN( );
  148.  
  149.    Tnum x( "(312) ","777-","1111" );
  150.    Tnum y( "(312) ","444-","2222" );
  151.    cout << x << y <<T<<T<<T<<T<<"\n__________________________\n";
  152.  
  153.    WorTN q( "Security" );
  154.    WorTN r( "Library" );
  155.    cout << q << r <<T<<T<<T<<T<<"\n__________________________\n";
  156.  
  157.    BTN s( 10 );
  158.    BTN t( 8 );
  159.    cout << s << t <<T<<T<<T<<T<<"\n__________________________\n";
  160.  
  161. system ("pause");     
  162. return 0;             
  163. }                     
Feb 1 '10 #1
Share this Question
Share on Google+
3 Replies


P: 52
Your question is not understandable, please explain in detail
Feb 1 '10 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Expand|Select|Wrap|Line Numbers
  1. int main( ) { 
  2.  
  3.    Tnum xx;     xx.ph_num( );          xx.~Tnum( );  
  4.    WorTN yy;    yy.cust_num( );        yy.~WorTN( ); 
  5.    BTN zz;      zz.cust_num_lines( );  zz.~BTN( ); 
Why are you calling destructors? These functions are there for the compiler to call and not you. What you are doing is trashing the objects you have just created.

All you need is:

Expand|Select|Wrap|Line Numbers
  1. int main( ) { 
  2.  
  3.    Tnum xx;
  4.    WorTN yy; 
  5.    BTN zz;  
Feb 1 '10 #3

P: 4
Thank you 'weaknessforcats' - i will give that a whirl
Feb 1 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.