Archived

This topic is now archived and is closed to further replies.

object declaring new object

This topic is 5704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

      
#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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Something along these lines:

      
#include <iostream>

#include <vector>

using namespace std;

// Moved before Bank

class 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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;
[/QUOTE]

Secondly, is pushback related to "accounts" declared above?
[QUOTE]
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 ++;
}
[/QUOTE]

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

Share this post


Link to post
Share on other sites
Vector is an STL (Standard Template Library) class (as I understand it, it contains an array which it resizes at need to create a sort of "dynamic array"), not a keyword. I suggest you read up on templates, the STL, and the vector class.

Share this post


Link to post
Share on other sites