[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
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:

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'
This:

Code: Select all

character.weapon.dispWeapon(); 
isn't legal because you defined weapon as private. Move the "Weapon weapon;" declaration to Character's public members and it will work just fine. If you want to leave it in private then you need to make an accessor (which would probably end up returning a pointer to weapon that you could then use to make function calls).
Last edited by dandymcgee on Mon Jun 29, 2009 4:12 pm, edited 2 times in total.
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 »

I know about declaring them and declaring them in
separate header files, thanks though.
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 know about declaring them and declaring them in
separate header files, thanks though.
Please re-read.. I completely edited because you posted at the same time as me. ;)
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 know about declaring them and declaring them in
separate header files, thanks though.
Please re-read.. I completely edited because you posted at the same time as me. ;)
:lol: sorry about that. Something like this?;
it actually worked...:

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 Weapon
{
	public:
		void aFunc();
		void setWeapon(Weapon temp) {weapon = temp;}
		Weapon getWeapon() const {return weapon;}
	private:
		Weapon weapon;
	protected:
};

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

int main()
{
	Character character;

	character.aFunc();

	character.getWeapon().dispWeapon();

	return 0;
}
I am assuming I did it right I guess.... :|
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: :lol: sorry about that. Something like this?;
it actually worked...:

Code: Select all


I am assuming I did it right I guess.... :|[/quote]
Yup, that looks about right.  If it compiled then I suppose it works.   ;)
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: :lol: sorry about that. Something like this?;
it actually worked...:

Code: Select all


I am assuming I did it right I guess.... :|[/quote]
Yup, that looks about right.  If it compiled then I suppose it works.   ;)[/quote]

It does work and displays what's inside the functions
hmm weird I have never gotten around to this if this
stuff is super common...:(. Thanks though I believe
I got this down now.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Question on class object

Post by MarauderIIC »

zodiac976 wrote:

Code: Select all

class Character: public Weapon
{
	public:
		void aFunc();
		void setWeapon(Weapon temp) {weapon = temp;}
		Weapon getWeapon() const {return weapon;}
	private:
		Weapon weapon;
	protected:
};
No. You're saying a character is a weapon and a character has a weapon. You want only one of these relationships.

"class Character : public weapon" = "Character IS A weapon"
"class Character { /* ... */ Weapon weapon; /* ... */ };" = "Character HAS A weapon

Further, you write,

Code: Select all

		Weapon getWeapon() const {return weapon;}
This is bad because it makes a copy of the weapon and returns it. You don't want to make a copy, this is slow. Return a const reference or a const pointer. You might be returning a const copy, I'm not sure, I have to brush up on the different locations of the const keyword. I think declaring the function as

Code: Select all

Weapon& getWeapon() const /* ... */
will give you a const reference.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply