c++ beginner help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

c++ beginner help

 
0
  #1
Dec 16th, 2008
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:
  1.  
  2. #include <cstdlib>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class Badguy {
  7. private:
  8. int this.health = 10, this.defense = 3;
  9. char this.name = "Orc";
  10.  
  11. int getAttributesInfo() {
  12. attributes [] = {this.health, this.defense, this.name};
  13. return attributes;
  14. }
  15. } ;
  16.  
  17. class Player {
  18. private:
  19. srand((1),(7));
  20. int this.attack = rand();
  21. char name = "Player";
  22.  
  23. int getDamage(void) {
  24. int Badguy.damage = this.attack - defense;
  25. if (Badguy.damage > Badguy.defense) {
  26. Badguy.health -= Badguy.damage;
  27. }
  28. }
  29. } ;
  30.  
  31. int main() {
  32.  
  33. }

And if you could tell me about any errors in this that would be appreciated
...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: c++ beginner help

 
1
  #2
Dec 16th, 2008
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.

  1. class example
  2. {
  3. int testint;
  4. public:
  5. example();
  6. int ret_val(){return testint;};
  7. };
  8.  
  9. example::example():testint(123){}; //Constructor initializes testint to 123. You can also do initialization & all in the {} like a function.
  10.  
  11. int main()
  12. {
  13. example obj;
  14. cout << "testint is: " << obj.ret_val() << "\n";
  15. return 0;
  16. }

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

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class ref
  7. {
  8. int health;
  9. char undead;
  10. string Name;
  11. public:
  12. ref();
  13. void Add_Attrib(int& x, char& y){ x = health; y = undead;};
  14. friend class example; //class example is friend.
  15. };
  16.  
  17. class example
  18. {
  19. int testint;
  20. char testchar;
  21. ref obj; //creates object of ref.
  22. public:
  23. example();
  24. int ret_val(){
  25. obj.Add_Attrib(testint, testchar);
  26. cout << "Health is: " << testint << "\nUndead: " << testchar << "\n"; return testint;};
  27. void friend_ex(){cout << "Enemy: " << obj.Name << endl;}; //example displays a private member of class ref without calling a member function of ref.
  28. };
  29.  
  30. example::example():testint(123){}; //Constructor initializes testint to 123.
  31. ref::ref():health(100), undead('y'), Name("Orc"){};
  32.  
  33. int main()
  34. {
  35. example obj;
  36. cout << "testint is: " << obj.ret_val() << "\n";
  37. obj.friend_ex(); //uses object of class example to display private member of another class in this case class ref.
  38. return 0;
  39. }

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

  1. example::example():testint(123){ NewName = obj.Push1;}; //Constructor initializes testint to 123.
  2. 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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: c++ beginner help

 
0
  #3
Dec 16th, 2008
Can you re-write my program so that it works, i find it easier to figure out what i did wrong that way
...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: c++ beginner help

 
0
  #4
Dec 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: c++ beginner help

 
0
  #5
Dec 16th, 2008
Originally Posted by Yiuca View Post
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:

  1.  
  2. class Badguy {
  3. private:
  4. int attributes [] = {int health, int defense, char name};
  5. public:
  6. badguy();
  7. int ret_val(){return attributes;};
  8. };
  9.  
  10. class Player {
  11. private:
  12. int attributes[] = {char name, int attack};
  13. public:
  14. player();
  15. int ret_val(){return attributes;};
  16. int attack() {
  17.  
  18. }
  19. };
  20.  
  21. Badguy::badguy()::attributes(){health, defense, name};
  22. Player::Player()::attributes(){name, attack};
...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 17
Reputation: AHUazhu is an unknown quantity at this point 
Solved Threads: 2
AHUazhu AHUazhu is offline Offline
Newbie Poster

Re: c++ beginner help

 
0
  #6
Dec 16th, 2008
  1. int this.health = 10,
  2. this.defense = 3;
  3. char this.name = "Orc";
//you can't init them here;
  1. int getAttributesInfo() {
  2. attributes [] = {this.health, this.defense, this.name};
  3. return attributes; }
you declread the function as int but "return attributes" confused me, else attributes is just an address.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: c++ beginner help

 
0
  #7
Dec 17th, 2008
Originally Posted by tomtetlaw View Post
Would this work:

  1.  
  2. class Badguy {
  3. private:
  4. int attributes [] = {int health, int defense, char name};
  5. public:
  6. badguy();
  7. int ret_val(){return attributes;};
  8. };
  9.  
  10. class Player {
  11. private:
  12. int attributes[] = {char name, int attack};
  13. public:
  14. player();
  15. int ret_val(){return attributes;};
  16. int attack() {
  17.  
  18. }
  19. };
  20.  
  21. Badguy::badguy()::attributes(){health, defense, name};
  22. 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

  1. class BadGuy
  2. {
  3. public:
  4. Badguy();
  5. }

is different than

  1. class BadGuy
  2. {
  3. public:
  4. BaDGuY();
  5. }

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

  1. Badguy::badguy()::attributes(){health, defense, name};
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: c++ beginner help

 
0
  #8
Dec 17th, 2008
Ok, thanks for that
...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 14
Reputation: mrinal.s2008 is an unknown quantity at this point 
Solved Threads: 0
mrinal.s2008's Avatar
mrinal.s2008 mrinal.s2008 is offline Offline
Newbie Poster

Re: c++ beginner help

 
0
  #9
Dec 17th, 2008
Originally Posted by Yiuca View Post
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

  1. class BadGuy
  2. {
  3. public:
  4. Badguy();
  5. }

is different than

  1. class BadGuy
  2. {
  3. public:
  4. BaDGuY();
  5. }

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

  1. Badguy::badguy()::attributes(){health, defense, name};
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: c++ beginner help

 
0
  #10
Dec 17th, 2008
I did it, Thanks for all your help, heres my code if you want to see:

  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Badguy {
  6. private:
  7. int health, defense;
  8. char name;
  9.  
  10. public:
  11. Badguy();
  12. int ret_val(){return health, defense, name;};
  13. };
  14.  
  15. class Player {
  16. private:
  17. char name;
  18. int attack;
  19.  
  20. public:
  21. Player();
  22. int ret_val(){return name, attack;};
  23. };
  24.  
  25. Badguy::Badguy(){
  26. health = 100; defense = 3;
  27. };
  28.  
  29. Player::Player(){
  30. name = 'Tom'; attack = 5;
  31. };
  32.  
  33. int main() {
  34. Player p;
  35. Badguy b;
  36. cin.get();
  37. }
...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC