Why did I make this thread?
• Simple answer, I'm tired of seeing threads regarding what the hell C++ is, where do I download it, and what it does. You don't like it, don't read it. But it's for people who actually care, and I'm summarizing basically a whole chapter or two, about history, and a few things.
C++
What is it?
• C++ is a programming language in which computers communicate in, kinda like how you and me talk in English, and usually there's the odd one out that speaks
retard. But anyways, it's a language used widely over the world by programmers, much more than any other language. Kinda like how ENGLISH, is called the basic language. C++ developed off C, so that's why people call it a super-set/expansion of C.
What you can do in C, you
CAN do in C++, but there's things in C++, which you can't do in C.
Example would be, the differences of C and C++.
C uses printf to display output,
C++ uses cout.
So why learn it?
• I'm sure that there's a quarter of people that learn C++ just so they can make hacks for a game, and no different than the average choob, I was the same. But now, I wanna learn it because I have an interest in it. Honestly, if that's the reason you came in, then ask yourself. What the hell are you gonna do once you do learn how to make 'em? Hack? Well good going, you wasted a large quantity of your time learning how to hack a 10 year old's game. Seriously, if you're going to learn it, then either find a better reason, or try it out, and see if you actually understand it, and have an interest in it.
ENOUGH, about that. Anyways, let's start. I'd love to go in detail about history of C++, but I know that half of you won't give a damn, so I'm gonna summarize it here, with some easy examples to follow.
C++ consists of four different things, that are included in Object-Oriented Programming(OOP) which is one of the new features in
C++ that you
C doesn't have.
1 & 2. Encapsulation & Data hiding.
Encapsulation: Binds Code and Data. -
MSDN
Data Hiding: Simple as that, hides data from the outside, where it's not supposed to be used.
Noob example:
Your Immune system
KNOWS how to fight off the infection, but you don't care how it
DOES IT, as long as it does. You don't care how your immune system works, as long as you know it works. Basically, as long as your Killer T cells, and Killer B cells fight off infections with whatever chemicals are needed, you don't care, you're happy. So what just happened? Your body fought off an infection without
YOU knowing it, and it used both your brain and immune system, which allow this to take place.
Oh btw, welcome to
Bio 101.
"Users of a well-defined class do not need to know how the class works; they just need to know how to use it." - Teach Yourself C++ in 21 Days, Second Edition.
3. Inheritance and Reuse.
Inheritance and Reuse:
"A new type, which is an extension of an existing type, can be declared. This new subclass is said to derive from the existing type and is sometimes called a derived type." -
Teach Yourself C++ in 21 Days, Second Edition.
A subclass is formed from the original class.
Noob example:
Basically, your given a vaccine, and your immune system fights it off, and stores it in it's memory. Now, somehow you get the disease itself, and your immune system has to decide. Does it want to fight it over again, or just use the memory cells it has stored?
Instead, it says, fighting it over again would be too much of a hassle, as where I use the memory cells, I might have an adantage, and whatever I didn't pick up the last time, I'll get it for sure this time.
U c wut it did thar? It basically used the information it had gathered the last time, and re-used it.
3. Polymorphism.
Polymorphism:
Poly: Many. Such as Polygamy: Many wives, Monogamy:One wife, get it?
Good example of a Polygamist, is Tiger Woods. If you don't listen to the news, then GTFO. He had one wife, but f***ed with other girls.
Morph: Manipulation, aka forms.
Aka clay, you can manipulate it to whatever form you wish, and there's mroe than one way to mold it. You should know this man, you playd with Playdoh as a kid, don't even lie.
Ism: Is+m.
You
IS a
Moron. Kay, I admit that one sucked, but it's just the ending given to 2 words combined. Kind of how like Rape, turns into Rapist. Y'know?
Poly+Morph+ism=
'Poly'morph'ism'.
So, if you still haven't figured it out by now, it means
many forms.
Polymorphism: Basically, meaning that however you write your own code, or however you do things, you should get the same result as another person. Now I don't mean, you write a Program that says "Hello". While the other person writes a program that "adds" two numbers. What I mean is that, if you both had the same idea, you can write your codes differently, do what you want differently, but the end result should be the same. Aka,
whatever you do, in the end, you get what you want, and you're happy.
Noob example:
The new cell might fight off it's infection by releasing an acid, while the other might have used just simple chemicals. You don't care as long as the infection is cured. Polymorphism means many forms, aka many ways in Bio.
Now, that you know what the differences and new features are, let's start with
Classes. I won't go into much detail but I'll show you a quick example.
[php]
class human{ // Humans are a class.
public: or private:// The class can be either public, of private. Hence encapsulation.
person aj // Individuals are the objects of the class.
int height; // These are variables of the human class.
int weight; // Variable.
sleep(); // This would be a function of the human class.
eat(); // As well as this.
/* Example by zeco */
}
[/php]
So what is a class?
• MSDN says that "A set of plans that specify how to build an object."
Kind of like your body, isn't it?
Your body decides how it wants to fight off an infection, or even as simple as, your body allowing breathing. Breathing breaks down into organs, which breaks down to amount of oxygen, and carbon dioxide.
[php]
class Breathing{
public:
breathing lungs;
double amount_of_oxygen;
double amount_of_carbon dioxide;
exhaleCO2();
inhale O2();
}
[/php]
Now, some of the 'pros' might say I haven't declared the function type clearly and whatnot, but let me ask you this. Did you learn functions in the very first chapter? Well, you probably did if you were
zeco, but I will go over functions as it goes on, but for now. I want it to be simple, it's an example. :3
Public or Private.
It's up to you whether you wanna make the class Private or Public, each have their own advantages, but disadvantages as well.
Public:
Anything outside the class has access to it, and could change an important variable from a random function that's got nothing to do with the class itself.
"Meaning if it's public, it could lead to havoc. If the variable is compromised by another function." - zeco.
So let's say you like this girl, and you tell your best friend. Well, your best friend turns out to be a hoe, and tells everyone else. Now everyone knows, and rumors get changed around and whatnot, and it went from you liking a girl, to you liking a guy. See how that lead to havoc? Yeah, don't ever trust your best friend again.
Private:
The variable can only be accessed inside the class itself, and can't be changed by any other part of the code.
So, let's say you like this other girl. Only you know about this girl, no one else does. So there, it's a private matter. Simple as that.
You don't need to know much about Classes in the beginning, but it'll only make your life easier later on.
So, your first program, eh? Most books explain this fairly well, and there has been tutorials by
BxR and
That0n3Guy, but I'll do it for the hell of it.
[php]
// This is a comment, it isn't included in the code, so you're free to write
//what you want. This type of comment lasts for just 1 line. // = Comment.
/* This is also a comment. But this can go on
for as long
as you want, until you put the */.
// Basically, Starts with /*, and ends with */. Use comments often!
#include <iostream>
/* # is a signal the preprocesser. Like a nerve impulse to your brain.
These are read before anything else in your code, to start the preprocesser.
Like how your brain, warms you up before you do an activity.
include is command that tells the preprocesser to include the following file.
Another example is
#define IQ 25. Include & Define are directives, aka commands.
Which tell the preprocesser what to do. So in this case the file Input-Output-Stream is included,
without it you would not be allowed to use cout(Console Output).
I'd love to go in detail more, but I think it's enough. */
using namespace std;
/*using is simple as that, using. Like a command.
Namespaces are basically Classes, objects, and functions under one name.
In this case it's std, which stands for standard.
Meaning using the standard library of C. No more detail */
int main()
/* First of all, I'm gonna assume you have idea what this means.
So let's start with the word int. Int-> Integer.
It's the type of data to be returned after the program is completed.
main() is to be included to start your block of code,
which can consist of anything you want, functions, variables, whatever you wish.
It's a must. */
{
/* This is really simple, it basically states that oh, the main function starts here.
It's the opening brace. Done. */
cout <<"I love men";
/* Okay, remember console output? Aka cout. This states the output,
meaning it's do be displayed on the computer screen.
<< is an operator which is used by cout. cin, which stands for Console Input, uses
the operator >>. Opposites of each other, k? Inside the " ",
is what is to be displayed on the screen.
Will discuss further later on */
cin.get();
/* Most books/tutorials don't include this, but without it the program would close instantly.
cin, remember what it stands for? Console-Input.
The computer waits for you to press an additional key, before it closes the program.
Allows you to look at your wonderful piece of art*/
return 0;
/* All programs that do not give errors are return a 0.
Only time it does not return 0 is when it has an error.
This basically means, that at the end, terminate the main function and shut down the program. */
}
/* This is the closing brace, meaning the function is done,
and all the code is between the opening and closing brace. */
[/php]
Without the notes, here's the program once more.
Code:
#include <iostream>
using namespace std;
int main()
{
cout <<"Hello World!";
cin.get();
return 0;
}
One important character in the C++ language, and every person in the world would fail English without it(not like you already aren't), but oh well. It is
the.....NEWLINE CHARACTER, YEAH!
In C++, it can be written two different ways, whichever you find works for you.
1. \n - This has to be put in apostrophes like this. "\n"
2. endl - While this, can be just written like so. << endl
Get what I mean? Yeah, I doubt it so I'll show you an example.
So, whichever one you use, you get the same results? Polymorphism, omgosh? :O. No noob, not yet. ;(
Using
\n.
[php]
#include <iostream>
using namespace std;
int main()
{
cout<<"Hi little boy.\n";
/* See? It's within the apostrophes, and guess what? It won't be included in your code, but it will make a newline. Try compiling this. */
cout <<"What's your name?

" << endl;
/* As opposed to \n, this is written with the output operator, but does not require apostrophes, just that.

*/
cin.get();
return 0;
}
[/php]
Example of both.
IMPORTANT NOTE: CAPITALIZATION DOES MATTER IN C++.
Try writing COUT, or cOuT, and tell me what happens.
Variables
There are different types of variables to be declared in C++, as you've probably already seen one.
int which stands for Integer. Integer can only store whole numbers, such as, 1,2,3,4,5. But it can not store decimal/fractional values such as, 2.56.
However, the variable that CAN store decimals is called
float.
There are 9 variable types, and each of them store a different amount of values, as well as, occupy different amount of space.
I'll group them together so it's easier to understand.
Integers
There are 3 for this category, occupy different sizes, and different ranges.
short int - Occupies 2 bytes, and ranges from -32768 to 32767.
Meaning, it can only hold numbers between -32768 to 32767.
Don't believe me? Go ahead and try this code.
[php]
#include <iostream>
using namespace std;
int main()
{
short int a = 32769; // The value is more than 32767 by 2.
cout << a;
cin.get();
return 0;
}
[/php]
So, what'd you get? Cause I got -32767. Exactly, it can't hold anymore than the maximum. So, if you wanted it to hold 32769, you would have to use Integer. Short int, well as you've seen is written as short int. Not SHORT INT, or SHORT int, or short INT. But exactly short int.
Integer
int - Occupies 4 bytes, and ranges between -2147483648 to 2147483647.
Like you've seen, int is written as int. Not INT, or iNt, but int.
Long Integer(Long e-Penor)
long - Occupies the same space as int, which is 4 bytes, and has the same range. -2147483648 to 2147483647. Seem familiar? So what's the difference you might ask? Different operating systems, different bits of computer, such as 32-bit, or 64-bit.
int basically adjusts to the architecture of your computer. For instance, if you're running 16 bit, it will 16 bits long(not the type), if it's 32-bit, then it'll be 32 bits long. Meaning it will adjust.
As opposed to
long, long will always be 32 bits, no matter what you might be running, even if your computer is 64-bit.
Thanks to
zeco for helping me out with that bit.
Floats(aka decimals).
Functions
Two types of functions, either they return a value, or return nothing at all. Functions that involve numbers, usually return a value, and functions used to just display characters don't. Think about it, if a function was to add two numbers it would return the sum, but let's say there was a function that just asked, "Hello, how're you?" well. What do you think it's gonna return other than just that line, nothing else.
Example of function with a return value:
[php]
// Example by Davidm44
int add(int x,int y)
{
return x+y;
}
/* Notice anything? See how the function has an int type, so what's that mean?
It means it's returning a integer value. */
int main()
{
int result = add(10,5);
cout << result;
cin.get();
}
[/php]
There's two things you should notice right away, the function type, which is an int. And the "+" sign, which is called an
Operator in C++. Yes, C++ is math. No, but really. It's like math.
Some common operators in C++ you'll probably use are:
+ = Addition.
- = Subtract.
* = Multiply.
/ = Divide.
Now, the equal sign is used in more than one way. You can use it to declare variables, such as int a = 10. Or, to say a number is equal to another number. For this, you basically would write it as.
Code:
10 == 10
// This means that, 10 is equal to 10.
//While this,
10 = 10
// Will screw you over. :D
Okay, here is an example of a function, that has no return value, but just prints a statement.
[php]
#include <iostream>
using namespace std;
void turtle()
{
cout << "I love turtles!";
}
/* What's the function type? Void, right? Now, what does a void function do?
It simple means that the function does not return a value. Surprised?
you shouldn't be if you were listening. Two types of functions, remember?
So basically, all the turtle() function does is, display "I love turtles after displaying "Sup". */
int main ()
{
cout <<"Sup?\n"
<< endl;
turtle();
cin.get();
return 0;
}
[/php]
I will add the rest of Data Types tommorrow, due to me being a retard and eating Serrano peppers. Oh and, drinking milk doesn't help btw. Mythbusters lied to you.
If you see a mistake, and you correct it, I will give you credit for it. >_> I'm no C+P'er, I don't need to see to pee.
References Used: