Object initialisation

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 6
Reputation: slanker70 is an unknown quantity at this point 
Solved Threads: 0
slanker70 slanker70 is offline Offline
Newbie Poster

Object initialisation

 
0
  #1
May 1st, 2007
Hi,

First of all I'd like to apologise if there is any ambiguity in my questions since I'm new at c++.

My problem is trying to figure out how to initialise an object of a class that would depend on the user.

For example I have class Account and as far as I know I can initialise it's constructor by using the command eg.

Account acc1 (jack, 500);

First problem is how can I make a user set the initialisation? For example the user enter it's name and the ammount he/she wishes to deposit then an account object would be initialised.

Therefore to call a method of this object I could use a command eg.

acc1.getBalance();

Second problem is, since there is no object yet, how can I determined what object name will be used in the command above if there is no object that have been initialised?

I know that this is wuite a lot of but I would really appreciate any help.

Thank you,
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Object initialisation

 
0
  #2
May 1st, 2007
  1. #include <iostream>
  2. #include <string>
  3. using namespace std ;
  4.  
  5. struct account
  6. {
  7. explicit account( const string& name, double amount = 0.0 )
  8. : _name(name), _amount(amount) {}
  9. // ...
  10. private:
  11. const string _name ;
  12. double _amount ;
  13. };
  14.  
  15. int main()
  16. {
  17. string name ; double amt ;
  18. cout << "enter name <space> amount: " ;
  19. // assumes there are no embedded white spaces in name
  20. cin >> name >> amt ;
  21. account ac1(name,amt) ;
  22. }
Last edited by vijayan121; May 1st, 2007 at 11:55 am.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 6
Reputation: slanker70 is an unknown quantity at this point 
Solved Threads: 0
slanker70 slanker70 is offline Offline
Newbie Poster

Re: Object initialisation

 
0
  #3
May 1st, 2007
First of all thank you for your reply, but I still have some problems which I'm not sure of.

