943,981 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1058
  • C++ RSS
May 1st, 2007
0

Object initialisation

Expand Post »
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,
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slanker70 is offline Offline
6 posts
since May 2007
May 1st, 2007
0

Re: Object initialisation

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
May 1st, 2007
0

Re: Object initialisation

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slanker70 is offline Offline
6 posts
since May 2007
May 1st, 2007
0

Re: Object initialisation

do not allow attempt at withdraw if account has not been created.
pseudocode
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Iomanip
Next Thread in C++ Forum Timeline: Deconstuctor question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC