Jump to content

Classes object's interacting between each other? (newbie question about classes)

- - - - -

  • Please log in to reply
13 replies to this topic

#1
masky007

masky007

    Newbie

  • Members
  • PipPip
  • 19 posts
Hi, first of all i apologizie for taking this out to the forum when i know some day ill learn it from some book.. though i recently started on a little project of mine for fun and i was wondering if objects from another class can call each other functions.. or some technique that would do the job?
I searched a bit but couldn't find anything specific.

Let's say i have something like this:

Class RedBattleship

{

public:

void TakeDamage();


private:

int m_healthPoints;

};


RedBattleship::RedBattleship(int healthPoints) m_healthpoints(healthPoints)

{

  cout << "New red battleship has been constructed";

}


void TakeDamage()

{

  m_shields -= 100;

}


//new class


Class BlueBattleship

{

public:

void TakeDamage();


private:

int m_healthPoints;

};


BlueBattleship::blueBattleship(int healthPoints) m_healthpoints(healthPoints)

{

  cout << "New blue battleship has been constructed";

}


void TakeDamage()

{

  m_shields -= 100;

}



int main()

{

RedBattleShip RedBattleShip1; // initializes object named RedBattelShip1 of the RedBattleShip class

BlueBattleShip BlueBattleShip1; // initializes object named BlueBattleShip1 of the BlueBattleShip class


//now i would like to know if there is i way to simply call the function TakeDamage() from one to hurt the another .. something like this:

RedBattleShip1.TakeDamage(); /* i tried to define this function "TakeDamage()"(class RedBattleShip) earlier in the definition scopes like this 


void TakeDamage(BlueBattleShip::int& m_HealthPoints)

{

  m_healthPoints -= 100; --- so here i tried to simply call the red battleship function and access the blue battleship member in order to simulate taking damage to the blue ship trough the red one. */ 

// this is all just in my head, again sorry if this seems stupid i am just inpatient atm :) soz..

Any help would mean much, thank you! 

}



return 0;

}



#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Okay, first let's make it so the Battleships can do damage to each other. We'll use the same TakeDamage() method, but implement it in another method to the object called DealDamage().

class RedBattleship
{
public:
  RedBattleship(int healthPoints)
    : m_healthPoints(healthPoints) {}

  void TakeDamage()
  {
    m_healthPoints -= 100;
  }

  void DealDamage(BlueBattleship &blue)
  {
    blue.TakeDamage();
  }

private:
  int m_healthPoints;
};

Now we can use these two objects to have one object actually deal damage to another object.

