473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 - 'weaknessforcats' 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


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


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

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

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
1
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition...
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
9
by: James Wong | last post by:
Hi, I use the RichTextBox in my program. It will use different language in this RichTextBox (chinese and english characters), and it set the "DualFont" and use different fonts. By the way, how...
1
by: sasha | last post by:
class Base{ public: std::ostream& operator<<( std::ostream& os, const string &str ) { return print(os); } void print(std:ostream& os){ os<<"Base\n";} };
10
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.