Spunky's C++ Tutorial

King of the Hello Kitty Fanclub
💻 Oldtimer
Joined
Sep 6, 2004
Messages
1,675
Best answers
0
Location
Australia
This thread has slightly inspired me to write a similar basic tutorial for Java, Java's open sourced-ness makes it a bit easier to pick up than having to fork out cash for Visual Studio (or use dodgy compilers). Would people be keen on something like that? It'd help me revise my stuff if I can write a tutorial on it.
 
Freelance Mappzor
✔️ HL Verified
🚂 Steam Linked
💻 Oldtimer
Joined
Nov 21, 2003
Messages
17,065
Best answers
0
Location
Stairing at the Abyss
AS long as you mention that the whole basis for Java is actually C++ from what Java was created.

Strange how they never mention that in public. Its only written in the dark corners of their site ^^
 
King of the Hello Kitty Fanclub
💻 Oldtimer
Joined
Sep 6, 2004
Messages
1,675
Best answers
0
Location
Australia
If I did do it, it wouldn't say that I don't think :p It'd just show the syntax and how to use the language and the environment and mention some the extensive libraries of code.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Nov 6, 2004
Messages
3,055
Best answers
0
Location
Round Rock, TX
*UPDATE*

Added a program demonstrating the sizeof function in Chapter III. We use it to find the amount of memory each data type occupies on the local machine. This varies from system to system. It's always good to know how much of a resource you have. In this case, our precious resource is memory.
 
Freelance Mappzor
✔️ HL Verified
🚂 Steam Linked
💻 Oldtimer
Joined
Nov 21, 2003
Messages
17,065
Best answers
0
Location
Stairing at the Abyss
I suggest you allso throw malloc in there. Since its kinda on the same subject.
 
Freelance Mappzor
✔️ HL Verified
🚂 Steam Linked
💻 Oldtimer
Joined
Nov 21, 2003
Messages
17,065
Best answers
0
Location
Stairing at the Abyss
malloc is C. The C++ way would be to use new, or, whenever possible, to use stl.
Heh im actually just reading on the STL.

But i must confess i did use malloc quite a few times with C++ aswell ^^
 
Member
✔️ HL Verified
Discord Member
Joined
Sep 27, 2005
Messages
61
Best answers
0
Thread seems a little dusty, so I might add a thing or 2 to this thread.
Haven't posted in here in years so lets talk a basics again.

I personally use Visual Studio Professional 2005, makes it easier because it already has the Windows SDK built in. If you want to, you can always get the Visual Studio Express Edition free from Micro$oft and download the Windows SDK separate and have them link, but thats another discussion xD..

Well, I guess I'll talk about loop functions.

<Just as a sidenote, if you're using Visual Studio, all my examples are going to be done in the 'Win32 Console Application' template in mind>

Today I'll do Spunky a favor and talk about an easy loop function called the for/next loop. This is a basic conditional looping function. I'm going to assume that you have a basic grasp of variables and what they are, for instance the int, double, string(if you have it included in the header), and others out there.

For/Next Loops are mainly used as a counter loop, so, for example, if you have a user put in number to signify how many times they lick a lollipop, it should look something like this.

Code:
// Example For/Next Loop
// Your Name Here <----

#include "stdafx.h"
#include <iostream> //Your basic user calls

using namespace std; //A lazy way to include all the methods above


int main()
{
	int licks;
	cout << "How many times would you like to lick the lolipop? ";
	cin >> licks;

	for (int i = 0; i < licks; i++)
	{
		cout << "Lick " << i+1 << ": Yum" << endl;
	}

	return 0;
}
If you're lost, thats fine, we'll go from the top. This is actually fairly simple once you see whats going on. Ignore the header for now and lets just look into main().
The first line you see is:
int licks;

Basically what is happening there is that I'm making a variable of type "int", which of course stands for integer, named licks. Make sure you end your statements with a ';', I don't know how many errors I've gotten from that dumb mistake.


The next line after that you see:
cout << "How many times would you like to lick the lolipop? ";

The "cout" is a method of the header <iostream> that basically prints, if you will, a message out to the console.
The "<<" operator is the line insertion operator. Basically what it will do for you is add to a message in this case. So when you do:
"cout <<" you are adding the capability to display something to the console.
In this case I'm adding "How many times would you like to lick the lollipop? ". You can have that say whatever you want, I always put an extra space at the end of it so it looks nice when you see it in the console, and as always, end with a ';'.


The next line as follows is:
cin >> licks;

The "cin" is also a method of the <iostream> class that allows you, in this case, to take the user input and place it into a variable.
The ">>" is the extraction operator which allows you to take that value and plop it into a variable, which in this case is licks.


Yay, and the next line you've all been waiting for is:
for (int i = 0; i < licks; i++)

What the hell is going on there you say? Lets go from the top.
The "for" commands asks for 3 things for you to pass, a variable that you're using, a comparison event, and an aftermath event. So basically what you're seeing is: for( variable ; event of sort ; aftermath ).
Specifically lets use the said line and break it down.
int i = 0. I'm having the for loop create an integer of variable name "i" and giving it a value of zero. The variable must be initialized to be used in a loop statement sometime before it occurs if it is not assigned a value already.
In the above statement, "int licks;" I did not initialize it, therefore it would have given me an error message IF I had not assigned it a value since it's creation.
OK on to the next portion, the event of sort.
I'm declaring that as long as the variable I created "i" is less than "<" my variable of licks (which has the value from the "cin"). As long as this is true we can do our loop.
And to the end, we have the aftermath. In order for this to be a good counter loop, "i" needs to at some point meet my event's needs.
I have "i++" declared at the end of it. The ++ basically means to add a value of 1 to my variable in question, in this case "i". This also holds true with the subtraction sign "--".
If this is not thorough enough, please comment this with a question, I'll see if I can give you a better response than I'm showing here.


After that we have this segment going on:
{
cout << "Lick " << i+1 << ": Yum" << endl;
}

When you are making a loop: for/next, do/do while statement, you need to encompass your statements in the brackets {,} and it needs to start with the { first.
After that we see our response entangled between our brackets. You can have it do whatever your hearts desire in between these, I simply had it display the message
Lick (your current number + 1 because it starts at 0 and it looks bad as lick 0) and then Yum.
The endl; at the end is basically an easy way to tell it to end the line and start the next.


OK, so at this point, if you compile the said code and run it, it should ask you to put a value in for the licks.

The output should look something like this if your value is 3.

Code:
How many times would you like to lick the lollipop? 3
Lick 1: Yum
Lick 2: Yum
Lick 3: Yum
Press any key to continue  .  .  .

And yay! There you have it, if you have more questions please ask, I probably should have covered if statements before I did this, but I'll see if I can do a rewind for you guys later. Until then, happy coding, again if you have questions ask.
 
Last edited:

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Good post swobj. Once you get up to if statements and loops is when the learning gets truly interesting. You start to see how it's possible to make simple programs that branch.

Anyway, seeing as how no ones mentioned it yet, I'd like to throw this site out there as a resource. I'm reading through about 4 online tutorials right now and that one is my favorite (Although it is a bit fuzzy in some areas). With that being said, books are infinitely better than anything you'll find online.
 
Member
✔️ HL Verified
Discord Member
Joined
Sep 27, 2005
Messages
61
Best answers
0
Good post swobj. Once you get up to if statements and loops is when the learning gets truly interesting. You start to see how it's possible to make simple programs that branch.

Anyway, seeing as how no ones mentioned it yet, I'd like to throw this site out there as a resource. I'm reading through about 4 online tutorials right now and that one is my favorite (Although it is a bit fuzzy in some areas). With that being said, books are infinitely better than anything you'll find online.
I absolutely agree with you there. Professor's definitely help a load too haha.. The shear tolerance and the step-by-step help really gets it stuck in the brain. I'll probably grab myself some time later and write an if statement section. I hope this helps someone out there xD.
 
Freelance Mappzor
✔️ HL Verified
🚂 Steam Linked
💻 Oldtimer
Joined
Nov 21, 2003
Messages
17,065
Best answers
0
Location
Stairing at the Abyss
Just one remark swboj.

To avoind some weird situations and strange errors that people tend to get when they forget to initialise a variable.

Allways give a variable a value when you make it. Since it can get quite messy once you have a lot of them and you can end up with weird values.

So allways put a = 0 at the end if its a number for a string.
Meaning do this -> int licks = 0;

Since if you dont set it to anything C++ just uses whats currently at that memory location. And while mostly when you just make a new program it really is a 0, but when you have bigger programs with multipla variables that you add and remove it can happen that you get a value thats different from 0.

Same thing can happen with strings or characters. So allways remember to give a varable an actuall value. Though stuff like JAVA will stop you from making such blunders C++ wont ^^
 
Member
✔️ HL Verified
Discord Member
Joined
Sep 27, 2005
Messages
61
Best answers
0
Just one remark swboj.

To avoind some weird situations and strange errors that people tend to get when they forget to initialise a variable.

Allways give a variable a value when you make it. Since it can get quite messy once you have a lot of them and you can end up with weird values.

So allways put a = 0 at the end if its a number for a string.
Meaning do this -> int licks = 0;

Since if you dont set it to anything C++ just uses whats currently at that memory location. And while mostly when you just make a new program it really is a 0, but when you have bigger programs with multipla variables that you add and remove it can happen that you get a value thats different from 0.

Same thing can happen with strings or characters. So allways remember to give a varable an actuall value. Though stuff like JAVA will stop you from making such blunders C++ wont ^^
Definitely, I did that on purpose to stress the importance of initializing a variable, showing that you don't have to, but its possible to do it and have it set at a later time. In that particular situation its actually fine because the "licks" variable is going to be inputted a value from the user before anything else happens. Now if it was happening elsewhere I definitely would have initialized it upon creation. Good point though =).

Edit:
On another note, when you're dynamically creating and deleting variables, its also good practice to set the variable to NULL so the memory location is completely wiped out, stopping a lot of the leaks from happening.
 
Last edited:
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Nov 6, 2004
Messages
3,055
Best answers
0
Location
Round Rock, TX
http://msdn.microsoft.com/en-us/beginner/cc305129.aspx

I don't think I could explain it any better than that. Herb Schildt was on the ANSI/ISO committee that pretty much molded C++ into its present state in 1998. I don't think there's a better source to learn from. However, I'll leave this thread open for discussion, and if anyone has any questions, I'll still happily answer them.

[video=youtube;CObg3tbT2lg]http://www.youtube.com/watch?v=CObg3tbT2lg[/video]

I uploaded this video two years ago for this tutorial, but never got up to pointers in the lessons.
 
Freelance Mappzor
✔️ HL Verified
🚂 Steam Linked
💻 Oldtimer
Joined
Nov 21, 2003
Messages
17,065
Best answers
0
Location
Stairing at the Abyss
Hehe so now you show that vid XD

Told you it was great XD
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Nov 6, 2004
Messages
3,055
Best answers
0
Location
Round Rock, TX
Haha, I couldn't remember who sent me that video, but yeah, it's great.
 

Users who are viewing this thread

Top Bottom