Basically my programme is like this

  1. CLASS ACCOUNT:
  2.  
  3. // file account.h
  4. // account class definition
  5.  
  6. #ifndef ACCOUNT_H
  7. #define ACCOUNT_H
  8.  
  9. class Account {
  10.  
  11. public:
  12. Account( const char *, const char *, int = 0 ); // constructor
  13. ~Account(); // destructor
  14.  
  15. void setBalance(int); // set the accoutn balance to a certain ammount
  16. void deposit(int); // deposit money to account balance
  17. void withdraw(int); // withdraw money from account balance
  18.  
  19. const char *getFirstName() const; // return first name
  20. const char *getLastName() const; // return last name
  21. void getBalance(); // return account balance
  22.  
  23. // static member function
  24. static int getAccountNumber(); // return account number
  25.  
  26.  
  27. private:
  28. char *firstName;
  29. char *lastName;
  30. int balance;
  31.  
  32. // static data member
  33. static int accountNumber; // number of accounts instantiated
  34.  
  35. }; // end class account
  36.  
  37. CLASS ACCOUNTIMPLEMENTATION:
  38.  
  39. #endif
  40.  
  41. // file employee2.cpp
  42. // Member-function definitions for class Account
  43.  
  44. #include <iostream>
  45. #include <new> // C++ standard new operator
  46. #include <cstring> // strcpy and strlen prototypes
  47. #include "account.h" // account class definition
  48. using namespace std;
  49.  
  50. // define and initialize static data member
  51. int Account::accountNumber = 0;
  52.  
  53. // define static member function that returns number of
  54. // account objects instantiated
  55. int Account::getAccountNumber()
  56. {
  57. return accountNumber;
  58. } // end static function getAccountNumber
  59.  
  60. // constructor dynamically allocates space for
  61. // first and last name and uses strcpy to copy
  62. // first and last names into the object
  63. Account::Account( const char *first, const char *last, int ammount )
  64. {
  65. firstName = new char[ strlen( first ) + 1 ];
  66. strcpy( firstName, first );
  67.  
  68. lastName = new char[ strlen( last ) + 1 ];
  69. strcpy( lastName, last );
  70.  
  71. Account::setBalance(ammount);
  72.  
  73. ++accountNumber; // increment static count of account number
  74.  
  75. cout << "Account constructor for " << firstName
  76. << ' ' << lastName << " called." << endl;
  77.  
  78. } // end Eaccount constructor
  79.  
  80. // destructor deallocates dynamically allocated memory
  81. Account::~Account()
  82. {
  83. cout << "~Account() called for " << firstName
  84. << ' ' << lastName << endl;
  85.  
  86. delete [] firstName; // recapture memory
  87. delete [] lastName; // recapture memory
  88.  
  89. --accountNumber; // decrement static count of account number
  90.  
  91. } // end destructor ~Account
  92.  
  93. void Account::setBalance(int ammount)
  94. {
  95. balance =+ ammount;
  96. }
  97.  
  98. void Account::deposit(int ammount)
  99. {
  100. if(ammount > 0)
  101. balance += ammount; // add balance with ammount
  102. }
  103.  
  104. void Account::withdraw(int ammount)
  105. {
  106. if(ammount > 0)
  107. balance -= ammount; // substract balance with ammount
  108. }
  109.  
  110. // return first name of account
  111. const char *Account::getFirstName() const
  112. {
  113. // const before return type prevents client from modifying
  114. // private data; client should copy returned string before
  115. // destructor deletes storage to prevent undefined pointer
  116. return firstName;
  117.  
  118. } // end function getFirstName
  119.  
  120. // return last name of account
  121. const char *Account::getLastName() const
  122. {
  123. // const before return type prevents client from modifying
  124. // private data; client should copy returned string before
  125. // destructor deletes storage to prevent undefined pointer
  126. return lastName;
  127.  
  128. } // end function getLastName
  129.  
  130. void Account::getBalance()
  131. {
  132. cout << "Your account balance is: " << balance << endl;
  133. } // end function getBalance
  134.  
  135. MY MAIN:
  136.  
  137. // file myMain.cpp
  138. // Driver to test class account
  139.  
  140. #include <iostream>
  141. #include <new> // C++ standard new operator
  142. #include "account.h" // account class definition
  143. using namespace std;
  144.  
  145. int main()
  146. {
  147. Account susan( "Susan", "Baker", 100 );
  148. susan.withdraw(50);
  149.  
  150. } // end main[/inlinecode]
Here I invoke the class account constructor from within the programme, but for example if I wanted the user to invoke it eg.

switch
case("1")
The user enter the input that will be passed to the class account constructor eg. firstName, lastName and ammount.
case("2")
The user wishes to withdraw money so
susan.withdraw(); would have to be invoked, but since the oject susan it self haven't been initialised an error occured.

Any suggestions?

Thank you
Last edited by Ancient Dragon; May 1st, 2007 at 12:18 pm. Reason: corrected code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Object initialisation

 
0
  #4
May 1st, 2007
do not allow attempt at withdraw if account has not been created.
pseudocode
  1. void user_input()
  2. {
  3. account* ac = 0 ;
  4. // accept user input
  5. // if create
  6. {
  7. if( ac == 0 )
  8. {
  9. string name ; double amt ;
  10. cout << "enter name <space> amount: " ;
  11. // assumes there are no embedded white spaces in name
  12. cin >> name >> amt ;
  13. ac = new account(name,amt) ;
  14. }
  15. else cerr << "account already created\n" ;
  16. // perhaps you can offer to delete and recreate?
  17. }
  18. // if withdraw
  19. if( ac == 0 ) cerr << "can't withdraw if you haven't created an account\n" ;
  20. else
  21. {
  22. // accept amout
  23. ac->withdraw(amt) ;
  24. }
  25. delete ac ; // before you return
  26. }
Last edited by vijayan121; May 1st, 2007 at 12:40 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC