Help with using a Class functionality within another functionality

This is a discussion on Help with using a Class functionality within another functionality within the C++ Programming forums, part of the General Programming Boards category; Each functionality of the class should do only one thing. So I am trying to get the stuff from the ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    55

    Help with using a Class functionality within another functionality

    Each functionality of the class should do only one thing. So I am trying to get the stuff from the functionality get_monthly_total inside the functionality of show_info. However, I have been unsuccessful at all of my attempts. I just don't know the syntax to accomplish this. Can someone please provide a suggestion? Thanks.

    Code:
    # include <iostream>
    # include <string>
    # include <cstdlib>
    using namespace std ;
    
    
    class Standard_Plan
    {
          public :
                 void input_hours_online () ;
                 void show_info () ;
                 double get_monthly_total () ;
          
          private :
                  double the_hours_online ;
                  
    } ;
    
    
    class Premium_Plan
    {
          public :
                 void input_hours_online () ;
                 void show_info () ;
                 double get_monthly_total () ;
          
          private :
                  double the_hours_online ;
                  
    } ;
                  
    
    
    int main ()
    {
        cout << "This will Calculate your Monthly Bill." <<endl ;
        
        
        // This will determine whether user is in Standard Plan or Premium Plan.
        
        char response ;
        cout << "Are you Subscribed to the Standard or Premium Plan? Enter s or     
    p: " ;
        cin >> response ;
        
        if (response == 's')
        {
          Standard_Plan  aStandardPlan ;
          aStandardPlan . input_hours_online () ;
          aStandardPlan . show_info () ;          
        }
        
        else
             {
               Premium_Plan  aPremiumPlan ;
               aPremiumPlan . input_hours_online () ;
               aPremiumPlan . show_info () ; 
             }
    
         
        system("PAUSE") ;
    
        return 0 ;
    }
    
    
    
    
     void Standard_Plan :: input_hours_online () 
     {
          cout << "Enter Number of Hours Used: " ;
          cin >> the_hours_online ;
          cout << "You are Subscribed to the Standard Plan" ;      
     }
    
    
     void Premium_Plan :: input_hours_online () 
     {
          cout << "Enter Number of Hours Used: " ;
          cin >> the_hours_online ;
     }
     
    void Standard_Plan :: show_info () 
     {
       cout << get_monthly_total () ;                  
     }
    
    void Premium_Plan :: show_info () 
     {
      cout << get_monthly_total () ;    
     }
    
    double get_monthly_total ()
    {
          double base_charge ;
          double additional_charge ;
          double premium_discount ;
          char response ;
          int the_hours_online ;
          
          if (response == 's')
           {
                       // This will calculate charges for Standard Plan
                     
                     if (the_hours_online <= 20)
                     {
                         base_charge = 20.00 ;
                         cout << "Fixed Monthly Charge is: $" << base_charge << endl ;
                         cout << "Total Monthly Bill is: $" << base_charge << endl ;
                     }
                                                            
                     if (the_hours_online > 20)
                     {
                         base_charge = 20.00 ;
                         additional_charge = (the_hours_online - 20) * 00.50 ;
                         cout << "Fixed Monthly Charge is: $" << base_charge << endl ;
                         cout << "Additional Charges: $" << additional_charge << endl ;
                         cout << "Total Monthly Bill is: $" << base_charge + additional_charge << endl ;
                     } 
           }
           
          else
           
           {
           // This will calculate charges for Premium Plan
                     
                     if (the_hours_online <= 30)
                     {
                         base_charge = 25.00 ;
                         additional_charge = 0.00 ;
                         premium_discount = (base_charge + additional_charge) * .10 ;
                         cout << "Fixed Monthly Charge is: $" << base_charge << endl ;
                         cout << "10% Discount off Bill: $" << premium_discount << endl ;
                         cout << "Total Monthly Bill is: $" << base_charge - premium_discount << endl ;
                     }
                      
             
                     if (the_hours_online > 30)
                     {
                         base_charge = 25.00 ;
                         additional_charge = (the_hours_online - 30) * 00.50 ;
                         premium_discount = (base_charge + additional_charge) * .10 ;
                         cout << "Fixed Monthly Charge is: $" << base_charge << endl ;
                         cout << "Additional Charges: $" << additional_charge << endl ;
                         cout << "10% Discount off Bill: $" << premium_discount << endl ;
                         cout << "Total Monthly Bill is: $" << (base_charge + additional_charge) - premium_discount << endl ;
                     }
    
           }
    }
    Last edited by Ken Fitlike; 09-23-2006 at 04:28 PM. Reason: removed the extra page of whitespace in code tags

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You really don't need so many newlines at the end of your program . . . .

    Code:
    double get_monthly_total ()
    Is that supposed to be prefixed with a class?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > However, I have been unsuccessful at all of my attempts.
    Start by reading about inheritance

    You have a plan (stuff common to all plans)

    From that, you derive two specialised plans called 'standard' and 'premium'.

    For example, your input_hours_online() is essentially the same code for both, so it would be in the plan base class. Doing this, you would only have to write the code once, not copy/paste it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by Salem
    > However, I have been unsuccessful at all of my attempts.
    Start by reading about inheritance

    For example, your input_hours_online() is essentially the same code for both, so it would be in the plan base class. Doing this, you would only have to write the code once, not copy/paste it.

    The assignment mentioned that we would be doing inheritance at the end of the semester. So for now, we are only supposed to use standard classes.


    [QUOTE]
    You really don't need so many newlines at the end of your program . . . .
    [QUOTE]

    I have a tendency to do this. After everyting is working I go back and format things correctly with respect to new lines.

    Code:
    double get_monthly_total ()Is that supposed to be prefixed with a class?
    double get_monthly_total () is a functionality of both classes. I was wondering if it was possible to use that functionality within the show_info () functionality of the classes. Something like:

    Code:
    void Standard_Plan :: show_info () 
     {
       cout << get_monthly_total () ;                  
     }
    
    void Premium_Plan :: show_info () 
     {
      cout << get_monthly_total () ;    
     }
    so that in the main block, apremiumplan . show_info () for example, would output the calculations and stuff from get_monthly_total () .

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Yes, you can call a common function from two different class member functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by Salem
    Yes, you can call a common function from two different class member functions.

    This does not work.

    Code:
    void Standard_Plan :: show_info () 
     {
       cout << get_monthly_total () ;                  
     }
    
    void Premium_Plan :: show_info () 
     {
      cout << get_monthly_total () ;    
     }
    I have tried just about everything, but no success. Can you tell me what the general syntax is so this way I can fix the program? Thanks.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You didn't remove the member functions with the same name
    Code:
    # include <iostream>
    # include <string>
    # include <cstdlib>
    using namespace std;
    
    //!! A common function, not in any class
    double get_monthly_total();
    
    class Standard_Plan {
      public:
        void input_hours_online();
        void show_info();
        //!! double get_monthly_total () ;
    
      private:
        double the_hours_online;
    
    };
    
    
    class Premium_Plan {
      public:
        void input_hours_online();
        void show_info();
        //!! double get_monthly_total () ;
    
      private:
        double the_hours_online;
    
    };
    
    
    
    int main()
    {
        cout << "This will Calculate your Monthly Bill." << endl;
    
        // This will determine whether user is in Standard Plan or Premium Plan.
        char response;
        cout <<
            "Are you Subscribed to the Standard or Premium Plan? Enter s or p: ";
        cin >> response;
    
        if (response == 's') {
            Standard_Plan aStandardPlan;
            aStandardPlan.input_hours_online();
            aStandardPlan.show_info();
        } else {
            Premium_Plan aPremiumPlan;
            aPremiumPlan.input_hours_online();
            aPremiumPlan.show_info();
        }
    
    
        system("PAUSE");
    
        return 0;
    }
    
    
    
    
    void Standard_Plan::input_hours_online()
    {
        cout << "Enter Number of Hours Used: ";
        cin >> the_hours_online;
        cout << "You are Subscribed to the Standard Plan";
    }
    
    
    void Premium_Plan::input_hours_online()
    {
        cout << "Enter Number of Hours Used: ";
        cin >> the_hours_online;
    }
    
    void Standard_Plan::show_info()
    {
        cout << get_monthly_total();
    }
    
    void Premium_Plan::show_info()
    {
        cout << get_monthly_total();
    }
    
    double get_monthly_total()
    {
        double base_charge;
        double additional_charge;
        double premium_discount;
        char response;
        int the_hours_online;
    
        if (response == 's') {
            // This will calculate charges for Standard Plan
    
            if (the_hours_online <= 20) {
                base_charge = 20.00;
                cout << "Fixed Monthly Charge is: $" << base_charge << endl;
                cout << "Total Monthly Bill is: $" << base_charge << endl;
            }
    
            if (the_hours_online > 20) {
                base_charge = 20.00;
                additional_charge = (the_hours_online - 20) * 00.50;
                cout << "Fixed Monthly Charge is: $" << base_charge << endl;
                cout << "Additional Charges: $" << additional_charge << endl;
                cout << "Total Monthly Bill is: $" << base_charge +
                    additional_charge << endl;
            }
        }
    
        else
        {
            // This will calculate charges for Premium Plan
    
            if (the_hours_online <= 30) {
                base_charge = 25.00;
                additional_charge = 0.00;
                premium_discount = (base_charge + additional_charge) * .10;
                cout << "Fixed Monthly Charge is: $" << base_charge << endl;
                cout << "10% Discount off Bill: $" << premium_discount << endl;
                cout << "Total Monthly Bill is: $" << base_charge -
                    premium_discount << endl;
            }
    
    
            if (the_hours_online > 30) {
                base_charge = 25.00;
                additional_charge = (the_hours_online - 30) * 00.50;
                premium_discount = (base_charge + additional_charge) * .10;
                cout << "Fixed Monthly Charge is: $" << base_charge << endl;
                cout << "Additional Charges: $" << additional_charge << endl;
                cout << "10% Discount off Bill: $" << premium_discount << endl;
                cout << "Total Monthly Bill is: $" << (base_charge +
                                                       additional_charge) -
                    premium_discount << endl;
            }
    
        }
        //!! MUST return a result
        return base_charge + additional_charge;
    }
    Study the //!! comments
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21