• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Learning C/C++ - Tutorial

OmfgRegister

Active Member
Joined
Mar 7, 2009
Messages
173
Reaction score
47
Since Fallen is done writing the turoials and I'm bored...

10. C++ - Subclassing abstract classes and re-defining virtual functions.

Lets take a class for example

Code:
class Orc
{
public:
	int hp, maxHp;
	int mana, maxMana;
	
	void fight()
	{
		cout << "Fighting!\n";
	}
	void run()
	{
		cout << "Running!\n";
	}
	void growl()
	{
		cout << "Growl\n";
	}
};

class Elf
{
	int hp, maxHp;
	int mana, maxMana;
	
	void fight()
	{
		cout << "Fighting!\n";
	}
	void run()
	{
		cout << "Running!\n";
	}
	void speakElven()
	{
		cout << "El grath\n";
	}

};


One of the big things in programming is to have as little duplicated code as possible (saves time)
Looking at these two classes they look pretty similar (almsot identical)

An abstract class is a class that others can inherit off of and use their functions and data


Code:
class Monster
{
public:
	int hp, maxHp;
	int mana, maxMana;
	
	void fight()
	{
		cout << "Fighting!\n";
	}
	void run()
	{
		cout << "Running!\n";
	}
};


class Orc : public Monster
{
public:
	void growl()
	{
		cout << "Growl\n";
	}
};

class Elf : public Monster
{
public:
	void speakElven()
	{
		cout << "El grath\n";
	}
};
the orc class and elf class now inherit all of the functions of monster
Examples of code
Code:
Elf legolas;
legolas.hp = 200;
legolas.fight();
As you can tell the elf class functions in the exact same way as before


Now lets say that an elf fights a little differently than a normal monster and needs it own fight function
this is where virtual functions come in
Code:
class Monster
{
public:
	int hp, maxHp;
	int mana, maxMana;
	
	// by specifying virtual, it allows the fight function to be overriden in subclasses
	virtual void fight()
	{
		cout << "Fighting!\n";
	}
	void run()
	{
		cout << "Running!\n";
	}
}


class Orc : public Monster
{
public:
	void growl()
	{
		cout << "Growl!\n";
	}
}

class Elf : public Monster
{
public:

	// overriden fight function from monster parent class
	void fight()
	{
		// do eleven fight
	}
	void speakElven()
	{
		cout << "El Grath!\n";
	}
}

now we can have an elf that call its own fight function instead of the default

Lets move onto to pure virtual functions
these are functions in a parent class that the sub classes must override

Example:
Code:
class Monster
{
	int hp,maxHp;
	int mana, maxMana;
	
	virtual void speak() = 0;
	virtual void fight()
	{
		cout << "fighting!\n";
	}
	void run()
	{
		cout << "Running\n";
	}
	
}
Notice the line
virtual void speak() = 0;
the = 0 means there is no implementation of the function
and since the class now has pure virtual functions you can no longer create a monster class
the following code is now illegal
Code:
Monster m;  // Error: cannot create an instance of an abstract class
m.run();

However you can do:
Code:
Monster *m = new Elf();
m.speak();


But remember the sub classes now must have a function speak() in them so our old elf and orc classes will no longer compile

Lets fix those..
Code:
class Orc : public Monster
{
public:
	void growl()
	{
		cout << "Growl!\n";
	}
	
	void speak()
	{
		growl();
	}
}

class Elf : public Monster
{
public:

	// overriden fight function from monster parent class
	void fight()
	{
		// do eleven fight
	}
	void speakElven()
	{
		cout << "El Grath!\n";
	}
	
	void speak()
	{
		speakElven();
	}
}


Polymorphism: feature that allows values of different data types to be handled using a uniform interface.
Code:
vector<Monster*> monstersOnMap;
for (int i =0; i < monstersOnMap.size(); i++)
{
	monstersOnMap[i]->speak();  // since I know that a monster has a speak function I can call it.. doens't matter if its an elf or an orc or whatever
}



















11. C++ - The meaning of a static variable/function.

Static means something doesn't change
And in some instances that there is only 1 of them

lets look at a class Button:
Code:
class Button
{
	Image buttonPicture; // picture is 50 kb
	
	std::string text;
	
public:
	void pressed();
	void setText(std::string text)();
	
};

And lets look at some imaginary code(this won't compile):
Code:
Form::Form() // the initlizer of a form class (Something that would have buttons)
{
	Button b1();
	Button b2();
	Button b3();
	Button b4();
	
	add(b1);
	add(b2);
	add(b3);
	add(b4);

}


so basicly i'm creating a form with 4 buttons
every button has its own memory
and its own buttonPicture
so the memory consumption is 200 kb just for the images (which every button has the same of)

If we rewrite the Button class using a static variable for buttonImage

Code:
class Button
{
	static Image buttonPicture; // picture is 50 kb
	
	std::string text;
	
public:
	void pressed();
	void setText(std::string text)();
	
};

It will now only have 1 instance of buttonPicture for the class
and thus save memory

lets look at mis-uses of static and why not to use it in some cases
Code:
class Monster{
public:
	static int hp;
	
};

Code:
	Monster m;
	m.hp = 100;
	
	Monster m2;
	m2.hp =200;
	
	cout << m.hp << endl; // prints out 200 since hp is static


Static functions are a little different.. they are functions that are called without an instance of the class
and can only use the classes static varaibles

Code:
class ABC
{
	static int x;
	int y;
	static void random()
	{
		cout << x << endl;
		cout << y; // Error because y is not static
	
	}
}

you can call static functions by:
Code:
ABC::random();
// an instance of the class may also call them

ABC a;
a.random();
 
Last edited:
OP
Fallen

Fallen

Freelancer
Senator
Joined
Aug 21, 2009
Messages
3,712
Reaction score
250
Location
Egypt
Updated first post with two logical operators, AND and OR. Later will be adding the rest.
 

jn0619

I am too Handsome
Joined
May 23, 2012
Messages
28
Reaction score
1
Location
Quezon City,Philippines
You could use "#define" so that you won't have to type the whole thing. :p

#define p printf
#define s scanf

Like this:

Code:
#include<stdio.h>
#define p printf

    main()
   {
	p("Hello World!\n");

	getch();
}

-JannoV
 
Last edited:

pasiak12

Well-Known Member
Joined
Jun 7, 2009
Messages
259
Solutions
13
Reaction score
69
15. A Brief idea of how OT code work (we'll be using TFS). - does anyone know where can I find any informations about this?
 

StreamSide

Joseluis Gonzalez
Support Team
Joined
Aug 31, 2007
Messages
3,482
Solutions
45
Reaction score
1,052
Location
Arica - Chile
there are lots of guides and books about it, but the very best option to learn -imo- is to reproduce errors on the thing you working on (tfs on this case), try to understand why the developers add that or why they change constants to interprets, uint to uint32, for to auto, etc. and done.
then you need to work on it ofc.
 

drakylucas

Well-Known Member
Joined
Dec 15, 2015
Messages
162
Solutions
1
Reaction score
67
Can someone do a next part talking about threads? Also, thank everyone!! I learned a lot, more than at college.
What I want to know about threads:
How to initialize them
The correct way to use them
The wrong way to use them
How to lock / unlock threads
Why does deadlock happens and how to avoid them?

In Java it's pretty easy, since they have "synchronized" argument, but I don't see it in C/C++, I've read about mutex but I haven't understand very well.

Thanks !!!!
 
Top