class problems

Started by
5 comments, last by dsage 21 years, 9 months ago

  //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>  
Advertisement
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.

  // 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 mpprivate:	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 membervoid 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 membervoid 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]
and I think you forgot to...
quote:
int Cat::ShHp()

... change to Unit::ShHp() here ...

The same is for Cat::ShMp()

---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
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.
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]
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 mpprivate:	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 membervoid 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 membervoid 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]

This topic is closed to new replies.

Advertisement