How to stop money from going over

Pages: 1234... 7
oh thats right i forgot the constructor, do i create it in main.cpp or Test_Class.h?
never mind i think i got it, i just put it in the main.cpp
wait a minute, should

testClass::testClass()
{}

testClass::~testClass()
{}

go in main.cpp or test.cpp?
test.cpp
ah ok thanks
ok so now i got things all set up with 2 classes does everything look good? i want to make sure im doing it right.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "test.h"
#include "Player.h"

using namespace std;

int main()
{
    Player pc;

    pc.start();
}



Test_Class.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef TEST_CLASS_H_INCLUDED
#define TEST_CLASS_H_INCLUDED

#include <string>

class testClass
{
    public:
        testClass();
        ~testClass();

    protected:
        std::string name;
        int money;
};

#endif // TEST_CLASS_H_INCLUDED 



test.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "Test_Class.h"
#include "Player.h"
#include <iostream>
#include <string>

using namespace std;

testClass::testClass()
{

}

testClass::~testClass()
{

}

Player::Player()
{

}

Player::~Player()
{

}

void Player::start()
{
    cout << "This is the beginning" << endl;
    getline(cin, name);

    cout << name << endl;
    
    plr_name = name; //this has no real reason im just too lazy to change stuff

    cout << plr_name << endl;
}



Player.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED

#include <string>
#include "Test_Class.h"

class Player : public testClass
{
    public:
        Player();
        ~Player();
        void start();

    private:
        int plr_health;
        std::string plr_name;
};

#endif // PLAYER_H_INCLUDED 
I would make your test class private and not protected unless you plan of have some derived classes.
Also you aren't even using your constructors :p
One more suggestion for each header file have a separate source.
ex:

For your player header/class have a player source instead of combining with the test class.

For the most part for each class you have you want one header/source for it unless they are very similar/derived classes.
Ok but when i make it private i get 6 errors

1
2
3
4
5
6
7
8
9
10
11
class Player : public testClass
{
    public:
        Player();
        ~Player();
        void start();

    private:
        int plr_health;
        std::string plr_name;
};


or do you mean do this: class Player : private testClass


||=== Modular testing, Debug ===|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h||In member function 'void Player::start()':|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|13|error: 'std::string testClass::name' is private|
C:\Users\Chay Hawk\Desktop\Modular testing\test.cpp|31|error: within this context|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|13|error: 'std::string testClass::name' is private|
C:\Users\Chay Hawk\Desktop\Modular testing\test.cpp|33|error: within this context|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|13|error: 'std::string testClass::name' is private|
C:\Users\Chay Hawk\Desktop\Modular testing\test.cpp|35|error: within this context|
||=== Build finished: 6 errors, 0 warnings (0 minutes, 1 seconds) ===|

How do i use the constructors, i never really used them? can you show me how please.
Last edited on
If you have a variable as private, you're going to need a public function to access the variable value if needed. The syntax for the constructor is like:

1
2
3
4
5
// using your player class as an example
Player(/*arguments here*/) // note that there isn't a defined type, plus it must have the same name as the class
{
    //whatever code you want to execute when the object is initialized.
}
Oh whoops sorry I didn't see you had it derived yeah protected if you have derived. Most of the time if you are going to derive a class from a base class you will put those two classes together in the same header file.

http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/classes2/
http://www.cplusplus.com/doc/tutorial/inheritance/

Constructors are the main purpose of having a class =p

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//square.h
class Square
{
    public:
        Square( void );
        Square( const int x );
        int area( void );
    private:
        int width;
};

//square.cpp
Square::Square( void ) : width( 2 ){} //default is a 2x2 square
Square::Square( const int x ) : width( x ){} //initialize width to value of x
int Square::area( void ){ return( width * width ); }

//main.cpp
#include <iostream>

int main()
{
    Square s1 , s2( 3 );
    std::cout << s1.area() << std::endl;
    std::cout << s2.area() << std::endl;
}


Say you had a shape class you could use that as a base and then square, rectange , circle , triangle , ect..as the sub classes.

*edit put semi-colon where curly braces belonged on accident.
Last edited on
ok i read all of that and im still confused, those tutorials dont help me at all either.
What all are you confused on exactly?
using the constructors. i think it would help if we used my classes as explanations because when i see code that isnt mine it just makes it soooo much harder for me to understand it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Player
{
    public:
        Player();
        ~Player();
        void start();

    private:
        int plr_health;
        std::string plr_name;
};

Player::Player(): plr_name(){}

