Advertisement

fundamentals

Started by October 11, 2024 07:03 PM
4 comments, last by JoeJ 1 month ago

I haver decided to read a book on c++ and do the exercises in the back of the chapters. Hopefully this will help me learn the fundamentals of c++ programming

Doing exercises is always a good idea.

However, I think you need exercises not so much in the fundamentals, but more in how to scale to larger and maintainable programs. Unfortunately, I don't know any book or exercises about that.

Hopefully someone else has some ideas about that.

Advertisement

Alberth said:
However, I think you need exercises not so much in the fundamentals, but more in how to scale to larger and maintainable programs.

Nah, imo it is about fundamentals. If somebody has never used multiple instances of a struct or class, that's a very fundamental issue.

A good exercise would be a program to enter and store multiple persons and their phone number. Then we can enter a name and the program does a search and spits out the number.
Initially it would be good enough to hardcode all persons in code, using an array or std::vector of persons. This already addresses the fundamental issue.
The next steps would be a feature to enter new persons at runtime, and to save / load the data to disk. This would teach towards making larger programs.

Thus i would say that for fundamentals C is be better suited than C++.
This is because a C++ class would use multiple instances for multiple persons, but it would use only one class method to compare a person with a given name to seek. This can be a minor source of confusion eventually: The function is not duplicated with the data.

With C we will use one global function taking a pointer to a given instance, so it is more clear that we need the code only once even for multiple instances of data.

That's why i would recommend to restrict yourself to C language features when the goal is to learn fundamentals, but then extending to C++ when it comes to larger programs.
But well, C is old school and maybe that's just me. C++ is fine too. Just make sure to understand that no matter if we use global C functions or C++ class methods - the final instructions for the (not inlined) function exist only once, no matter how many instances of data we have.

I don't think that's so hard to learn, but yes - you need to find the right exercises. I guess the phone numbers program is common and maybe you can search for that to find the right book or tutorial.

I am going to stick with c++ because I want to learn classes and object-oriented programming skills

pbivens67 said:
I am going to stick with c++ because I want to learn classes and object-oriented programming skills

Once you can write the phone book program without any help, OOP concepts such as composition or abstraction become useful.
But if you can't yet, OOP concepts wont help you to get there. And chances are that a OOP book does not teach you those missing fundamentals, since the writer may assume you already can.

In the worst case, this happens:

Exercise: Write a PacMan game.
Your solution, now using some fancy OOP:

class PacMan
{
	int x,y;
	public:
	void Update(); 
};

class Ghost
{
	int x,y;
	public:
	Paint();
};

class Blinky : public Ghost
{
	public:
	void Update();
};
class Pinky: public Ghost
{
	public:
	void Update();
};
class Inky: public Ghost
{
	public:
	void Update();
};
class Clyde : public Ghost
{
	public:
	voud Update();
};

void main ()
{
	PacMan player;
	Blinky blinky;
	Pinky pinky:
	Inky inky;
	Clude clyde;
	
	while (true)
	{
		player.Update();
		blinky.Update();
		pinky.Update();
		inky.Update();
		clyde.Update();
		
		PaintBoard();
		PaintPlayer(player);
		blinky.Paint();
		pinky.Paint();
		inky.Paint();
		clyde.Paint();
	}
}

I'm sure that's what you would do after reading OOP books. ; )
But it still shows your fundamental problem.
If you target this problem specifically, your code might look something like that:

struct Sprite
{
	int x,y;
	void Paint ();
}

void MovePlayer (Sprite &player, int buttonDown);
void ChasePlayer (Sprite &ghost, Sprite &player);

void main()
{
	Sprite player;
	Sprite ghosts[4];
	
	while (true)
	{
		MovePlayer (player, pressedKey);
		for (int i=0; i<4; i++) ChasePlayer (ghosts[i], player);
		
		player.Paint();
		for (int i=0; i<4; i++) ghosts[i].Paint();
	}
}

That's much better.
But notice that using OOP, or not using it, has nothing to do with your fundamental problem and bad habit of duplicating code together with data.

So you may feel interested in learning advanced concepts such as OOP right now,
but likely it won't help you to overcome the fundemantal barrier which holds you back no matter what.
Even worse: The newly learned details and skills may only distract you from getting there. And because the base is faulty, the newly learned concepts might add more confusion than they help with getting better.

If you can identify a certain fundamental problem, you should target this problem first. Work on that problem, and only that problem, until you get it right.
Only after that you can proceed to learn more and new things you find interesting.
You can build a house only on a stable fundament. If the fundament is already faulty, the house will break down.

Your problem has nothing to do with OOP or C vs. C++. Keep that in mind. Focus on books and tutorials which show how to avoid your specific problem.

Unfortunately i can not give a name to your problem so you could just google for it, thus the proposal of a phone book tutorial or something similar. Seek for exercises which use multiple instances of the same struct or class.

If i was a guitar teacher, a similar situation would be:

Guy comes in, and he says: ‘Hi! Can you teach me how to play fast?’
Me: ‘Sure, play something…’
Guy plays a solo on his guitar.
Me: ‘Dude, you pick every note with a downstroke. Learn alternate picking. Alternating down and up strokes makes you twice as fast.’
Guy tries it out, but it feels unpleasant. He says: ‘Ugh, that sucks. Can't i just learn to do only down strokes, but faster?’
Me: ‘No.’

Guy leaves. He learns the harmonic minor scale over all the fretboard and in all keys. Because he finds music theory interesting, and he likes Yingwie Malmsteens music.
But he never learns alternate picking, so he never becomes fast. Finally he dies as a slow guitar player, unable to applicate the things he have learned to do the things he want.

Spend your time wisely and target your problems before your curiousity.

This topic is closed to new replies.

Advertisement