Composition

Confusion on usage

Page 1 of 1

10 Replies - 961 Views - Last Post: 01 May 2010 - 11:07 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Composition

Posted 30 April 2010 - 10:34 PM

In our final project we are to demonstrate composition... I have a couple of questions on composition.

1) Why would you use composition?
2) How do you use composition?
2 personal knowhow :
class poultry{
   Chicken first;
   Egg second;
   Chick third;
...
};


Something like that, I know the classes and names are not quite that composition is, but the overall idea is.

3) how do you use the classes within the class? is it something like:
void first::weight()
{
int weight;
cout << "get weight: ";
cin >> weight;
cout << "You entered " << weight << " for the chicken" << endl;
}

int main() 
{
Poultry frozen;
 first.weight();
...}


???

Would you just use the classes as normal within main()?
[/code]

Is This A Good Question/Topic? 0
  • +

Replies To: Composition

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Composition

Posted 30 April 2010 - 10:57 PM

you use either . or ->

eg.
#include <iostream>

using namespace std;

class Chicken	{
public:
  int i;
};

class Egg {
public: 
   char c;
};

class Chick {
public:
  char sx;
};

class Poultry	{
public:
  Chicken first;
  Egg second;
  Chick third;
};

int main() {
  
  Poultry p;
  // create objects //
  Chicken chicken;
  chicken.i = 2;
  Egg egg;
  egg.c = 'A';
  Chick chick;
  chick.sx = 'M';

  // now set the Poultry Member Objects //
  p.first = chicken;
  p.second = egg;
  p.third = chick;

  return 0;
}


So if for instance you have a member function in the Chicken class called doSomething you would call it on the Chicken Object inside the poultry class using
p.first.doSomething();
Depending on how the objects are declared.
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Composition

Posted 30 April 2010 - 11:05 PM

-sigh-... I guess I should have done this before I coded my entire project... LoL.

So everything is accessed like normal, it's just with an appending base class, right?

so if I had:
class Banking
{
saving Sbank;
checking Cbank;
checkAndSave CanS;
...
}


I would do this to access ANY functions (including pure virtual functions from base class):
int main()
{
Banking B;
saving Sbank;
checking Cbank;
checkAndSave CanS;

B.Sbank = Sbank;
B.Cbank = Cbank;
B.CanS = CanS;

B.Sbank.setDeposit(); // pure virtual function from Banking class
...


??

This post has been edited by IngeniousHax: 30 April 2010 - 11:07 PM

Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Composition

Posted 30 April 2010 - 11:13 PM

Ummmm....yes. If you aren't sure about syntax, there's no penalty for revisiting your books !?
Was This Post Helpful? 0
  • +
  • -

#5 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Composition

Posted 30 April 2010 - 11:20 PM

Yeah, I am not too sure our book covers this in-depth too much. Infact, I haven't seen this in our text book as of yet and the last two chapters deal with BST's and stacks, queues and algorithms.
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Composition

Posted 30 April 2010 - 11:27 PM

Looks like your book is an awkward hybrid between trying to teach C++ and trying to teach data structures and algorithms.

Get a proper C++ book and learn the syntax from it. You should get a sense of how proper C++ idioms are constructed from a book like Accelerated C++. There's also more reference oriented books like C++ Primer by Lippman.
Was This Post Helpful? 2
  • +
  • -

#7 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Composition

Posted 30 April 2010 - 11:30 PM

Awesome, thank you for the book suggestion.

I also just found out, you can not use composition in an abstract class... LoL
Was This Post Helpful? 0
  • +
  • -

#8 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Composition

Posted 30 April 2010 - 11:39 PM

ALright, So I changed my program around a bit, and was wondering if this is the proper usage... It compiles and runs, so I imagine it can't be ALL wrong, but is it this a proper usage?


class composition
{
public:
	Saving save;
	Checking check;
	checkAndSave CanS;
};



...snip headers...
int main()
{
	Banking  *bank;
	//Banking Mbank;
	Checking Cbank;
	Saving Sbank;
	checkAndSave CSbank;	// derived class of Banking, for checking and saving account activities
	composition C;
	bank = &CSbank;
	//check = &Cbank;	// derived class of Banking, for checking accounts
	//save = &Sbank;		// derived class of Banking, for savings accounts
	//bank = &Mbank;	// base bank class

	C.save = Sbank;
	C.check = Cbank;
	C.CanS = CSbank;

	char choice;
	float depAmt = 0;

	choice = bank -> menu();		// Call the banking menu
	switch(choice)
		{
			case 'A':
				choice = bank -> newAccountMenu();
					switch(choice)
						{
							case 'A':
								C.check.newCheckingAccount();
								break;
	
							case 'B':
								C.save.newSavingAccount();
...snip rest of code...


This post has been edited by IngeniousHax: 30 April 2010 - 11:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Composition

Posted 01 May 2010 - 04:05 AM

Well personally i would be creating a constructor and mutator methods to access any data, so for instance when you create your composition class you should have a constructor to pass variables to the instance and do it that way, rather than assignment using . for each member variable, just assign it all on creating of the instance.
Was This Post Helpful? 0
  • +
  • -

#10 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Composition

Posted 01 May 2010 - 09:16 AM

I don't having anything for the constructor in any of my derived classes though, the only thing I have for a constructor is Banking::Banking(), which sets all the member variables to 0 or empty strings.
Was This Post Helpful? 0
  • +
  • -

#11 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Composition

Posted 01 May 2010 - 11:07 PM

Perhaps you should then add an overload constructor for each class and implement declaration using the constructors :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1