[Solved] Question on class object

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

[Solved] Question on class object

Post by zodiac976 »

I have a question about a class containing an object of
another class inside it as private.

Example:

Code: Select all

class Class1
{
     public:
     private:
          Class2 class2;
};

class Class2
{
     public:
     private:
};
I think I see what it does but would using inheritance or
friendship be a better choice?

If anyone could explain the purpose of doing something
like this I would appreciate it.
Last edited by zodiac976 on Sat Jul 04, 2009 11:21 pm, edited 3 times in total.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Question on class object

Post by Scoody »

If you want to make a specialized version of a class, ie. extend the baseclass, you use inheritance.
Example:
Car - base class
Sportscar - specialized version, inherits the base of car and adds new functions and improves the existing

And one class using another, if you got a Wheel-class, then the previous Car-class would contain the wheel.

Look up "has-a is-a relationship" on google, might be along those lines what you're looking for.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Question on class object

Post by zodiac976 »

I understand the use of inheritance and friendship, I was
just wondering why you would declare an object of another
class inside a class as a private member.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Question on class object

Post by Falco Girgis »

Dude, that should be way more common than inheritance and friendship. Don't let whatever OO guide you're reading make you think differently.

What if the class1 is a player, and class2 is a weapon? The player has a weapon. What if class1 is a GUI, and class 2 is a button? The GUI has a button. What if class1 is a dude, and class2 is his penis? The dude has a penis.

You should have noticed the "has a" relationship. If you can ever say that class1 "has" class2, you don't use inheritance. That's how you handle the scenario.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Question on class object

Post by zodiac976 »

GyroVorbis wrote:Dude, that should be way more common than inheritance and friendship. Don't let whatever OO guide you're reading make you think differently.

What if the class1 is a player, and class2 is a weapon? The player has a weapon. What if class1 is a GUI, and class 2 is a button? The GUI has a button. What if class1 is a dude, and class2 is his penis? The dude has a penis.

You should have noticed the "has a" relationship. If you can ever say that class1 "has" class2, you don't use inheritance. That's how you handle the scenario.
So I would use something like this to sorta connect
the classes together:

class Character
{
public:
private:
Weapon weapon;
};

class Weapon
{
public:
private:
};

Man I must have confused Inheritance or something...

Inheritance is like extending a base class right?
How would I access an object of a class inside
the class? Is it the same as accessing a regular
private variable?

The book I read wasn't that good I mostly learned
all this stuff through sitting down and doing trial
and error with some googling and also asking
questions.

I went through 2 classes in college of c++ and basically
had to teach myself this language. The instructor wasn't
very good and was too slow paced in my opinion but anyways...
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Question on class object

Post by thejahooli »

GyroVorbis wrote:What if class1 is a dude, and class2 is his penis? The dude has a penis.
Not always
I'll make your software hardware.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Question on class object

Post by avansc »

thejahooli wrote:
GyroVorbis wrote:What if class1 is a dude, and class2 is his penis? The dude has a penis.
Not always
you said you would keep that a secret ;(
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: Question on class object

Post by Innerscope »

Inheritance is like extending a base class right?
How would I access an object of a class inside
the class? Is it the same as accessing a regular
private variable?
It's the same as accessing on object. You're just doing it within that class.
Here is another example to help illustrate inheritance:
Animal - Base Class
Mammal - Extended Animal
Reptile - Extended Animal
Human - Extended Mammal

Do you see what is happening here? A human now contains all of the traits(functions and variables) of a mammal. Because mammal contains all of the traits of animal, a human does as well. The reptile is an extension of animal by itself and does not contain the traits of mammal nor human.
Hope that helps/makes sense
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Question on class object

Post by dandymcgee »

Also, back to accessing private members:

See this.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Question on class object

Post by zodiac976 »

Innerscope wrote:
Inheritance is like extending a base class right?
How would I access an object of a class inside
the class? Is it the same as accessing a regular
private variable?
It's the same as accessing on object. You're just doing it within that class.
Here is another example to help illustrate inheritance:
Animal - Base Class
Mammal - Extended Animal
Reptile - Extended Animal
Human - Extended Mammal

Do you see what is happening here? A human now contains all of the traits(functions and variables) of a mammal. Because mammal contains all of the traits of animal, a human does as well. The reptile is an extension of animal by itself and does not contain the traits of mammal nor human.
Hope that helps/makes sense
I understand inheritance now and how to access private variables
such as: int, double, string, etc but how would I access the private
object: Weapon weapon; inside class Character?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Question on class object

Post by dandymcgee »

zodiac976 wrote: I understand inheritance now and how to access private variables
such as: int, double, string, etc but how would I access the private
object: Weapon weapon; inside class Character?
The exact same way you access variables. What's your question?

Inside class Character you can make calls to weapon just like you'd access character from main:

Code: Select all

void Character::EquipWeapon()
{
     weapon.equip();
}

void Character::UnequipWeapon()
{
     weapon.unequip();
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Question on class object

Post by zodiac976 »

dandymcgee wrote:
zodiac976 wrote: I understand inheritance now and how to access private variables
such as: int, double, string, etc but how would I access the private
object: Weapon weapon; inside class Character?
The exact same way you access variables. What's your question?

Inside class Character you can make calls to weapon just like you'd access character from main:

Code: Select all

void Character::EquipWeapon()
{
     weapon.equip();
}

void Character::UnequipWeapon()
{
     weapon.unequip();
}
I know but when I run it in working code I get type specifier errors
and of course weapon isn't a member of character error.

Code: Select all

class Character
{
public:
private:
Weapon weapon; //<---isn't a member of character and type specifier
};

class Weapon
{
public:
private:
};
I know how to make it a member but the declaration of it must
be wrong?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Question on class object

Post by avansc »

you have to declare your classes before you define them. especially if you use classes in classes.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Question on class object

Post by zodiac976 »

avansc wrote:you have to declare your classes before you define them. especially if you use classes in classes.
I tried declaring class Weapon first also but it had no effect.
I must be declaring it wrong or something...I will post a piece
in a minute to show you what I mean.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Question on class object

Post by zodiac976 »

Code: Select all

#include <iostream>
#include <string>
using namespace std;

class Weapon
{
	public:
		void dispWeapon();
	private:
		string weapName;
	protected:
};

void Weapon::dispWeapon()
{
	weapName = "knife";
	cout << weapName << endl;
}

class Character
{
	public:
		void aFunc();
	private:
		Weapon weapon;
	protected:
};

void Character::aFunc()
{
	cout << "aFunc\n";
}

int main()
{
	Character character;

	character.aFunc();

	character.weapon.dispWeapon();   //I know this is wrong but how am I supposed to access
							               //class Weapon through class Character
	return 0;
}
error C2248: 'Character::weapon' : cannot access private member declared in class 'Character'
see declaration of 'Character::weapon'
see declaration of 'Character'
Post Reply