Thread: first class

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    first class

    I am attempting to compile this file but do not understand even the first error. This is my first attempt at using a class in c++

    Code:
    //bank account prblm #1 c++ primer plus
    #include <iostream>
    
    #include <cstring>
    #ifndef _BANKH_CPP_
    #define _BANKH_CPP_
    using namespace std;
    
    class Account
    {
    	private:
    		char name[40];
    		char acctnum[25];
    		double balance;
    	public:
    	    Account(char * client, char * num, double bal=0.0);
    		void set(void);
    		void show(void) const;
    		void deposite(double cash);
    		void withdraw(double cash);
    		~Account();
    };
    
    // class constructor
    Account::Account (char * client, char * num, double bal)
    { 
        strncpy(name,client,40);
        name[40]='\0';
        strncpy(acctnum,num,25);
        acctnum[25]='\0';
        balance=bal;
    }
    
    Account::set()
    {
        name="John Doe";
        acctnum="0";
        balance=0.0;
    }
    
    Account::show()
    {
        cout << "Name:" << name
             << "\t Account ID:" << acctnum
             << "\n\t Balance:" << balance;
    }
    
    Account::void deposite(double cash)
    {
        balance+=cash;
    }
    
    Account::void withdraw(double cash)
    { 
        double temp;
        temp=balance;
        if (temp-cash<0)
        {
            cout << "You do not have enough money in your account!\n";
        }
        else
        {
            balance-=cash;
        }
    }
    // class destructor
    Account::~Account(){}
    #endif
    C:\Borland\BCC55\src>bcc32 bankh.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    bankh.cpp:
    Error E2316 bankh.cpp 35: 'Account::set()' is not a member of 'Account'
    Error E2277 bankh.cpp 36: Lvalue required in function Account::set()
    Error E2277 bankh.cpp 37: Lvalue required in function Account::set()
    Error E2316 bankh.cpp 42: 'Account::show()' is not a member of 'Account'
    Error E2206 bankh.cpp 44: Illegal character '\' (0x5c)
    Error E2206 bankh.cpp 45: Illegal character '\' (0x5c)
    Error E2206 bankh.cpp 45: Illegal character '\' (0x5c)
    Error E2272 bankh.cpp 48: Identifier expected
    Error E2040 bankh.cpp 48: Declaration terminated incorrectly

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    you're on the right track.....

    little syntax errors......

    your declrarations don't match your definitions...... i am talking about your member functions....... i.e.

    inside your class you write:

    void set(void);

    outside your class you write:

    Account::set();

    instead u should write:

    void Account::set();

    ...............

    similar with the other functions....

    instead of writing:

    Account::void deposite(double cash)

    you should write :

    void Account::deposite(double cash)


    ...................................


    fix the other function definitions and you should be O.K........

    hint: one error can trigger more errors even though they're not really errors, compiler just gets too confused and at the end it just gives up......

    good luck

    Regards,
    matheo917

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Look at declaration and the first line of your definition. They would be the first thing i would be fixing. ie consistency.

    Second look at your declaration of name

    ie char name[40]

    look how you use it

    name="John Doe";


    see the inconsistency.


    If you still can't get what I mean maybe you should check your textbook.


    kwigibo

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    matheo917 you obviously started typing before me damn...

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    hmmmmm :-)

    well, hopefully we both were able to help this guy......

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    thnks one last error

    I decided to simplify things by including everything in one file for now. I have one last error in the program i con-t figure out.

    Code:
    //bank account prblm #1 c++ primer plus
    #include <iostream>
    
    #include <cstring>
    
    using namespace std;
    
    class Account
    {
    	private:
    		char name[40];
    		char acctnum[25];
    		double balance;
    	public:
    	    Account(char * client, char * num, double bal=0.0);
    		void set(void);
    		void show(void) const;
    		void deposite(double cash);
    		void withdraw(double cash);
    		~Account();
    };
    
    // class constructor
    Account::Account(char * client, char * num, double bal)
    { 
        strncpy(name,client,40);
        name[40]='\0';
        strncpy(acctnum,num,25);
        acctnum[25]='\0';
        balance=bal;
    }
    
    void Account::set()
    {
        strncpy(name,"John Doe\0",40);
        strncpy(acctnum,"0\0",25);
        balance=0.0;
    }
    
    void Account::show() const
    {
        cout << "Name:" << name
             << "\t Account ID:" << acctnum
             << "\n\t Balance:" << balance;
    }
    
    void Account:: deposite(double cash)
    {
        balance+=cash;
    }
    
    void Account::withdraw(double cash)
    { 
        double temp;
        temp=balance;
        if (temp-cash<0)
        {
            cout << "You do not have enough money in your account!\n";
        }
        else
        {
            balance-=cash;
        }
    }
    // class destructor
    Account::~Account(){}
    
    
    int main()
    {
        double deposit=0.0;
        double withdra=0.0;
        
        Account checking=("Troy","A609",12.25);\\line 74 error here???
        checking.show();
        checking.set();  
        checking.show();
        cout << "Enter deposite:";
        cin >> deposit;
        checking.deposite(deposit);
        cout << endl;
        checking.show();
        cout << "Enter withdrawl:";
        cin >> withdra;
        checking.withdraw(withdra);
        cout << endl;
        checking.show();
        return 0;
    }
    C:\Borland\BCC55\src>bcc32 bankh.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    bankh.cpp:
    Error E2034 bankh.cpp 74: Cannot convert 'double' to 'Account' in function main(
    )
    *** 1 errors in Compile ***

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    1) Drop the equals sign.

    2) Either drop the balance when you initiate an instance of account because you already declare the value in your prototype or drop the =0 from balance in the constructor.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    thnks again

    It's amazing how other people can spot your errors. So I assume if you set a default in the prototype you can not override this as I tryed to do. Thanks for all your help!!!

  9. #9
    Unregistered
    Guest
    you can override default values in the prototype, your last error was caused by the = operator.

    A constructor like this should work fine -
    Code:
    Account checking("Troy","A609",12.25);

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, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM