I'm learning c++ at the moment, and i just learnt about classes, so i decided to make a text game with a badguy and player class, and i was wondering how to refer to other classes variables(like this.health)
from other classes. and wether you have to create an instance of those classes like you do in python, heres my code:

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

class Badguy {
  private:
    int this.health = 10, this.defense = 3;
    char this.name = "Orc";
    
  int getAttributesInfo() {
    attributes [] = {this.health, this.defense, this.name};
    return attributes;
  }     
} ;

class Player {
  private:
    srand((1),(7));
    int this.attack = rand();
    char name = "Player";
    
  int getDamage(void) {
    int Badguy.damage = this.attack - defense;
    if (Badguy.damage > Badguy.defense) {
        Badguy.health -= Badguy.damage;
      }     
  }
} ;

int main() {

}

And if you could tell me about any errors in this that would be appreciated :)

Recommended Answers

All 23 Replies

I figure I ought to start off with in C++ with classes you can only declare variables not initialize them... Initializing them can be done via constructor.

class example
{
    int testint;
    public:
    example();
    int ret_val(){return testint;};
};

example::example():testint(123){}; //Constructor initializes testint to 123. You can also do initialization & all in the {} like a function.

int main()
{
    example obj;
    cout << "testint is: " << obj.ret_val() << "\n";
    return 0;
}

Accessing other class variables either needs those variables to be declared public, or to be accessed by a public member function of the class you want the variables for.

Or you can declare a friend class or similar, excuse the somewhat poorly written example, it is messy but I'm hoping it gets the point across...

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

class ref
{
    int health;
    char undead;
    string Name;
    public:
    ref();
    void Add_Attrib(int& x, char& y){ x = health; y = undead;};
    friend class example; //class example is friend.
};

class example
{
    int testint;
    char testchar;
    ref obj; //creates object of ref.
    public:
    example();
    int ret_val(){
        obj.Add_Attrib(testint, testchar);
        cout << "Health is: " << testint << "\nUndead: " << testchar << "\n"; return testint;};
        void friend_ex(){cout << "Enemy: " << obj.Name << endl;}; //example displays a private member of class ref without calling a member function of ref.
};

example::example():testint(123){}; //Constructor initializes testint to 123.
ref::ref():health(100), undead('y'), Name("Orc"){};

int main()
{
    example obj;
    cout << "testint is: " << obj.ret_val() << "\n";
    obj.friend_ex(); //uses object of class example to display private member of another class in this case class ref.
    return 0;
}

Take note that despite the constructor initializing testint to 123, because of Add_Attrib taking 2 reference parameters which 1 were testint, it changes the value to health instead... Just play around with the code & you'll soon see what you can do.

Although Assigning variables from another class means doing so in the bracket... Example...

example::example():testint(123){ NewName = obj.Push1;}; //Constructor initializes testint to 123.
ref::ref():health(100), undead('y'), Name("Orc"),Push1("Test Example"){};

Just the constructors, Push1 is a private member in class ref of type string, NewName is of type string in class example... You can see in the constructor there, that NewName is initialized inside the block this time.

Again Apologies for the poorly written example, I sort of just winged it quickly! :)

Can you re-write my program so that it works, i find it easier to figure out what i did wrong that way:)

You'll learn more from trial & error when playing around with code yourself, having someone else write it for you won't strengthen your ability.

I've given you the necessary code for you to be able to correct your own code. I've told you that you can't initialize a variable in a class, instead you need to use the constructor. which should be declared public & be the same name of the class without a return type specified. (eg class Badguy constructor would be "public: Badguy();" without ")

I've also displayed multiple ways of accessing private members of other classes to which all it requires from you now, is to use the code I shown you to try stuff out & learn from it, to which you will then be able to apply it to your own needs much better than if someone re-writes your actual program for you.

Last but not least, Google is your friend, need more examples... Google up tutorials on classes & friend classes/friend functions.

You'll learn more from trial & error when playing around with code yourself, having someone else write it for you won't strengthen your ability.

I've given you the necessary code for you to be able to correct your own code. I've told you that you can't initialize a variable in a class, instead you need to use the constructor. which should be declared public & be the same name of the class without a return type specified. (eg class Badguy constructor would be "public: Badguy();" without ")

I've also displayed multiple ways of accessing private members of other classes to which all it requires from you now, is to use the code I shown you to try stuff out & learn from it, to which you will then be able to apply it to your own needs much better than if someone re-writes your actual program for you.

Last but not least, Google is your friend, need more examples... Google up tutorials on classes & friend classes/friend functions.

Would this work:

class Badguy {
  private:
    int attributes [] = {int health, int defense, char name};
  public:
    badguy();
    int ret_val(){return attributes;};    
};

class Player {
  private:
    int attributes[] = {char name, int attack};
  public:
    player();
    int ret_val(){return attributes;};
    int attack() {
      
    }      
};

Badguy::badguy()::attributes(){health, defense, name};
Player::Player()::attributes(){name, attack};
int this.health = 10,
          this.defense = 3;    
         char this.name = "Orc";

//you can't init them here;

int getAttributesInfo() {    
attributes [] = {this.health, this.defense, this.name};  
return attributes;  }

you declread the function as int but "return attributes" confused me, else attributes is just an address.

Would this work:

class Badguy {
  private:
    int attributes [] = {int health, int defense, char name};
  public:
    badguy();
    int ret_val(){return attributes;};    
};

class Player {
  private:
    int attributes[] = {char name, int attack};
  public:
    player();
    int ret_val(){return attributes;};
    int attack() {
      
    }      
};

Badguy::badguy()::attributes(){health, defense, name};
Player::Player()::attributes(){name, attack};

Have you tried compiling it & seeing for yourself? The short answer would be no & there are quite a few reasons why & since it's late & I'm tired I'll go through it quickly...

Elements of an array are the same type as the array, that is an int array only holds ints & they don't have different names, in the case of

int attributes [] = {int health, int defense, char name};

it's an invalid definition of an array & don't forget you can't initialize in a class anyway, I'd honestly vouch for taking the easier route & getting rid of attributes... Just declare

int health, defence;
string name;

instead or something similar.

You almost got the constructor call right, badguy(); is case sensetive to the class name, that is

class BadGuy
{
public:
Badguy();
}

is different than

class BadGuy
{
public:
BaDGuY();
}

to which both are invalid... On line 4: BadGuy(); would be correct however! :)

Badguy::badguy()::attributes(){health, defense, name};
Player::Player()::attributes(){name, attack};

Style is, Class::Constructor():Variable(Value){};
Notice that after Constructor there is only 1 colon, the :: is a scope operator, which isn't what is wanted. Inside the blocks you can also do initialization... Example

Badguy::Badguy():health(100){};

is the same as

Badguy::Badguy(){health = 100;};

Both of those initialize health to 100.
Assume you declare int health, defence; in the Badguy class then an initializer like so would work...

Badguy::Badguy(){health = 100; defence = 100;};

Both health & defence in the class would then be 100. Try compiling the code yourself to see if it works.

Ok, thanks for that :)

Have you tried compiling it & seeing for yourself? The short answer would be no & there are quite a few reasons why & since it's late & I'm tired I'll go through it quickly...

Elements of an array are the same type as the array, that is an int array only holds ints & they don't have different names, in the case of

int attributes [] = {int health, int defense, char name};

it's an invalid definition of an array & don't forget you can't initialize in a class anyway, I'd honestly vouch for taking the easier route & getting rid of attributes... Just declare

int health, defence;
string name;

instead or something similar.

You almost got the constructor call right, badguy(); is case sensetive to the class name, that is

class BadGuy
{
public:
Badguy();
}

is different than

class BadGuy
{
public:
BaDGuY();
}

to which both are invalid... On line 4: BadGuy(); would be correct however! :)

Badguy::badguy()::attributes(){health, defense, name};
Player::Player()::attributes(){name, attack};

Style is, Class::Constructor():Variable(Value){};
Notice that after Constructor there is only 1 colon, the :: is a scope operator, which isn't what is wanted. Inside the blocks you can also do initialization... Example

Badguy::Badguy():health(100){};

is the same as

Badguy::Badguy(){health = 100;};

Both of those initialize health to 100.
Assume you declare int health, defence; in the Badguy class then an initializer like so would work...

Badguy::Badguy(){health = 100; defence = 100;};

Both health & defence in the class would then be 100. Try compiling the code yourself to see if it works.

Hi,
I suggest you to have a good understanding of following features from C and C++ before you begin writing good classes.

C

datatypes
Arrays
character strings and string functions.
Structures
Functions


C++

class & object
bool datatype
constructor
destructor
functions in C++
default arguments to function parameters
this pointer
reference variable
---------------------

http://www.cplusplus.com/doc/tutorial/
should be good to start at.

I did it, Thanks for all your help, heres my code if you want to see:

#include <iostream>
using namespace std;

class Badguy {
  private:
    int health, defense;
    char name;
    
  public:
    Badguy();
    int ret_val(){return health, defense, name;};    
};

class Player {
  private:
    char name;
    int attack;

  public:
    Player();
    int ret_val(){return name, attack;};  
};

Badguy::Badguy(){
  health = 100; defense = 3;
};

Player::Player(){
  name = 'Tom'; attack = 5;
};

int main() {
  Player p;
  Badguy b;   
  cin.get();
}

How to you refer to variables from an other class from a class?
i want to write an attack function for the player, but i dont know how to do that ?

I did it, Thanks for all your help, heres my code if you want to see:

#include <iostream>
using namespace std;

class Badguy {
  private:
    int health, defense;
    char name;
    
  public:
    Badguy();
    int ret_val(){return health, defense, name;};    
};

class Player {
  private:
    char name;
    int attack;

  public:
    Player();
    int ret_val(){return name, attack;};  
};

Badguy::Badguy(){
  health = 100; defense = 3;
};

Player::Player(){
  name = 'Tom'; attack = 5;
};

int main() {
  Player p;
  Badguy b;   
  cin.get();
}

say you wish to refer to Badguy's health. Now, since 'health' is a private member of the Badguy class, you will not be able to mess with it. One way to work with both the player and badguy's variables are to use global friend functions. Suppose, you create a function to do the calculations for damages and stuff. You may go as :

#include <iostream>
using namespace std;

class Badguy {
  private:
    int health, defense;
    char name;
    
  public:
    Badguy();
    int ret_val(){return health, defense, name;};
    //Friend Function is Used now
    Friend void damageCalc(Badguy Brutus, Player Popeye);
};

class Player {
  private:
    char name;
    int attack;

  public:
    Player();
    int ret_val(){return name, attack;};  
    //Friend Function is Used now
    Friend void damageCalc(Badguy Brutus, Player Popeye);
};

void damageCalc(Badguy Brutus, Player Popeye){
//your calculations here.
//You will be able to access the variables in Badguy and Player
//Simultaneously.
}

Badguy::Badguy(){
  health = 100; defense = 3;
};

Player::Player(){
  name = 'Tom'; attack = 5;
};

int main() {
  Player p;
  Badguy b;   
  cin.get();
  damageCalc(b, p);
  //this will do your damage calculations and you will be able to
  //access all types of variables in both the classes
}

If I understood you question correctly, then this is the way to do it. Don't wory about the names used in the friend function, if you have good knowledge of functions then you will understand what i mean ;)

Hi,
In your statements like
return name, attack;
do you intend to return multiple things from your functions?

Hi,
I suggest you to have a good understanding of following features from C and C++ before you begin writing good classes.

C

datatypes
Arrays
character strings and string functions.
Structures
Functions


C++

class & object
bool datatype
constructor
destructor
functions in C++
default arguments to function parameters
this pointer
reference variable
---------------------

http://www.cplusplus.com/doc/tutorial/
should be good to start at.

Why are you quoting me when you make that post?

say you wish to refer to Badguy's health. Now, since 'health' is a private member of the Badguy class, you will not be able to mess with it. One way to work with both the player and badguy's variables are to use global friend functions. Suppose, you create a function to do the calculations for damages and stuff. You may go as :

#include <iostream>
using namespace std;

class Badguy {
  private:
    int health, defense;
    char name;
    
  public:
    Badguy();
    int ret_val(){return health, defense, name;};
    //Friend Function is Used now
    Friend void damageCalc(Badguy Brutus, Player Popeye);
};

class Player {
  private:
    char name;
    int attack;

  public:
    Player();
    int ret_val(){return name, attack;};  
    //Friend Function is Used now
    Friend void damageCalc(Badguy Brutus, Player Popeye);
};

void damageCalc(Badguy Brutus, Player Popeye){
//your calculations here.
//You will be able to access the variables in Badguy and Player
//Simultaneously.
}

Badguy::Badguy(){
  health = 100; defense = 3;
};

Player::Player(){
  name = 'Tom'; attack = 5;
};

int main() {
  Player p;
  Badguy b;   
  cin.get();
  damageCalc(b, p);
  //this will do your damage calculations and you will be able to
  //access all types of variables in both the classes
}

If I understood you question correctly, then this is the way to do it. Don't wory about the names used in the friend function, if you have good knowledge of functions then you will understand what i mean ;)

i wrote it like this:

void damageCalc(Badguy Brutus, Player Popeye){
  int Badguy.damage = Player.attack - Badguy.defense;
  if (Badguy.damage > Badguy.defense) {
    cout << "You damaged the orc!";
    Badguy.health -= badguy.damgage;
    cout << "The orc's health is: "  << Badguy.health << endl;
  }
}

but i get this error for this bit of code:

Code:

friend void damageCalc(Badguy Brutus, Player Popeye);

error:
`Player' has not been declared

Any ideas?

hmm.. maybe its because we declare player after we use the function. Which compiler do you use?? Can you paste the whole code so that i may try to compile it on my system, two minds are better than one!! ;)

hmm.. maybe its because we declare player after we use the function. Which compiler do you use?? Can you paste the whole code so that i may try to compile it on my system, two minds are better than one!! ;)

Heres the code:

#include <iostream>
using namespace std;

class Badguy {
  private:
    int health, defense;
    char name;
    
  public:
    Badguy();
    int ret_val(){return health, defense, name;};
    friend void damageCalc(Badguy Brutus, Player Popeye);
};

class Player {
  private:
    char name;
    int attack;

  public:
    Player();
    int ret_val(){return name, attack;};  
    friend void damageCalc(Badguy Brutus, Player Popeye);
};

void damageCalc(Badguy Brutus, Player Popeye){
  int Badguy.damage = Player.attack - Badguy.defense;
  if (Badguy.damage > Badguy.defense) {
    cout << "You damaged the orc!";
    Badguy.health -= badguy.damgage;
    cout << "The orc's health is: "  << Badguy.health << endl;
  }
}

Badguy::Badguy(){
  health = 100;
  defense = 3;
};

Badguy::~Badguy(){
  delete health;
  delete defense;
};

Player::Player(){
  name = 'Tom';
  attack = 5;
};

Player::~Player(){
  delete &name;
  delete &attack;                  
};

int main() {
  Player p;
  Badguy b;   
  damageCalc(b, p);
  cin.get();
}

i wrote it like this:

void damageCalc(Badguy Brutus, Player Popeye){
  int Badguy.damage = Player.attack - Badguy.defense;
  if (Badguy.damage > Badguy.defense) {
    cout << "You damaged the orc!";
    Badguy.health -= badguy.damgage;
    cout << "The orc's health is: "  << Badguy.health << endl;
  }
}

but i get this error for this bit of code:

Code:

friend void damageCalc(Badguy Brutus, Player Popeye);

error:
`Player' has not been declared

Any ideas?

hmm.. maybe its because we declare player after we use the function. Which compiler do you use?? Can you paste the whole code so that i may try to compile it on my system, two minds are better than one!! ;)

It's because in the 1st class where the function is declared, it's declaring an object of the class which is not yet declared...

In Badguy the function declaration, states 2 parameters, 1 object of Badguy, & 1 object of Player, yet the declaration of the class Player is after Badguy hence it doesn't yet exist, causing the error.

How would i fix that?

Check earlier posts, where I shown example of friend classes & accessing private members that way... Or generally above the declaration of the function in the 1st class add the 2nd class to be a friend... That is on Line 13 add friend class Player;

I got rid of that error by removing Badguy brutus and Player popeye from everything but now for this bit of code on line 3:

void damageCalc(){
  int Badguy.damage = Player.attack - Badguy.defense;
  if (Badguy.damage > Badguy.defense) {
    cout << "You hit the orc for" << Badguy.damage << endl;
    Badguy.health -= badguy.damgage;
    cout << "The orc's health is: "  << Badguy.health << endl;
  }
}

i get this error:
expected primary-expression before "int"

any ideas?

Ok.. i compiled and the following code is compiling. need to go for an 8 hour drive tomorrow morning at 6 so hitting my bed and can't help you till tomorrow night, though do post here anything new:

#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>

class Badguy {
    int health, defense, 
        damage; //You require this for damageCalc function
    char name;

public:
    Badguy();
    //The deconstructor was not declared:
    //~Badguy();
    int ret_val(){return health, defense, name;};
    friend class Player;
    friend void damageCalc(Badguy Brutus, Player Popeye);
    };

class Player {
    //No need to declare privates in class unless preceded by public!
    /*char []*/string name; //Name needs to be an array rather then a single character.
    int attack;

public:
    Player();
    int ret_val(){return name, attack;};  
    friend void damageCalc(Badguy Brutus, Player Popeye);
    };

void damageCalc(Badguy Brutus, Player Popeye){
    //Incorrect naming: Badguy should be Brutus and same for player!
    Brutus.damage = Popeye.attack - Brutus.defense;
    if (Brutus.damage > Brutus.defense) {
        cout << "You damaged the orc!";
        Brutus.health -= Brutus.damage;
        cout << "The orc's health is: "  << Brutus.health << endl;
        }
    cout << "The health is " << Brutus.health << endl;
    }

Badguy::Badguy(){
    health = 100;
    defense = 3;
    };

//The following destructor is not required as there are no arrays to be deleted.
//Int's don't require deletion.
//Badguy::~Badguy(){
//  delete health;
//  delete defense;
//};

Player::Player(){
    name = "Tom";
    attack = 5;
    };

//The following destructor is not required as there are no arrays to be deleted.
//Int's don't require deletion.
//Player::~Player(){
//  delete &name;
//  delete &attack;                  
//};

int main() {
    Player p;
    Badguy b;   
    damageCalc(b, p);
    cin.get();
    }

Hope this helps. Also, do read the comments, there were quite a few mistakes! lol!!

Thanks for all that ;)

I will soon be writing a small program to make use of your program for you in c++, though it would have seperate header file and a source file. i'll post it as soon as i make it.

PS:This will be a console app.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.