print derived class object contents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LamontSharp
    New Member
    • Jan 2010
    • 4

    print derived class object contents

    This code compiles nicely - i am looking for print methods to make the output print the format shown below - i am looking for a function or method of calling the existing code objects to make them display like the below example - 'weaknessforcat s' has given great advice but we can still add a function to that main() to get a display from the current objects in this type of format

    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


    Code:
    // 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( "(303) ","777-","1111" );
       Tnum y( "(303) ","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;              
    }
  • LamontSharp
    New Member
    • Jan 2010
    • 4

    #2
    print derived class object contents

    This code compiles nicely - i am looking for print methods to make the output print the format shown below - i am 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


    Code:
    // 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( "(303) ","777-","1111" );
       Tnum y( "(303) ","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;              
    }

    Comment

    Working...