Page 54 of 68

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 4:57 am
by Yumtard
hmm now I'm, confused why we use inheritence.

if we make a class Character with a bunch of pure virtual functions. Then we make 2 new classes that inherits from Character. Since we need to implement all the functions we want to use, wouldn't it have been easier just to skip the whole Character part and just create the 2 classes?
I mean even if they both have the same Update function we'll still need to implement in both classes and now we're writing more code for no reason?

About your second post:
So should I then also do this?

in main
Gun* revolver = new Gun(35, 70, 6, 1.5f, 0.5f, mcCreeName, "Revolver");
mcCree.AddWeapon(&revolver);
mcCree.AddWeapon(revolver);

oh god I'm just about getting the hang of pointers and now I hear "unique_ptr" :D

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 5:05 am
by chili
in main
Gun* revolver = new Gun(35, 70, 6, 1.5f, 0.5f, mcCreeName, "Revolver");
mcCree.AddWeapon(&revolver);

I want you to take another close look at this and tell me why it is terrible :lol:

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 5:08 am
by Yumtard
Also, if I do this
charptrs.push_back( new Mcree( ... ) );
charptrs.push_back( new Datboi( ... ) );
charptrs.push_back( new DonaldTrump( ... ) );

I feel it will be very confusing when calling stuff like AddWeapon()
Because I'll have to keep track if charptrs[0] is mcCree or roadHog etc

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 5:10 am
by Yumtard
chili wrote:in main
Gun* revolver = new Gun(35, 70, 6, 1.5f, 0.5f, mcCreeName, "Revolver");
mcCree.AddWeapon(&revolver);

I want you to take another close look at this and tell me why it is terrible :lol:
Uhm I'm not sure what happened there. I think I just pasted from my code twice and just changed one of them :P it's supposed to be revolver not &revolver

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 5:30 am
by chili
Well the idea is, you write your code so that you don't need to know which character is at 0, 1, etc. That is the whole point of polymorphism. The caller doesn't need to know, it just calls baseptr->VirtualFunc() and the function does the right things.

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 5:55 am
by Yumtard
^ Yeah but I need to know which is which to know what arguments to send
ir AddWeapon(revolver) or AddWeapon(scrapGun)

Maybe I'm missunderstanding you and should look more into inhertiance and polymorphism

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 6:45 am
by chili
well that is only at the beginning you want to know what weapon to give to whom. and usually that stuff is handled by automatic loading from datafiles, not hardcoded anyways.

IF it makes you feel better you can do

auto pGood = new GoodGuy(...);
pGood->AddWeapon(new Revolver() );
auto pBad = new BadGuy(...);
pBad->AddWeapon(new Scrap() );

pushback(pGood);
pushback(pBad);

But anyways, you're in over your head here, and I'd say don't mess with it right now (at least not in an asssignment you're going to submit). For one thing, it's gonna add a lot of heap pointers that you will likely miss cleaning up. You might as well wait for it to be covered in class, that IS what you're paying for there after all ;)

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 6:57 am
by Yumtard
Alright, interesting stuff!

Yeah prob best not to mess around with it now 5 minutes before deadline :p

Just wanna correct your last sentence. "That is what you're paying for" to "That is what you're getting paid for"
I live in Sweden so I get about a 350 dollar salary to study :P

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 1:00 pm
by Yumtard
Lecture today went briefly over some different naming conventions, the different ways we all implemented support for multiple weapons, strings and having self commenting code.

Teacher mentioned that it's preferred to pass char pointers than std::string references so I changed all the std::strings om my code to char*

He also mentioned that we should avoid storing references in our classes.

After the lecture I asked for some feedback on my code. He took a quick look at it and seemed to think I've done a pretty good job (he didn't look much in detail tho). He told me I could use some prefix/sufix for member variables and parameters.
He also wanted me to split up static data in classes by making structs for those.

So I changed all my member variables to mHealth instead of health and health_in etc for all parameters.
Made structs WeaponData and CharacterData and pass those as pointers to the Character and weapon constructors. I don't like the way main gets clogged up with a bunch of:
roadHogData.health = 600; but oh well.

dynomite branch
https://github.com/Yumtard/CombatSimula ... e/dynomite

On monday we'll be doing some dynamic memory allocation and linked lists.
Giving my self some homework over the weekend which will be to watch episode 6 of intermediate and doing the homework and reading the chapter in my book about linked lists.
Will probably watch more episodes than that but need to hang out a bit with mrs Yumtard and Yumtard jr too :)

Re: Noob learns to code in 3 months

Posted: September 8th, 2017, 1:40 pm
by chili
Yumtard wrote:Teacher mentioned that it's preferred to pass char pointers than std::string references so I changed all the std::strings om my code to char*
This is actually kind of worrying... :?

https://stackoverflow.com/a/23383614/3773305