Archived

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

dsage

class problems

Recommended Posts


  //project.cpp : Practicing classes!


#include <iostream.h>

class Unit
{
public:
	int ShHp(); //show hp

	void ChHp(int hp); //change hp

	int ShMp(); //show mp

	void ChMp(int mp); //change mp

private:
	int chhp;
	int chmp;
};

// ShHp, Public accessor funtion returns

// value of hp and returns it to ShHp()

int Cat::ShHp()
{
	return chhp;
}

// def of ChHp, public accessor function

// returns sets ShHp member

void Unit::ChHp(int hp)
{
	// set member var its age to value 

	// passed in by paremeter age

	int chhp = hp;
}

// ShHp, Public accessor funtion returns

// value of hp and returns it to ShHp()

int Cat::ShMp()
{
	return chmp;
}

// def of ChHp, public accessor function

// returns sets ShHp member

void Unit::ChMp(int mp)
{
	// set member var its age to value 

	// passed in by paremeter age

	int chmp = mp;
}

int main()
{
	cout << "Making unit GunMan.\n";
	Unit GunMan;
	GunMan.ChHp(100);
	cout << "GunMan Hp set to: " << GunMan.ShMp() << ".\n";
	GunMan.ChMp(20);
	cout << "GunMan Mp set to: " << GunMan.ShHp() << ".\n";
	return 0;
}
[source]

<SPAN CLASS=editedby>[edited by - dsage on July 18, 2002 5:09:28 PM]</SPAN>  

Share this post


Link to post
Share on other sites
You neglected to say what the problem was, but here''s why it''s happening:
quote:
Original post by dsage
// def of ChHp, public accessor function
// returns sets ShHp member
void Unit::ChHp(int hp)
{
// set member var its age to value
// passed in by paremeter age
int chhp = hp;
}

// def of ChHp, public accessor function
// returns sets ShHp member
void Unit::ChMp(int mp)
{
// set member var its age to value
// passed in by paremeter age
int chmp = mp;
}



leave the int off chmp and chhp. Since you''ve already declared them (in your class def) this covers them with local variables of the same name. Most compilers will warn you about this.

Also, in the future, use names like GetHP and SetHP. Get and Set are standard idioms for accessors and mutators in the OOP world.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites

  // project.cpp : Practicing classes!

//


#include <iostream.h>

class Unit
{
public:
int ShHp(); //show hp

void ChHp(int hp); //change hp

int ShMp(); //show mp

void ChMp(int mp); //change mp

private:
int itsHp;
int itsMp;
};

// ShHp, Public accessor funtion returns

// value of hp and returns it to ShHp()

int Unit::GetHp()
{
return itsHp;
}

// def of ChHp, public accessor function

// returns sets ShHp member

void Unit::SetHp(int hp)
{
// set member var its age to value

// passed in by paremeter age

int itsHp = hp;
}

// ShHp, Public accessor funtion returns

// value of hp and returns it to ShHp()

int Unit::GetMp()
{
return itsMp;
}

// def of ChHp, public accessor function

// returns sets ShHp member

void Unit::SetMp(int mp)
{
// set member var its age to value

// passed in by paremeter age

int itsMp = mp;
}

int main()
{
cout << "Making unit GunMan.\n";
Unit GunMan;
GunMan.SetHp(100);
cout << "GunMan Hp set to: " << GunMan.GetMp() << ".\n";
GunMan.SetMp(20);
cout << "GunMan Mp set to: " << GunMan.GetHp() << ".\n";
return 0;
}


i get this error?
"c:\documents and settings\user\desktop\project\project.cpp(60) : fatal error C1010: unexpected end of file while looking for precompiled header directive" using vc++

OMG I cant beleave that got in there =/

[edited by - dsage on July 18, 2002 5:25:24 PM]

Share this post


Link to post
Share on other sites
Oh, that. Haven''t you searched the forum? There must be a few hundred posts on that topic.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
ok so it works now cause of newbish mistake but im getting weird answers like -80003400
should i define hp and mp?

[edited by - dsage on July 18, 2002 5:39:38 PM]

Share this post


Link to post
Share on other sites
Ok I have the progy working!

  // project.cpp : Practicing classes


#include <iostream.h>

class Unit
{
public:
int GetHp(); //show hp

void SetHp(int hp); //change hp

int GetMp(); //show mp

void SetMp(int mp); //change mp

private:
int itsHp;
int itsMp;
};

// ShHp, Public accessor funtion returns

// value of hp and returns it to ShHp()

int Unit::GetHp()
{
return itsHp;
}

// def of ChHp, public accessor function

// returns sets ShHp member

void Unit::SetHp(int hp)
{
// set member var its age to value

// passed in by paremeter age

itsHp = hp;
}

// ShHp, Public accessor funtion returns

// value of hp and returns it to ShHp()

int Unit::GetMp()
{
return itsMp;
}

// def of ChHp, public accessor function

// returns sets ShHp member

void Unit::SetMp(int mp)
{
// set member var its age to value

// passed in by paremeter age

itsMp = mp;
}

int main()
{
cout << "Making unit GunMan.\n";
Unit GunMan;
GunMan.SetHp(10);
cout << "GunMan Hp set to: " << GunMan.GetHp() << ".\n";
GunMan.SetMp(2);
cout << "GunMan Mp set to: " << GunMan.GetMp() << ".\n";
return 0;
}


[edited by - dsage on July 18, 2002 5:46:23 PM]

Share this post


Link to post
Share on other sites