Advertisement

object declaring new object

Started by May 04, 2002 06:17 PM
9 comments, last by viper35al 22 years, 10 months ago
Does anyone know how to declare one object from another one? (in a basic dos class) like what would you type and where. also, is there any special things you can do when creating an object under a pointer? thanks
Maybe my question wasn''t clear enough. If in a C++ source file, I have 2 basic classes. Is there anyway in which I can create a new object of a class without an object, inside of a method of the class that already has an object.

If my question is still unclear, please tell me, and I''ll try to explain it better.

Thanks.
Advertisement
It''s still unclear. Can you tell us how you can create an object "with object" (possibly some source code) and what you want to get rid of in that source?
---visit #directxdev on afternet <- not just for directx, despite the name

      #include <iostream.h>class Bank{public:	int getnumberofaccounts();	void createnewaccount();private:	int numofaccounts = 0;};int Bank::getnumberofaccounts(){	return numofaccounts;}void Bank::createnewaccount(){	// would like to create a new object using the // Account class here.	// also, is there anyway in which I can have the first// Account made 	// be Account1, and then have the next one be Account2, // and so on.	numofaccounts ++;}class Account{public:	void deposit(int amounttodeposit);	void withdrawl(int amounttowithdrawl);	int getamount();private:	int amount;};void Account::deposit(int amounttodeposit){	amount += amounttodeposit;}void Account::withdrawl(int amounttowithdrawl){	amount -= amounttowithdrawl;}int Account::getamount(){	return amount;}int main(){	Bank Bank1();	Bank1.createnewaccount(); 	 // is it possible to create an Account class object // through the Bank class	 // that can be used in main? return 0;}      


It's a pretty stupid program, but I think it gets my point across. If you still hav questions please ask.


[edited by - viper35al on May 5, 2002 3:48:52 PM]

[edited by - viper35al on May 5, 2002 3:52:24 PM]
Well, you can instantiate the Account class within methods of the Bank class as easily as anywhere else, but you''ll also need somewhere to store the Accounts. The Bank should have a number of Accounts -- an array, to be simple, or maybe a linked list -- that you can add Account objects to.
Something along these lines:

      #include <iostream>#include <vector>using namespace std;// Moved before Bankclass Account{public:	Account();	void deposit(int amounttodeposit);	void withdrawl(int amounttowithdrawl);	int getamount();private:	int amount;};Account::Account(){	// Initialize amount here	amount = 0;}void Account::deposit(int amounttodeposit){	amount += amounttodeposit;}void Account::withdrawl(int amounttowithdrawl){	amount -= amounttowithdrawl;}int Account::getamount(){	return amount;}class Bank{public:	Bank();	int getnumberofaccounts();	void createnewaccount();private:	// You can't initialize data here; use constructor (below)	int numofaccounts;	// This stores all accounts	vector <Account> accounts;};Bank::Bank(){	numofaccounts = 0;}int Bank::getnumberofaccounts(){	return numofaccounts;}void Bank::createnewaccount(){	// would like to create a new object using the // Account class here.	// also, is there anyway in which I can have the first// Account made 	// be Account1, and then have the next one be Account2, // and so on.	Account a;	accounts.push_back(a);	// Access the first account	accounts[0].deposit(100);	numofaccounts ++;}int main(){	Bank Bank1;	Bank1.createnewaccount(); 	 // is it possible to create an Account class object // through the Bank class	 // that can be used in main? return 0;}      



[edited by - IndirectX on May 5, 2002 6:17:41 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
do any of you know what this error messgae means?

c:\program files\microsoft visual studio\myprojects\multiclassbank1\a.cpp(85) : error C2228: left of ''.createnewaccount'' must have class/struct/union type

thanks.
It means that the left hand side is not a struct. If it''s a pointer to a struct, you should be using the arrow operator.
Sorry, I missed this: you should declare Bank1 as

Bank Bank1;

Compiler thinks Bank1 is a function the way you declared it.
---visit #directxdev on afternet <- not just for directx, despite the name
There are 2 parts which I don''t seems to understand and need some enlightenment.

First, what does the Vector keyword do
quote:

vector <Account> accounts;


Secondly, is pushback related to "accounts" declared above?

void Bank::createnewaccount(){
// would like to create a new object using the // Account class here.
// also, is there anyway in which I can have the first
// Account made
// be Account1, and then have the next one be Account2,
// and so on.
Account a;
accounts.push_back(a); // Access the first account accounts[0].deposit(100);
numofaccounts ++;
}


I am not a professional nor seasoned but just a newbie who wish to learn programming. Please flame me not. Tkx

This topic is closed to new replies.

Advertisement