| | |
Object initialisation
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2007
Posts: 6
Reputation:
Solved Threads: 0
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,
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,
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std ; struct account { explicit account( const string& name, double amount = 0.0 ) : _name(name), _amount(amount) {} // ... private: const string _name ; double _amount ; }; int main() { string name ; double amt ; cout << "enter name <space> amount: " ; // assumes there are no embedded white spaces in name cin >> name >> amt ; account ac1(name,amt) ; }
Last edited by vijayan121; May 1st, 2007 at 11:55 am.
•
•
Join Date: May 2007
Posts: 6
Reputation:
Solved Threads: 0
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
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
Basically my programme is like this
c Syntax (Toggle Plain Text)
CLASS ACCOUNT: // file account.h // account class definition #ifndef ACCOUNT_H #define ACCOUNT_H class Account { public: Account( const char *, const char *, int = 0 ); // constructor ~Account(); // destructor void setBalance(int); // set the accoutn balance to a certain ammount void deposit(int); // deposit money to account balance void withdraw(int); // withdraw money from account balance const char *getFirstName() const; // return first name const char *getLastName() const; // return last name void getBalance(); // return account balance // static member function static int getAccountNumber(); // return account number private: char *firstName; char *lastName; int balance; // static data member static int accountNumber; // number of accounts instantiated }; // end class account CLASS ACCOUNTIMPLEMENTATION: #endif // file employee2.cpp // Member-function definitions for class Account #include <iostream> #include <new> // C++ standard new operator #include <cstring> // strcpy and strlen prototypes #include "account.h" // account class definition using namespace std; // define and initialize static data member int Account::accountNumber = 0; // define static member function that returns number of // account objects instantiated int Account::getAccountNumber() { return accountNumber; } // end static function getAccountNumber // constructor dynamically allocates space for // first and last name and uses strcpy to copy // first and last names into the object Account::Account( const char *first, const char *last, int ammount ) { firstName = new char[ strlen( first ) + 1 ]; strcpy( firstName, first ); lastName = new char[ strlen( last ) + 1 ]; strcpy( lastName, last ); Account::setBalance(ammount); ++accountNumber; // increment static count of account number cout << "Account constructor for " << firstName << ' ' << lastName << " called." << endl; } // end Eaccount constructor // destructor deallocates dynamically allocated memory Account::~Account() { cout << "~Account() called for " << firstName << ' ' << lastName << endl; delete [] firstName; // recapture memory delete [] lastName; // recapture memory --accountNumber; // decrement static count of account number } // end destructor ~Account void Account::setBalance(int ammount) { balance =+ ammount; } void Account::deposit(int ammount) { if(ammount > 0) balance += ammount; // add balance with ammount } void Account::withdraw(int ammount) { if(ammount > 0) balance -= ammount; // substract balance with ammount } // return first name of account const char *Account::getFirstName() const { // const before return type prevents client from modifying // private data; client should copy returned string before // destructor deletes storage to prevent undefined pointer return firstName; } // end function getFirstName // return last name of account const char *Account::getLastName() const { // const before return type prevents client from modifying // private data; client should copy returned string before // destructor deletes storage to prevent undefined pointer return lastName; } // end function getLastName void Account::getBalance() { cout << "Your account balance is: " << balance << endl; } // end function getBalance MY MAIN: // file myMain.cpp // Driver to test class account #include <iostream> #include <new> // C++ standard new operator #include "account.h" // account class definition using namespace std; int main() { Account susan( "Susan", "Baker", 100 ); susan.withdraw(50); } // end main[/inlinecode]
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
do not allow attempt at withdraw if account has not been created.
pseudocode
pseudocode
C++ Syntax (Toggle Plain Text)
void user_input() { account* ac = 0 ; // accept user input // if create { if( ac == 0 ) { string name ; double amt ; cout << "enter name <space> amount: " ; // assumes there are no embedded white spaces in name cin >> name >> amt ; ac = new account(name,amt) ; } else cerr << "account already created\n" ; // perhaps you can offer to delete and recreate? } // if withdraw if( ac == 0 ) cerr << "can't withdraw if you haven't created an account\n" ; else { // accept amout ac->withdraw(amt) ; } delete ac ; // before you return }
Last edited by vijayan121; May 1st, 2007 at 12:40 pm.
![]() |
Similar Threads
- Object-Oriented Programming (C++)
- How to maintain session for Web Services Object? (VB.NET)
- infected, embedded object. (Viruses, Spyware and other Nasties)
- Print Object hierarchy (C)
Other Threads in the C++ Forum
- Previous Thread: Iomanip
- Next Thread: Deconstuctor question
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






