GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Mar-2009, 02:46
Ndimangwa Ndimangwa is offline
New Member
 
Join Date: Dec 2008
Location: BANGALORE - INDIA
Posts: 11
Ndimangwa has a little shameless behaviour in the past

How An Object Can Create And Destroy Another Object


I have got a problem in objects interaction. In my case I want to have a master object which will keep track of other objects .

Example:

In bank and customer, I may have a class bank and class customer. In my program I want to create only one object of a type bank, under bank I will be creating objects of type customer or modifying them?

How can I go about this?? Thank you
  #2  
Old 17-Mar-2009, 07:17
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: How An Object Can Create And Destroy Another Object


Quote:
Originally Posted by Ndimangwa
I have got a problem in objects interaction. In my case I want to have a master object which will keep track of other objects .

Example:

In bank and customer, I may have a class bank and class customer. In my program I want to create only one object of a type bank, under bank I will be creating objects of type customer or modifying them?

How can I go about this?? Thank you

You probably want to use the FactoryMethod design pattern such that inside of class Bank, you have an instance of a CustomerFactory that produces your customers.

While I don't agree with the "thinking" behind it, you can use the Singleton design pattern so that you have only one instance of Bank. Since the real world tells us that there are Banks and BankBranches all over the place, a Singleton of Bank doesn't make much sense to me, but that's none of my business.

If you mean something else and/or do not want to use design patterns, here is a little example of how to do this in code where your Bank "manages" customers. Naturally, this example does only the minimalist amount of management. You would definitely want something more capable.

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Customer
{
public:
    Customer() : m_name("") {}
    std::string& getName() { return m_name; }
    void setName(std::string const& name) { m_name = name; }
    friend std::ostream& operator<<(std::ostream& os, Customer const& rhs)
    {
	os << rhs.m_name;
	return os;
    }
    friend std::istream& operator>>(std::istream& is, Customer& rhs)
    {
	is >> rhs.m_name;
	return is;
    }
private:
    std::string m_name;
};

typedef std::vector<Customer> vCustomers;

class Bank
{
public:
    Bank() {}
    Bank(const size_t numCustomers)
    {
	for(size_t i = 0; i < numCustomers; i++)
	{
	    Customer c;
	    m_vCustomers.push_back(c);
	}
    }
    void addCustomer(Customer const& customer)
    {
	m_vCustomers.push_back(customer);
    }
    void showCustomers()
    {
	vCustomers::iterator it;
	for(it = m_vCustomers.begin(); it != m_vCustomers.end(); it++)
	{
	    std::cout << (*it) << endl;
	}
    }
private:
    vCustomers m_vCustomers;
};

int main()
{
    size_t numCustomers = 0;
    cout << "Enter the number of customers for a new Bank object: ";
    cin >> numCustomers;
    if(numCustomers > 0)
    {
	Bank b;

	for(size_t i = 0; i < numCustomers; i++)
	{
	    Customer c;
	    cout << "Enter a name for Customer: ";
	    cin >> c;
	    b.addCustomer(c);
	}
	b.showCustomers();
    }

    return 0;
}


Output:

Code:
Enter the number of customers for a new Bank object: 2 Enter a name for Customer: Fred Enter a name for Customer: Barney Fred Barney

...while my main function does a good bit of the management work, you could incorporate yet another class CustomerManager into your app so that Bank had/has an instance of it so that you could manage the customers through the interfaces it provides.


MxB
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Click X in window won't destroy itself ? Algar MS Visual C++ / MFC Forum 4 02-Nov-2007 12:34
need help with a console menu system BullBuchanan C++ Forum 6 20-Aug-2006 15:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.