Noob learns to code in 3 months

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 8th, 2017, 4:57 am

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

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » September 8th, 2017, 5:05 am

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:
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 8th, 2017, 5:08 am

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

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 8th, 2017, 5:10 am

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

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » September 8th, 2017, 5:30 am

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

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 8th, 2017, 5:55 am

^ 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

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » September 8th, 2017, 6:45 am

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 ;)
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 8th, 2017, 6:57 am

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

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » September 8th, 2017, 1:00 pm

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 :)

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » September 8th, 2017, 1:40 pm

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
Chili

Post Reply