c++ - Print derived class object contents
- biggcarpenter (132/132) Feb 01 2010 Hi there, I've been working on this base class and 2 derived classes so ...
Hi there, I've been working on this base class and 2 derived classes so far everything works except for when I try to get a display that looks like this [CODE] Customer: Security Customer: Security ___________________________________ Customer: Library Customer: Library [/CODE] Can anyone point me in the right direction or offer resources that explain this aspect of printing derived class object contents? Here is my code, the program compiles nicely - i am looking for print methods to make the output print the format shown above - i am looking for a function or method of calling the existing code objects to make them display like the above example. Thanks from ~L [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; 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; 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 << 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) { 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; } [/CODE]
Feb 01 2010