Class problem...I think its the constructor.

This is a discussion on Class problem...I think its the constructor. within the C++ Programming forums, part of the General Programming Boards category; I wrote up the code for the class, despite not really knowing what a constructor truely is I went ahead ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Class problem...I think its the constructor.

    I wrote up the code for the class, despite not really knowing what a constructor truely is I went ahead and took a wild guess. When I got to main and created the class objects (account bal, account acc) the compiler gives me errors about no matching function for the call to the constructor.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Account {
       public:
              Account(int);            //Constructor
              int Debit(int);       //Member Function
              int Credit(int);      //Member Function
              int getBalance();     //Member Function
       private:
               int Balance; //= 1000;  //Data Member
               };
    
    /*Account constructor makes sure there are no overdraws and the account
      balance is always zero or above */
    Account::Account(int AmtDebit){
        if (Balance < AmtDebit){
           cout <<"Debit ammount exceeded account balance" <<endl;
           }
        if (Balance < 0){
           cout <<"Initial account balance invalid" <<endl;
           Balance = 0;
           }
       }
       
    // Account debit member function declaration.  Asks for amount to debit and subtracts from balance
    int Account::Debit(int AmtDebit){
        cout <<"How much do you want to withdraw?" <<endl;
        cin >>AmtDebit;
        Balance = Balance - AmtDebit;
        cout <<"The new account balance is " <<Balance <<endl;
    }
    
    // Account creddit member function declaration.  Asks for amount to credit and adds to balance
    int Account::Credit(int AmtCredit){
        cout <<"How much do you want to deposit?" <<endl;
        cin >>AmtCredit;
        Balance = Balance + AmtCredit;
        cout <<"The new account balance is " <<Balance <<endl;
    }
    
    // Output current balance
    int Account::getBalance() {
        cout <<"Account balance is " <<Balance <<endl;
    }
    
    int main(){
        Account acc;            // Account object to test debit and credit
        Account bal;            // Account object to test balance member function and data member
        int x;                  // Menu option control variable
        int amount;             // Amount to credit/debit
        
        cout <<"====================================================" <<endl;
        cout <<"=            Welcome to your ATM                   =" <<endl;
        cout <<"====================================================" <<endl;
        cout <<"+            1. View balance                       +" <<endl;
        cout <<"+            2. Withdraw                           +" <<endl;
        cout <<"+            3. Deposit                            +" <<endl;
        cout <<"+            4. Exit                               +" <<endl;
        cout <<"++++++++++++++++++++++++++++++++++++++++++++++++++++" <<endl;
        cout <<"Select an option (1-4) and press enter." <<endl;
        cin >>x;
        
        // View balance
        if (x == 1)
           {
              bal.getBalance();
           }
           
        if (x == 2)
           {
              cout <<"How much do you wish to withdraw?" <<endl;
              cin >>amount;
              acc.Debit(amount);
           }
           
        if (x == 3)
           {
              cout <<"How much do you with to deposit?" <<endl;
              cin >>amount;
              acc.Credit(amount);
           }
           
        if (x == 4)
           {
              cout <<"Thank you! Come again!" <<endl;
           }
           
        if (x > 4)
           {
              cout <<"Invalid option, please try again." <<endl;
              cin >>x;
           }
    
    return 0; 
    }
    I am thinking that I am not using the contructor properly.

    NOTE** I realize that I can make this code a lot shorter but the directions forbid me to use anything the class hasn't taught me.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    You need to overtly specify a default constructor.

    Code:
    class Account {
       public:
              Account(){int balance = 0;}          // Default Constructor
              Account(int);            //Constructor
              ...
    Because you are calling the default constructor in main():

    Code:
    int main(){
        Account acc;            // Account object to test debit and credit
        Account bal;
    It would work if you did it like this, though:
    Code:
    int main(){
        Account acc(10);            // Account object to test debit and credit
        Account bal(11);
    But that would be beside the point of the assignment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Also, your program wouldn't really work because it doesn't loop. Once you deposit your money, you'd have to check out the door.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    It because you constructor is expecting an int and you are passing it nothing.
    Woop?

  5. #5
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by cunnus88
    Also, your program wouldn't really work because it doesn't loop. Once you deposit your money, you'd have to check out the door.
    I understand that but the class has not learned about loops.

    I think it misunderstood the question that was asked.

    Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the constructor should display an error message, indicating that the initial balance was invalid.
    Would I be correct in assuming that this is they way the problem is asking me to do it? (Sorry, I still suck at C++ lingo)
    Code:
    Account::Account(){
        Balance = 1000;
        if (Balance < 0)
        {
           cout <<"Initial balance invalid" <<endl;
           Balance = 0;
        }
    }
    Knowledge is power and I want it all

    -0RealityFusion0-

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Well, it says the constructor recieves a balance, not creates one. How do you make a function recieve something? Also you're initializing your balance to zero if it's less than 0, but what about the cases where it's greater than or equal to. You still have to initialize your data member there.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    First, your call to the constructor should occur after the input section of the code. And you should call the constructor with the integer argument.
    Code:
        ...
        cin >>x;
        Account acc(x);
        ...
    And your constructor would need to check if the deposited amount was less than or equal to zero.

    Code:
    Account::Account(int deposit)
    {
        if(deposit > 0)
        {
            set balance to deposit amount;
        }
        else
        {
            set balance to zero;
            print error message;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  2. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  3. Weird class problem!
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2002, 07:12 AM
  4. Virtual Base Class & Constructor :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 04:14 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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