Player::~Player()
{

}
Last edited on
You use the constructors to create the class Object.
You can initialize the private variables using the constuctor/overloads
As you see I initailzied the private variables in the square object with the overload.
Aftre the object is initalized you can then do what ever like find the area of the Object.
Ok well i already ran into a problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Player
{
    public:
        Player();
        Player(string plr_name); //this will change so cant be constant
        ~Player();
        void start();

    private:
        int plr_health;
        std::string plr_name;
};

Player::Player(): plr_name("player1"){}

Player::~Player()
{

}
You are on the right track.
How about you add an overlaoded constructor also that takes in the health and name values?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Player
{
    public:
        Player();
        Player( int hp , std::string name );
        ~Player();
        void start();
        void output();

    private:
        int plr_health;
        std::string plr_name;
};

Player::Player(): /*this is the private value you are initalizing*/plr_name(/*value you are setting it to*/){}
//You must initalize in the order you defined which would be health then name.
Player::Player( int hp , std::string name ) : plr_health( hp ) , plr_name( name ){} //plr_health == hp && plr_name == name
//we can test this by outputting it by either overloading the operator<< or //making an output function I will example with output function

void Player::output(){ std::cout << "Name - " << plr_name << std::endl << "health - " << plr_health << std::endl; }

Player::~Player()
{

}


Hope this helps.

*edit just saw your second post
thats where you are mislead on line 5 the parameter can be constant because we are not modifying that we are modifying the private variable
Also if you have a const private variable the only way to initialize it is to use
the initalize list( constructor ) -> : co_variable( some_value ){}


http://www.cprogramming.com/tutorial/initialization-lists-c++.html
Last edited on
Hello,
It seems you are up to your neck in this game of yours. Maybe I can help.

Constructors are an easy way to set object variable upon creation of an instance of your object.

So for example... If your class player has a string variable called plr_name, you can use the constructor to place a name in that string variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Player
{
    public:
        Player(string);
        ~Player();
        void start();

    private:
        int plr_health;
        std::string plr_name;
};

Player::Player(string a) {
     plr_name =  a;
}

int main() {
Player player1("Bob");
return 0;
}
@giblit i get errors when running what you had, is Mangas way another way as well? people tell me and show me so many different ways of doing it it just confuses the hell out of me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <string>
#include <iostream>

class Player
{
    public:
        Player();
        Player(int hp, std::string name);
        ~Player();
        void start();
        void output();

    private:
        int plr_health;
        std::string plr_name;
};

Player::Player(): plr_name("player1")
{
}

Player::Player(int hp, std::string name) : plr_health( hp ),
                                           plr_name( name )
                                           {              }

Player::~Player()
{

}

void Player::output()
{
    std::cout << "Name - " << plr_name << std::endl << "health - " << plr_health << std::endl;
}


obj\Debug\test.o||In function `ZN6PlayerC2Ev':|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|21|multiple definition of `Player::Player()'|
obj\Debug\Player_Source.o:C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|21|first defined here|
obj\Debug\test.o||In function `ZN6PlayerC2Ev':|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|21|multiple definition of `Player::Player()'|
obj\Debug\Player_Source.o:C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|21|first defined here|
obj\Debug\test.o||In function `ZN6PlayerC2EiSs':|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|25|multiple definition of `Player::Player(int, std::string)'|
obj\Debug\Player_Source.o:C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|25|first defined here|
obj\Debug\test.o||In function `ZN6PlayerC2EiSs':|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|25|multiple definition of `Player::Player(int, std::string)'|
obj\Debug\Player_Source.o:C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|25|first defined here|
obj\Debug\test.o||In function `ZN6PlayerD2Ev':|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|29|multiple definition of `Player::~Player()'|
obj\Debug\Player_Source.o:C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|29|first defined here|
obj\Debug\test.o||In function `ZN6PlayerD2Ev':|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|29|multiple definition of `Player::~Player()'|
obj\Debug\Player_Source.o:C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|29|first defined here|
obj\Debug\test.o||In function `ZN6Player6outputEv':|
C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|35|multiple definition of `Player::output()'|
obj\Debug\Player_Source.o:C:\Users\Chay Hawk\Desktop\Modular testing\Test_Class.h|35|first defined here|
||=== Build finished: 14 errors, 0 warnings (0 minutes, 1 seconds) ===|
It says you have multiple definitions..show me all the files you have that looks like your header/source combined into one file
Also line 18 isn't really valid you have to initialize in the other they were declared . You can do it his way but if you ever have a const/reference for your private variables his way will not work. He is not initializing so technically they are undefined then he is re-assigning a value to them as my way initializes them so there for they are never undefined.
Pages: 1234... 7