int main(void)
{
  RedBattleship red(500);
  BlueBattleship blue(500);

  red.DealDamage(blue);

Any class may call the methods of another class if those methods are declared in the public scope. If they're in the private scope they may not be called by any other class. There is also protected scope, which are methods that may be called by subclasses of the class. You'll notice that I passed one battleship as an argument to another, this is normally how objects can be passed other objects externally, then can have methods called upon it.

I think that's what you were trying to do, and if there are any other questions don't hesitate to ask.
Wow I changed my sig!

#3
masky007

masky007

    Newbie

  • Members
  • PipPip
  • 19 posts
hi, thats exactly what i am looking to achieve but there is a problem with this if i am not doing anything wrong..
It seems i cannot pass the object as argument. when i try to do that my ide gives syntax error

here is the code:
#include <iostream>



using namespace std;


class RedBattleship

{

public:

	RedBattleship(int healthPoints)

		: m_healthPoints(healthPoints) {}


	void TakeDamage()

	{

		m_healthPoints -= 100;

	}


	void DealDamage(BlueBattleship &blue)

	{

		blue.TakeDamage();

	}


private:

	int m_healthPoints;

};


class BlueBattleship

{

public:

	BlueBattleship(int healthPoints)

		: m_healthPoints(healthPoints){}


	void TakeDamage()

	{

		m_healthPoints -= 100;

	}

	void DealDamage(RedBattleShip &blue)

	{

		blue.TakeDamage();

	}


private:

	int m_healthPoints;

};


int main()

{

	RedBattleship red(500);

	BlueBattleship blue(500);


	red.DealDamage(blue);


	

	return 0;

}

.cpp(17): error C2061: syntax error : identifier 'BlueBattleship'

.cpp(19): error C2065: 'blue' : undeclared identifier

.cpp(19): error C2228: left of '.TakeDamage' must have class/struct/union

1>          type is ''unknown-type''

1.cpp(36): error C2061: syntax error : identifier 'RedBattleShip'

.cpp(38): error C2065: 'blue' : undeclared identifier

.cpp(38): error C2228: left of '.TakeDamage' must have class/struct/union

1>          type is ''unknown-type''

.cpp(50): error C2660: 'RedBattleship::DealDamage' : function does not take 1 arguments

1>

1>Build FAILED.

it seems that whenever i tryy to pass the oject as an argument the compilers says that there is no yet any kind of RedBattleShip identified.

edit: i played around to see if i could eliminate the problem somehow.. but i ended up with a mess

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
While processing RedBattleship, BlueBattleship hasn't been declared yet. Also, be careful about case. RedBattleship is different from RedBattleShip. See if this helps:
#include <iostream>


using namespace std;
class BlueBattleship;
class RedBattleship
{
public:
    RedBattleship(int healthPoints)
        : m_healthPoints(healthPoints) {}

    void TakeDamage()
    {
        m_healthPoints -= 100;
    }

    void DealDamage(BlueBattleship &blue)
    {
        blue.TakeDamage();
    }

private:
    int m_healthPoints;
};

class BlueBattleship
{
public:
    BlueBattleship(int healthPoints)
        : m_healthPoints(healthPoints){}

    void TakeDamage()
    {
        m_healthPoints -= 100;
    }
    void DealDamage(RedBattleship &blue)
    {
        blue.TakeDamage();
    }

private:
    int m_healthPoints;
};

int main()
{
    RedBattleship red(500);
    BlueBattleship blue(500);

    red.DealDamage(blue);

    
    return 0;
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
masky007

masky007

    Newbie

  • Members
  • PipPip
  • 19 posts
thanks that helps :) i didn't knew i could only declare the class first :) ... cool

edit: i just ran home to try this but it didn't worked.. i though it would work when i first saw it...


.cpp(19): error C2027: use of undefined type 'BlueBattleship'
.cpp(5) : see declaration of 'BlueBattleship'
.cpp(19): error C2228: left of '.TakeDamage' must have class/struct/union[/HTML]

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Out of curiosity, why are you defining two classes that are basically identical? Why not just declare type Battleship?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
masky007

masky007

    Newbie

  • Members
  • PipPip
  • 19 posts
that's not my main code.. i am planing to use more kind of ships.. with different abilities like nukes bullets lasers shields different level of healoiints, even swarms of more objects of little ones.. thats why i am planning more than one class... the code above was just an example given by me on the fly to explain my question. :>

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Sounds like you should have a parent class of BattleShip, and child classes for different weapon types. Heck, you could have the weapons be children of a Weapon class. If you create BattleShip and Weapon as pure abstract classes, then you can use them to define the interfaces their children will implement, and you will probably have fewer issues with compiling :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
masky007

masky007

    Newbie

  • Members
  • PipPip
  • 19 posts
i think the best is to drop this to rest and continue with my book :$ heh :P .. anyways really why do i get compile errors with this? and why do you say i will prob get errors hehe :)?

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
oh. Duh! Define your methods AFTER you define your classes. Don't do inline definitions.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
masky007

masky007

    Newbie

  • Members
  • PipPip
  • 19 posts
i do that in princip , it was just here for the simplicity of explaining.. anyways i am bumping my head against the wall and i cant make this working :cursing:

edit: shhhhht! i have made it somehow playing around with i don't know how many hours now lolz :D.. saving it and studying the code carefully now.. i will evaluate laterz.. thanks guys the both of you were of great help!
(i just hope i won't mess it up again hehe)

#12
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Like WP said, having a base class will reduce errors, but it will also make your life easier because you will only have to update/modify code in one place.

.cpp(19): error C2228: left of '.TakeDamage' must have class/struct/union
You get that error because you only forwardly declared a class, but not methods/properties.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users