Thread: This is my Homework

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Lightbulb This is my Homework

    Problem Statement:

    Define a class to represent a bank account. Include the following members:

    1. Name of the depositor
    2. Account Number
    3. Type of account
    4. Balance amount in the account

    Member Functions

    1. To assign initial values
    2. To deposit an amount
    3. To withdraw an account after checking the balance
    4. To display name and balance

    Write a main() function to test your program

    What I did:


    Code:
    #include<iostream>
    
    class account
    {
           private:
                       string name;
                       int acnum;
                       bool type;   //0 for savings, 1 for current account
                       int balance;
            public:
                       account()
                       {
                             cout<<"\nName of customer";
                             cin>>name;
                             cout<<"\nAccount Number";
                             cin>>acnum;
                             cout<<"\nType--->0/1";
                             cin>>type;
                             cout<<"\nInitial balance";
                             cin>>balance;
                         }
                       
                          void deposit()
                         {
                               cout<<"\nDeposit Amount:";
                              cin>>int amount;
    	                   balance+=amount;
                              cout<<"\nDone";
                         }
    
                        void withdraw()
                       {
    	                cout<<"\nWithdraw Amount";
                            cin>>int amount;
    	                if(amount<=balance)
    	               {
    		            balance-=amount;
    		            cout<<"\nDone";
     	               }
    	               else
    	              {
    		            cout<<"\nGet lost @#$&#37;^&*";
    	               }
                    }
    
                  void display()
                {
    	          cout<<endl<<name;
    	          cout<<endl<<balance;
                 }
    Now I have no idea how to write the main function

    Thank you for any help

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You'll need to create an account (instantiate an "account"), then use the member functions to call your methods.

    Sort of like this (not syntax checked):

    Code:
     
    int main() { 
    
    myaccount = new account() ; 
    
    myaccount.deposit() ; 
    
    myaccount.withdraw() ; 
    }
    For deposit(), you want it to accept a positive value.

    For withdraw, you'll want it to withdraw a positive value.

    In C++, you will usually want to cause a newline by appending "endl" to the end of the cout expression.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    But how do I know how many customers the bank will have?

    Code:
    account customer1, customer2 ......//how many?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can keep track of all instances you've created within your class, or within main - either one.

    Each time you create a new instance, your constructor will be called. Perhaps you can create another variable to keep a count?
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Todd Burch View Post
    Code:
     
    int main() { 
    myaccount = new account() ; 
    myaccount.deposit() ; 
    myaccount.withdraw() ; 
    }
    But Todd, that essentially creates it on the heap, which in itself is useless, plus it makes the rest of the syntax wrong. It has to be -> in that case.
    Better would just be
    account myaccount;

    And abk - I see your class all wrong - the class should not ask for the data, not at all. The class is the object - you don't see a bank account asking for how much you want to deposit, do you? The software should do that - your code in main!
    You need to pass the correct data to these functions, not let them ask for it. That is not how object-oriented works.
    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
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    (not syntax checked):
    As I said....
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pretty big error
    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.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by Elysia View Post
    And abk - I see your class all wrong - the class should not ask for the data, not at all. The class is the object - you don't see a bank account asking for how much you want to deposit, do you? The software should do that - your code in main!
    You need to pass the correct data to these functions, not let them ask for it. That is not how object-oriented works.
    I was using constructor account() to initialise the values. What is the other alternative to do this?

    New Code:

    Code:
    #include<iostream>
    using namespace std;
    int menu();
    class account
    {
           private:
                       string name;
                       int acnum;
                       bool type;   //0 for savings, 1 for current account
                       int balance;
            public:
                       account()
                       {
                             cout<<"\nName of customer";
                             cin>>name;
                             cout<<"\nAccount Number";
                             cin>>acnum;
                             cout<<"\nType--->0/1";
                             cin>>type;
                             cout<<"\nInitial balance";
                             cin>>balance;
                            
                         }
                       
                          void deposit()
                         {
                               int amount;
                               cout<<"\nDeposit Amount:";
                              cin>>amount;
    	                   balance+=amount;
                              cout<<"\nDone";
                         }
    
                        void withdraw()
                       {
    	                int amount;
    	                cout<<"\nWithdraw Amount";
                            cin>>amount;  
    	                if(amount<=balance)
    	                   {
    		                 balance-=amount;
    		                 cout<<"\nDone";
     	                    }
    	                else
    	                   {
    		                 cout<<"\nGet lost @#$&#37;^&*";
    	                    }
                       }
    
                  void display()
                {
    	          cout<<endl<<name;
    	          cout<<endl<<balance;
                 }
    };
    
    
    
    int main()
    {
    	account customer1;
    	int flag=menu();
    	if(flag==1)
    		customer1.deposit();
    	if(flag==2)
    		customer1.withdraw();
    	if(flag==3)
    	customer1.display();
    	return 0;
    }
    
    
    int menu()
    {
    	int flag;
    	/*PRINT THE MENU*/
    	cout<<"\nEnter your choice";
    	cin>>flag;
    	return flag;
    }
    But this is only for one customer
    Last edited by abh!shek; 04-06-2008 at 08:41 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A class does not output anything to screen and it does not read anything from the screen either. A bank account cannot talk and it cannot read. It's an object, not human.
    All the cout and cin does not belong in the class.
    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.

  11. #11
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Question

    Code:
    #include<iostream>
    using namespace std;
    int menu();
    class account
    {
           private:
                       string name;
                       int acnum;
                       bool type;   //0 for savings, 1 for current account
                       int balance;
            public:
                       account()
                       {
                            
                           //commented out. Rest of the code unchanged.
                              /* cout<<"\nName of customer";
                             cin>>name;
                             cout<<"\nAccount Number";
                             cin>>acnum;
                             cout<<"\nType--->0/1";
                             cin>>type;
                             cout<<"\nInitial balance";
                             cin>>balance;*/
                            
                         }
                       
                          void deposit()
                         {
                               int amount;
                               cout<<"\nDeposit Amount:";
                              cin>>amount;
    	                   balance+=amount;
                              cout<<"\nDone";
                         }
    
                        void withdraw()
                       {
    	                int amount;
    	                cout<<"\nWithdraw Amount";
                            cin>>amount;  
    	                if(amount<=balance)
    	                   {
    		                 balance-=amount;
    		                 cout<<"\nDone";
     	                    }
    	                else
    	                   {
    		                 cout<<"\nGet lost @#$&#37;^&*";
    	                    }
                       }
    
                  void display()
                {
    	          cout<<endl<<name;
    	          cout<<endl<<balance;
                 }
    };
    
    
    
    int main()
    {
    	account customer1;
    	int flag=menu();
    	if(flag==1)
    		customer1.deposit();
    	if(flag==2)
    		customer1.withdraw();
    	if(flag==3)
    	customer1.display();
    	return 0;
    }
    
    
    int menu()
    {
    	int flag;
    	/*PRINT THE MENU*/
    	cout<<"\nEnter your choice";
    	cin>>flag;
    	return flag;
    }
    Now how do I initialize the values?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How would they do it on a bank? The class is the actual bank account or the computer to it.
    You need something to act as the one inputting things into the computer and you need the computer to accept these inputs and store them somewhere.
    Now there's your task.

    This applies to all functions in your class, not just the constructor.
    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.

  13. #13
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    .....Something is Wrong.......

    It is telling me that name, type, acnum and balance are private, so something is wrong. Dunno what.

    Code:
    #include<iostream>
    using namespace std;
    int menu();
    class account
    {
           private:
                       string name;
                       int acnum;
                       bool type;   //0 for savings, 1 for current account
                       int balance;
            public:
                       account()
                       {
                            /* cout<<"\nName of customer";
                             cin>>name;
                             cout<<"\nAccount Number";
                             cin>>acnum;
                             cout<<"\nType--->0/1";
                             cin>>type;
                             cout<<"\nInitial balance";
                             cin>>balance;*/
                                                    
                            
                            
                        }
                       
                          void deposit(int amountzzz)
                         {
                               /*int amount;
                               cout<<"\nDeposit Amount:";
                               cin>>amount;
    	                       balance+=amount;
                               cout<<"\nDone";*/
                               balance+=amountzzz;
                         }
    
                        void withdraw(int amountzzz)
                       {
    	                /*int amount;
    	                cout<<"\nWithdraw Amount";
                            cin>>amount;  
    	                if(amount<=balance)
    	                   {
    		                 balance-=amount;
    		                 cout<<"\nDone";
     	                    }
    	                else
    	                   {
    		                 cout<<"\nGet lost @#$&#37;^&*";
    	                    }*/
    	                    
    	                 balance-=amountzzz;   
                       }
    
                  void display()  //??
                {
    	          cout<<endl<<name; 
    	          cout<<endl<<balance;
                 }
    };
    
    
    
    int main()
    {
    	int amountzzz;
    	account customer1;
    	cout<<"\nName of customer";
        cin>>customer1.name;
        cout<<"\nAccount Number";
        cin>>customer1.acnum;
        cout<<"\nType--->0/1";
        cin>>customer1.type;
        cout<<"\nInitial balance";
        cin>>customer1.balance;
    	
    	int flag=menu();
    	
    	if(flag==1)
    		{
    			cout<<"\nDeposit Amount:";
    			cin>>amountzzz;		
    			customer1.deposit(amountzzz);
    		}
    			
    	if(flag==2)
    		{
    			cout<<"\nWithdraw Amount";
    			cin>>amountzzz;
    		    if(amountzzz<=balance)
    		        customer1.withdraw(amountzzz);
    		    else
    		        cout<<"\nGet lost @#$%^&*";
    		            
    		}
    	if(flag==3)
    	    customer1.display();
    	return 0;
    }
    
    
    int menu()
    {
    	int flag;
    	/*PRINT THE MENU*/
    	cout<<"\nEnter your choice";
    	cin>>flag;
    	return flag;
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The member variables are placed under the private keyword, which means that anything outside the class can't access them.
    You must read into local variables inside main and pass to the class functions.
    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.

  15. #15
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Talking Accomplished?

    Is this OK

    Code:
    #include<iostream>
    using namespace std;
    int menu();
    class account
    {
           private:
                       string name;
                       int acnum;
                       bool type;   //0 for savings, 1 for current account
                       int balance;
            public:
                       account(string namezzz, int acnumzzz, bool typezzz,int balancezzz)
                       {
                            /* cout<<"\nName of customer";
                             cin>>name;
                             cout<<"\nAccount Number";
                             cin>>acnum;
                             cout<<"\nType--->0/1";
                             cin>>type;
                             cout<<"\nInitial balance";
                             cin>>balance;*/
                            name=namezzz;
                            acnum=acnumzzz;
                            type=typezzz;
                            balance=balancezzz;
                                                    
                            
                            
                        }
                       
                          void deposit(int amountzzz)
                         {
                               /*int amount;
                               cout<<"\nDeposit Amount:";
                               cin>>amount;
    	                       balance+=amount;
                               cout<<"\nDone";*/
                               balance+=amountzzz;
                         }
    
                        void withdraw(int amountzzz)
                       {
    	                /*int amount;
    	                cout<<"\nWithdraw Amount";
                            cin>>amount;  
    	                if(amount<=balance)
    	                   {
    		                 balance-=amount;
    		                 cout<<"\nDone";
     	                    }
    	                else
    	                   {
    		                 cout<<"\nGet lost @#$&#37;^&*";
    	                    }*/
    	                    
    	                 balance-=amountzzz;   
                       }
    
                  void display()  //??
                {
    	          cout<<endl<<name; 
    	          cout<<endl<<balance;
                 }
                 
                 int getbalance()
                 {
                 	return balance;
                 }
    };
    
    
    
    int main()
    {
    	int amountzzz,acnumzzz,balancezzz; //LOCAL VARIABLES
    	string namezzz; //LOCAL
    	bool typezzz; //LOCAL
    	cout<<"\nName of customer";
            cin>>namezzz;
            cout<<"\nAccount Number";
            cin>>acnumzzz;
            cout<<"\nType--->0/1";
            cin>>typezzz;
            cout<<"\nInitial balance";
            cin>>balancezzz;
            account customer1(namezzz,acnumzzz,typezzz,balancezzz); 	
    	int flag=menu();
    	
    	if(flag==1)
    		{
    			cout<<"\nDeposit Amount:";
    			cin>>amountzzz;		
    			customer1.deposit(amountzzz);
    		}
    			
    	if(flag==2)
    		{
    			cout<<"\nWithdraw Amount";
    			cin>>amountzzz;
    		    if(amountzzz<=customer1.getbalance())
    		        customer1.withdraw(amountzzz);
    		    else
    		        cout<<"\nGet lost @#$%^&*";
    		            
    		}
    	if(flag==3)
    	    customer1.display();
    	return 0;
    }
    
    
    int menu()
    {
    	int flag;
    	/*PRINT THE MENU*/
    	cout<<"\nEnter your choice";
    	cin>>flag;
    	return flag;
    }
    Its working but only for one customer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM