How can i use getters and setters with math?

Pages: 1234
Could you by chance upload your project? that would be easier. Then I can just open up what you have and look at everything, then i'll make some of my own classes and go from there.
Last edited on
bump
Hi,

Would it be that bad to copy / paste those 6 files?
I was having a ton of problems with my project, So i just decided to make a new one and copy the files over to that, and I got it working :) I'll read your last post and look over the code and make some of my own classes.

BTW thank you for all your help and for having patients with me, I really appreciate it.
ok so i'm writing the medical class and so anything that all the items like health pack and adrenalin shot have in common go into the base class right?

So they both have names so name would go into the base, and they both give the player health so that would go into the base, but the adrenalin shot gives the player a faster walking speed so that would only be in adrenaline class right?
Hi,

.... so anything that all the items like health pack and adrenalin shot have in common go into the base class right?


Anything that those two have go into the medical class, as I mentioned earlier.

So they both have names so name would go into the base, ......


Name is already in the base class (CResource), all you have to do is extend it. That is, CMedical inherits from CResource , as I mentioned earlier.

....... and they both give the player health so that would go into the base, but the adrenalin shot gives the player a faster walking speed so that would only be in adrenaline class right?


So what is this with Player Health, walking speed etc ? For now, we are only doing something simple (A Player has some stuff). Can we see if we can get that happening first, before we venture into adding more things? The reason for this is that we don't want to get involved in any thing that involves behaviour relationships just yet, because this complicates things.

Player health doesn't belong in any of the CResource classes.

So can you make the classes I mentioned in my earlier posts, and get that working?

Hope all is well :+)

Hey, haven't forgot, I'm plannin on getting to it, my computer hard drive crashed and i had to send it all the way to california to get it repaired. I lost everything but i still have the code pasted on here so i'll copy it and get back to it.
ok so i got my project set up and I have one error in my CMedical which is on this line:


CMedical.h

1
2
3
4
CMedical::CMedical(const std::string& Name): m_Name(Name)
{
    //ctor
}


||=== Build: Debug in Class Project (compiler: GNU GCC Compiler) ===|
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp||In constructor 'CMedical::CMedical(const string&)':|
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp|6|error: no matching function for call to 'CResource::CResource()'|
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp|6|note: candidates are:|
C:\Users\thund_000\Desktop\Class Project\CResource.h|11|note: CResource::CResource(const string&)|
C:\Users\thund_000\Desktop\Class Project\CResource.h|11|note: candidate expects 1 argument, 0 provided|
C:\Users\thund_000\Desktop\Class Project\CResource.h|7|note: CResource::CResource(const CResource&)|
C:\Users\thund_000\Desktop\Class Project\CResource.h|7|note: candidate expects 1 argument, 0 provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


CMedical.cpp

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

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

class CMedical : public CResource
{
    public:
        CMedical(const std::string& Name);
        ~CMedical();

        virtual void PrintDetails();

    private:
        const std::string m_Name;
};

#endif // CMEDICAL_H 


Also I'm going to change those .h extensions to CPP, but I want to solve this first. I had them changed and working before my PC Crashed -_-
Last edited on
Hi,

The CVehicle, CMedical and CWeapon classes are similar, each has a constructor which calls it base class constructor; the member variables ( you need to add them in) that belong to the class are also initialised in the initialisation list.

Name belongs to the CResource class (it's already there), not the CMedical class. That is the whole idea of inheritance.

So all you need to do is look at the code I wrote, and do something similar for the new classes.

Hope all goes well :+)
Ok, well I think I'm going to take a couple days and read up on classes and inheritance to freshen my memory and watch some tutorials on youtube and then when I come back i'll read the entire post all over again and then hopefully I'll be able to understand it, I'm just not understanding whats going on for some reason, probably because I havent been programming for a long time. When you ask me to do something I just draw a blank and that's not good, that's not going to get us anywhere, I should have had this done a long long time ago. So i'll go study and i'll be back whenever I can figure stuff out. I plan on studying:

Inheritance
Classes
Polymorphism
Friendship

Anything else?
Last edited on
Hi,
Just wondering, once you have done your reading, could you explain how the inheritance, polymorphism and constructors work in the program we have so far? Especially the vector of resources we have in main, explain how that can be fed any pointer to a derived resource class, explain how the PrintDetails can still be used over the whole container regardless of what type the poiner is.

If you can all that out, that would be awesome 😀
Yeah I can do that, i have a few C++ books lying around and some PDF versions i'll get studying here soon. I'm trying to study a whole bunch of stuff at once, which may or may not be a good idea. I'm learning how to use a Katana, learning 3 different video editing softwares, Photoshop, Japanese and C++ D: I'll try to study some C++ tonight though.
alright we'll i've been looking through stuff and i cant get that error to go away and it's really irritating me so I give up, it's just too overwhelming for me. I re watched some tutorials and read some sections on that stuff and i still don't get it. I'm a visual learner, I learn by looking at something that's completed and then dissecting it, that's how it's always been and this just proves it even further. It's been 3 months since I first posted this thread and if I didnt get it in the first day then I don't expect to get it now. :( Sorry, I tried, I really did. If you could get it to work I could then tell you whats going on, cause like I said thats just how I learn things, then we can actually get somewhere, but if not thats fine.
Last edited on
Hi,

C:\Users\thund_000\Desktop\Class Project\CMedical.cpp||In constructor 'CMedical::CMedical(const string&)':|
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp|6|error: no matching function for call to 'CResource::CResource()'|


The Name variable is in the CResource class, so a derived class has to call the CResource constructor, just as the compiler error says. The same is true for any derived class, they have to call a constructor of it's parent - in order to initialise the parent class member variables.

...... I'm a visual learner, I learn by looking at something that's completed and then dissecting it, ......


As I was saying earlier, the classes you need to create are very similar to ones that I had. So all you need to do is the the same as I what I did. Obviously the classes are going to have their own variables, but the general idea of how they work is the same.

Look at the CResource class, The parameters to it's constructor are the Name, which is initialised in the initialiser list.

Look at the CVehicle class. The parameters to it's constructor are the Name, and the two variables that belong to the CVehicle class. In the initialiser list, there is a call to the CResource constructor, with Name as the argument. Then the two variables that belong to the class are initialised.

Now look at the CFourWheelDrive class. It's parameters are the Name, the two values that belong to the member variables in the CVehicle class, and values that belong to the member variables of the CFourWheelDrive class. In the initialiser list, the constructor for the CVehicle class is called with the arguments it needs. Then the variables that belong to the class are initialised.

So can you see the pattern here? A constructor for a derived class takes arguments (parameters) in order to initialise it's parent class, which in turn initialises it parent class, and so on until the very top base class is initialised. Note that the base class is created first, then successive derived classes, which is the reverse order of how they are called.

With the polymorphism, there is a pure virtual function called PrintDetails in the CResource class. A virtual function means that the function may be redefined by a derived class. A pure virtual function means that the function must be redefined by derived classes, so this forces specialised behaviour for the derived classes. An ordinary virtual function allows the same behaviour to apply to many derived classes, but the function is only defined once.

Now in CPlayer we have the std::vector Gear, which it is very important to note that it holds pointers to CResource

This is important because a pointer to a Derived class is a valid pointer to a Base class.

So the AcquireResource function can push a pointer to any of the derived CResource classes (FourWheelDrive, MediPack, Assault rifle say) into the Gear vector.

Now for the best part: In the CPlayer::ShowResources function when the the PrintDetails function is called for each of the pointers in the Gear vector, the correct function is called. The beauty of this is that each class has a function with the same name, so we can extend our inheritance tree without having to change things everywhere.

I hope this is enough information for you to fix up your existing code, and write some new code - remember it is similar to what you have already.

Hope all goes well. :+)
ok so here is my medical class, hopefully i'm getting closer, I think I understand what is going on a lot better:

1
2
3
4
5
6
7
8
#include "CMedical.h"

#include <string>

CMedical::CMedical(std::string Name, int amount): CResource::CResource(Name), m_amount(amount)
{
    //ctor
}



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

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

class CMedical : public CResource
{
    public:
        CMedical(std::string Name, int amount);

        int Getm_amount(){}

    private:
        int m_amount;
};

#endif // CMEDICAL_H 



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
#include <iostream>

#include "CFourWheelDrive.h"
#include "CPlayer.h"
#include "CMedical.h"


int main() {

    CFourWheelDrive MyFourby("MyFourby", 4200, 7, true);
    CFourWheelDrive YourFourby("YourFourby", 5000, 7, false);

    CMedical MedPack("Med Pack", 4);


    CPlayer Me("TheIdeasMan");

    Me.AcquireResource(&MyFourby);
    Me.AcquireResource(&YourFourby);

    Me.ShowResources();


return 0;
}
Last edited on
Hi,

Ok, thanks for the effort you have put in so far - that's great.

There are a couple of things to change in the CMedical class. CVehicle, CMedical, and CWeapon are similar in that they are just a place holder (a general type, for the more specialised types that inherit from it), so we don't want to be able to create an object of that type because it is incomplete. There is no pure virtual function in these classes, so to achieve the same thing, we make the constructor protected:

The other thing was the member variable we were going to have was called m_Size, and it should be an enum, so we can have small, medium and large sizes.

We could have a constexpr unsigned short m_MaxNumAllowed = 4; in order to limit how many medipacks or adrenalin shots can possessed at any given time. You can add this in, it will be a reminder that we need to do some code to implement that behaviour. There are some other implications that we can cover later.

In main(), you can add an object that represents you as a Player - call it Ch1156 or Chay or whatever you want. Add some other objects of type CVehicle, and use the AcquireResource function to add them to your list of stuff. Call the ShowResources() to print the list of your stuff.

Add the CMedipack and CAdrenalinShot classes. Don't worry about making some member variables for them just yet - we can add them later. Make sure you provide values to set the variables that belong to their parent classes (m_Name, m_Size).

You can add a CWeapons class, remember it will be similar to the CVehicle. Add the member variable double m_Weight.

Add the CRifle class, with member variable double m_BoreSize .

Add the CGrenade class, with member variable double m_ExplosivePower .

For all these new classes, create objects of those types in main(), acquire them and call ShowResources.

Remember that all these new classes are very similar to what we have already

Hope all goes well, And I look forward to seeing your new code. :+)
Last edited on
Ok so this is what I came up with, and I got some errors that I cant seem to fix:

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

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

class CMedical : public CResource
{
    public:


    protected:
        CMedical(std::string& Name, enum Size);

    private:
        enum m_Size{};
};

#endif // CMEDICAL_H 



1
2
3
4
5
6
7
8
#include "CMedical.h"

#include <string>

CMedical::CMedical(std::string& Name, enum Size): CResource::CResource(Name), m_Size(Size)
{
    //ctor
}



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
#include <iostream>

#include "CFourWheelDrive.h"
#include "CPlayer.h"
#include "CMedical.h"


int main() {

    CFourWheelDrive MyFourby("MyFourby", 4200, 7, true);
    CFourWheelDrive YourFourby("YourFourby", 5000, 7, false);

    CPlayer Chay("Chay");
    CFourWheelDrive MyCar("Truck", 5000, 4, true);

    Chay.AcquireResource(&MyCar);

    CPlayer Me("TheIdeasMan");

    Me.AcquireResource(&MyFourby);
    Me.AcquireResource(&YourFourby);

    Me.ShowResources();


return 0;
}



||=== Build: Debug in Class Project (compiler: GNU GCC Compiler) ===|
C:\Users\thund_000\Desktop\Class Project\CMedical.h|13|error: use of enum 'Size' without previous declaration|
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp|5|error: use of enum 'Size' without previous declaration|
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp||In constructor 'CMedical::CMedical(std::string&, int)':|
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp|5|error: 'enum CMedical::m_Size' is not a non-static data member of 'CMedical'|
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp|5|error: 'Size' was not declared in this scope|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Ok,

So you need to tell the compiler what the enum Size is - it's not a mind reader. As the error message says - "without previous declaration". So do some research to see how to use an enum. Sorry I am short of time, and it is good for one to learn how to do their own research. If you are going to use some feature of the language, always look up the reference for it beforehand.

So you can carry on and do the CWeapons class and it's children. It is simple just like the CVehicle class.

A minor formatting thing (what you did is not wrong), split things onto different lines to make them easier to read:

1
2
3
4
5
6
7
8
9
10
11
#include "CMedical.h"

#include <string>

CMedical::CMedical(std::string& Name, enum Size) : 
       // list the initialisers here, 1 per line
       CResource::CResource(Name), 
       m_Size(Size)
{
    //ctor
}


Hope all goes well :+)
Topic archived. No new replies allowed.
Pages: 1234