Help with constructors!

This is a discussion on Help with constructors! within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, it's been a long time since I've been here. I've come along way with C++, but I'm stuck ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    13

    Help with constructors!

    Hi everyone, it's been a long time since I've been here. I've come along way with C++, but I'm stuck with a problem I can't come to figure out: constructor use.

    NOTE: I'm not asking you to do the whole problem for me, but simply help with using the constructor correctly!

    Here's the problem:
    http://www.filecram.com/files/03-20-2008 11;32;08AM.JPG

    This is what I have so far:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class SavingAccount
    {
       private:
       string IdNo,
              name;
       double balance,
              rate;
       public:
       //SavingAccount(string UserID, string UserName);
       string GetId();
    };
    
    string SavingAccount::GetId()
    { 
       cout <<"Please enter your I.D. number." << endl;
       cin  >> 
           
    }
    
    
    
    class ATM
    {
       public:
       SavingAccount amount;
          
    };
    
    class User
    {
       public:
       ATM UserAccess;
          
    };
    int main()
    {
        User UserInfo;
        
        cout <<"AUTOMATIC TELLER MACHINE PROGRAM" << endl;
        cout <<"--------------------------------" << endl;
        cout <<"This program works like an ATM (automatic teller machine) - allowing the user to";
        cout <<"withdraw money from their account. We will withdraw $100.00 from the account." << endl;
        UserInfo.UserAccess.amount.GetId(); 
    
    system("pause");
    return 0;   
    }
    I know how to use construtors within a class and nested classes, but the problem I'm having are using the three functions - string GetId(), string GetName(), double GetBalance() - with the constructor. How am I suppose to pass the values called by each of the function when they all have a return type?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    A constructor is used to instantiate an object. If you already have a SavingAccount object, it implies that the SavingAccount constructor has been used. But if you do not have a SavingAccount object, you cannot use its GetId() member function.

    To resolve this paradox, do not ask for user input in GetID(). Instead GetID() would just return the IdNo. Rather, from outside the class, get the user input and use that to create a SavingAccount object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Quote Originally Posted by laserlight View Post

    To resolve this paradox, do not ask for user input in GetID(). Instead GetID() would just return the IdNo. Rather, from outside the class, get the user input and use that to create a SavingAccount object.
    I was thinking of using that method, but I thought the "Get" functions were used to get information from the user by input, not return it; I think that's what "Accessors" were for. Could I just simply create variables in function main and pass them off in the SavingAccount constructor?

    Code:
    int main()
    {
         ATM UserInfo;
        string UserID,
        string UserName;
    
        cout <<"AUTOMATIC TELLER MACHINE PROGRAM" << endl;
        cout <<"--------------------------------" << endl;
        cout <<"This program works like an ATM (automatic teller machine) - allowing the user to";
        cout <<"withdraw money from their account. We will withdraw $100.00 from the account." << endl;
       cout <<"Please enter your I.D. number." << endl;
       cin  >> UserID;
       cout <<"Please enter your name." << endl;
       getline(cin,UserName);
       UserInfo.UserAccess.amount.SavingAccount(UserID,UserName);
    
    system("pause");
    return 0;   
    }
    Will this function, UserInfo.UserAccess.amount.SavingAccount(UserID,Us erName);, work? If not, I'm simply create member functions within the SavingAccount class to aquire the user data. Thanks for your help!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    I was thinking of using that method, but I thought the "Get" functions were used to get information from the user by input, not return it; I think that's what "Accessors" were for.
    GetID() is an accessor.

    Could I just simply create variables in function main and pass them off in the SavingAccount constructor?
    Yes, you could. You could also write a free (non-member) function that asks for user input, creates and returns a SavingAccount object. Or you could create a default SavingAccount object, then assign to its member variables via mutators.

    Will this function, UserInfo.UserAccess.amount.SavingAccount(UserID,Us erName);, work?
    Try before you ask.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Get returns data, Set sets data. Anything else will just confuse people.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    If you look back at the original question, it says,"You have to define one class for each object. Each object should contain, as a minimum, the following attributes and member functions." Am I suppose to use a nested class to access member functions from the SavingAccount class? For example, in main, I'm using an ATM object called UserData to access member functions in the SavingAccount class.

    *Area of interest in bold red.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class SavingAccount
    {
        private:
        string name,
               IdNo;
        double balance,
               rate;
        
        public:
        //Constructor; Initializes the user's name, I.D., and balance
        SavingAccount(string UserID, string UserName, double bal = 0.00);
        //Destructor
        ~SavingAccount();
      bool Withdraw(double);
        
    };
    
    SavingAccount::SavingAccount(string UserID, string UserName, double bal)
    {
        name = UserName;
        IdNo = UserID;
        balance = bal;
    }
    
    SavingAccount::~SavingAccount()
    {
                                     
    }
    
    bool SavingAccount::Withdraw(double amount)
    {
      cout <<"Test" << endl;     
    }
    class ATM
    {
       public:
       SavingAccount amount;
          
    };
    int main()
    {
        string UserName,
               UserID;
               
        cout <<"AUTOMATIC TELLER MACHINE PROGRAM" << endl;
        cout <<"--------------------------------" << endl;
        cout <<"This program works like an ATM (automatic teller machine) - allowing the user to";
        cout <<"withdraw money from their account. For this exercise, we will withdraw $100.00." << endl;
        cout << endl;
        
        //Get User's Name and I.D. number
        cout <<"Please enter your name." << endl;
        getline(cin,UserName);
        cout << endl;
        cout <<"Please enter your I.D. number." << endl;
        cin  >> UserID;
        cout << endl;
        
        //Stores User's Name and I.D. number within constructor
        SavingAccount UserData(UserID, UserName);
        
        //Message sent to ATM object to withdraw $100.00
        ATM UserAccess.amount.Withdraw(100.00);    
    system("pause");
    return 0;    
    }
    Everytime I do this, I get errors:
    - expected primary-expression before "UserAccess"
    - expected `;' before "UserAccess"
    Last edited by -Syntax; 03-24-2008 at 07:02 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    What do you think the code you highlighted in main() should do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Quote Originally Posted by laserlight View Post
    What do you think the code you highlighted in main() should do?
    It's suppose to access the SavingAccount class, from the ATM class, which then stores the 100.00 inside the member function WithDraw. Whenever I run the program it says I have two errors, which are listed below.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    It's suppose to access the SavingAccount class, from the ATM class, which then stores the 100.00 inside the member function WithDraw.
    However, what it looks like is an attempt to declare an ATM object named UserAccess.amount.Withdraw, but of course such a variable name is not allowed.

    You need to create an ATM object first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Quote Originally Posted by laserlight View Post
    However, what it looks like is an attempt to declare an ATM object named UserAccess.amount.Withdraw, but of course such a variable name is not allowed.

    You need to create an ATM object first.
    I don't know why, but when I create an ATM object, I get an error when I try to compile the program. Here's what I have so far:
    Code:
    class ATM
    {
       public:
       SavingAccount amount;
          
    };
    
    
    
    int main()
    {
        string UserName,
               UserID;
               
        cout <<"AUTOMATIC TELLER MACHINE PROGRAM" << endl;
        cout <<"--------------------------------" << endl;
        cout <<"This program works like an ATM (automatic teller machine) - allowing the user to";
        cout <<"withdraw money from their account. For this exercise, we will withdraw $100.00." << endl;
        cout << endl;
        
        //Get User's Name and I.D. number
        cout <<"Please enter your name." << endl;
        getline(cin,UserName);
        cout << endl;
        cout <<"Please enter your I.D. number." << endl;
        cin  >> UserID;
        cout << endl;
        
        //Stores User's Name and I.D. number within constructor
        SavingAccount UserData(UserID, UserName);
        
        //Message sent to ATM object to withdraw $100.00
        ATM UserAccess;
    The ATM object is "UserAccess." Whenever I try to do an extension - or whatever the proper name is - to call the function withdraw in the SavingAccount class, I get an error. For example, here's what I did earlier to access the data.
    Code:
    ATM UserAccess  // UserAccess class object
    UserAccess.amount.Withdraw(100.00)
    I'm seriously stumped and this one problem is messing me up. Thanks for the help laserlight. I really appreciate it!

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    As dubious as it may sound, an ATM needs to create it's savings account member named "amount" in order to exist. That is what needs to happen when you create an ATM object. I'm actually quite bothered that your instructor has asked you to do it this way, but the constructor prototype he gave

    SavingAccount (string name, string id, double rate, double bal = 0.00);

    suggests that he expects you to create all the bank's savings accounts before an ATM machine exists, and the ATM machine is supposed to use one of them through it's own member. If I may may repeat a solution:
    Quote Originally Posted by laserlight
    you could create a default SavingAccount object, then assign to its member variables via mutators.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Quote Originally Posted by citizen View Post
    I'm actually quite bothered that your instructor has asked you to do it this way, but the constructor prototype he gave

    SavingAccount (string name, string id, double rate, double bal = 0.00);
    I agree as well. I could simply do this with one class alone using a constructor and destructor without the hassle of using multiple classes and objects. Thanks citizen!

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    No, to do OOP properly, you have to understand how classes can work with each other. Your teacher is asserting that an ATM "has a" savings account; that is, if you wanted to understand what an ATM is, a savings account has to be one of it's parts. That's where I don't agree. You could express the relationship that ATMs have with bank accounts in a cleaner way.

    But since I am not the teacher and you are the student, we have to follow the teacher's rules. And while I don't like it very much, laserlight came up with an idea that works fine. The idea is that you should build a member function for ATM that manages the account it's using.

    For example, here's an outline of what to expect.

    Code:
    #include <string>
    #include <iostream>
    
    class SavingAccount
    {
    public:
        SavingAccount (); // implement a default constructor
        SavingAccount (string name, string id, double rate, double bal = 0.00);
    
        string GetName();
        string GetID();
        double GetBalance();
        double GetRate();
    
        // other required members here...
        // perhaps some private ones?
    };
    
    class ATM
    {
    public:
        void Reset (const SavingAccount & saver);
        // other members...
    private:
        SavingAccount amount;
        // ...
    };
    
    // set the ATM machine to work with saver's account after you prove
    // your identity.
    void ATM::Reset (const SavingAccount & saver)
    {
        // transfer the saver data.
        idNo = saver.GetID();
        name = saver.GetName();
        rate = saver.GetRate();
        balance = saver.GetBalance();
    
        return ;
    }
    Initially nor most of the time, the ATM isn't looking at a specific account that you should be using, so it must be reset to one before a withdraw is attempted and so forth.
    Last edited by whiteflags; 03-24-2008 at 10:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:20 PM
  2. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  4. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 07:35 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM

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