Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

C++ Beginners Tutorial Part I

Chapter I - Intro to Programming


Intro:
I'm making this because I'm on my 4th semester of C++ at my university. So, I figured I'd teach some of you the basics of what I know. This will be extremely in-depth and will feature quite a bit of C++ terminology.

What is C++?
It is based of the C language and offers object-oriented features not found in C.

Reasons C++ is useful
C++ is popular because of it's low-level mixtures obtained from C as well as it's high-level features. C++ can also be written on one type of computer and then ran on many other systems. (It will need to be recompiled, but it's essentially the same)

How it works
After you have written a C++ code, it enters the process of converting it to machine language. It begins by going through a preprocessor which reads the code. (This is where special lines of code are used, I.E. #include, #windows.h, etc.) After the code is modified, the compiler goes through the preprocessed code and translates it into machine language. (This is where syntax errors, wrongful use of operators and other problems arise). If everything checks out okay, the compiler stores the machine code, otherwise known as object code, into an object file.


Visual Representation

[img]http://i.minus.com/jCUS88uvUY62Z.png[/img]


Writing Your First Source Code


Since this is the beginning of C++, we will start out with simple C++ console code.


[img]http://i.minus.com/ivE0UNrPp2Ex0.png[/img]


Although the source is valid and produces no errors, notice how it looks terrible and hard to read? Well this is where we each have our own style of writing. For every person, the style of writing differs. However, it doesn't really matter how you write it unless you're doing a job in which other people will need to see and read your source code.

Below is an example of how I would write this same code.

[img]http://i.minus.com/juYBn4ZcTOYhU.png[/img]


When writing C++, spacing does not matter. The compiler basically ignores any white space unless quoted.

Now, to make the output look a bit more presentable, let's play with the code we've already written.

[img]http://i.minus.com/iN6ZdxoAZJhHA.png[/img]



Don't like reading? Watch the video below!!

Oh and I'm sick at the moment, so excuse whatever mistakes I make/my voice.






The next tutorial can be found here.
Skype: ryand.8

[Image: XbaG5gl.gif]

This is a very nice tutorial for people just starting out on C++. Keep up the good work :)
Very nice tutorial.

However two things:

1) You shouldn't be using void main() it should be int main() as it returns an interger value.

2) You should have finished your code with return 0;
(Dec 31, 2011 12:45 PM)Han. Wrote:  Very nice tutorial.

However two things:

1) You shouldn't be using void main() it should be int main() as it returns an interger value.

2) You should have finished your code with return 0;

1) I'm not returning a value therefore void will work just fine.
2) Because I use void, I don't have to have a return value, so return 0; is unnecessary and would cause errors. I also save like 2 bits of memory which is EXTREMELY important.
3) If you read through my tutorial, I explain why void is okay to use while int isn't necessary for these purposes.

Skype: ryand.8

[Image: XbaG5gl.gif]

Someone in their 4th semester of C++ shouldn't be making these rookie mistakes...Main returns an integer.

http://en.wikipedia.org/wiki/Main_functi...nd_C.2B.2B

Wow I had to check to be sure...This is your fourth semester in C++ in university? I thought with these rookie mistakes it would be your four semester in C++ junior high school.
(Dec 31, 2011 12:55 PM)ryan⚜ Wrote:  1) I'm not returning a value therefore void will work just fine.
2) Because I use void, I don't have to have a return value, so return 0; is unnecessary and would cause errors. I also save like 2 bits of memory which is EXTREMELY important.
3) If you read through my tutorial, I explain why void is okay to use while int isn't necessary for these purposes.

If I recall right the ISO standard for C++ states that typing the words "return 0" is optional, but I think the standard is to still use int for main. In fact I'm pretty sure they threaten to come after your family if you do not use an integer return type for main.

by the way, nice tutorial, stick with it.
(Dec 31, 2011 02:04 PM)g4143 Wrote:  Someone in their 4th semester of C++ shouldn't be making these rookie mistakes...Main returns an integer.

http://en.wikipedia.org/wiki/Main_functi...nd_C.2B.2B

Wow I had to check to be sure...This is your fourth semester in C++ in university? I thought with these rookie mistakes it would be your four semester in C++ junior high school.

I don't understand why you come on my thread and bash it. I can see you've posted an AMAZING tutorial with NO mistakes. Even so, the use of void isn't a mistake. Like I said before, the function I used doesn't return a value. So rather than waste my time and put...

int main()
{
cout << endl;
return 0;
}

I can just do...

void main()
{
cout << endl;
}

You say the function main always has a value? Then why is it that my code worked just fine with no errors?

I'm not an idiot and I know that using void in C++ is frowned upon, but it's called an into to programming meaning it encompasses all aspects that I think are necessary for MY thread. Please get out and don't post unless it's something useful.

(Dec 31, 2011 02:18 PM)Blackouti386 Wrote:  If I recall right the ISO standard for C++ states that typing the words "return 0" is optional, but I think the standard is to still use int for main. In fact I'm pretty sure they threaten to come after your family if you do not use an integer return type for main.

by the way, nice tutorial, stick with it.

Yeah, this was the only time I was going to use void. It was just to prove a point. If you listen to my video, I talk a bit about it. If I left out the fact returning a value was optional, then it wouldn't really be a full understanding of C++ basics. Thanks for your input though.
Skype: ryand.8

[Image: XbaG5gl.gif]

(Dec 31, 2011 02:38 PM)ryan⚜ Wrote:  You say the function main always has a value? Then why is it that my code worked just fine with no errors?

I'm not an idiot and I know that using void in C++ is frowned upon, but it's called an into to programming meaning it encompasses all aspects that I think are necessary for MY thread. Please get out and don't post unless it's something useful.

To address your first point. Try compiling using ISO compliant settings on your compiler. Trust me, you'll get warnings and errors.

Your second and further points. I'm not sure if your an idiot but I do know your encouraging non-standard practices which is bad. If your going to put forth the effort to learn C++(or teach it) then do it correctly to promote a consistent language.

(Dec 31, 2011 02:59 PM)g4143 Wrote:  To address your first point. Try compiling using ISO compliant settings on your compiler. Trust me, you'll get warnings and errors.

Your second and further points. I'm not sure if your an idiot but I do know your encouraging non-standard practices which is bad. If your going to put forth the effort to learn C++(or teach it) then do it correctly to promote a consistency.

So, rather than say that to begin with you start by insulting me and basically tell me I'm stupid?
Skype: ryand.8

[Image: XbaG5gl.gif]

(Dec 31, 2011 03:00 PM)ryan⚜ Wrote:  So, rather than say that to begin with you start by insulting me and basically tell me I'm stupid?

No I never stated anything of the sort. I just noted that you should teach a consistent language and stay away from language extension that are only available on certain compilers.
(Dec 31, 2011 03:08 PM)g4143 Wrote:  No I never stated anything of the sort. I just noted that you should teach a consistent language and stay away from language extension that are only available on certain compilers.

Thanks for your input, I'll keep it in mind for my next tutorial.
Skype: ryand.8

[Image: XbaG5gl.gif]

I think he should use the extensions local to his compiler if he is making a tutorial on that specific compiler, that being stated you should also state that other extensions if they were to use a different compiler. I code myself and I use void main(), due to it working on my compiler.
@ Regios:

Keep making tutorials. Don't worry about ignorant members here on the forum. YOU are contributing useful information even if it is towards beginners. What are they doing? See my point. I'd like to see more tutorials on C++. Hopefully higher difficulty tutorials if you can. I try to read a few different materials on the same subject (ex: on for loops, on arrays, etc...) so I can get a complete understanding of it.
(Dec 31, 2011 09:15 PM)Mizz Blaise Wrote:  @ Regios:

Keep making tutorials. Don't worry about ignorant members here on the forum. YOU are contributing useful information even if it is towards beginners. What are they doing? See my point. I'd like to see more tutorials on C++. Hopefully higher difficulty tutorials if you can. I try to read a few different materials on the same subject (ex: on for loops, on arrays, etc...) so I can get a complete understanding of it.

Yeah definitely, I'll go over them in my next tutorial. Thanks!
Skype: ryand.8

[Image: XbaG5gl.gif]

nice beginner tutorial, keem 'em coming!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by w3w3w3Aug 3, 2021 12:38 PM 1 328 Aug 3, 2021 12:38 PM
Last Post: SIM-5058597
[C] by g4143Aug 21, 2019 01:44 PM 6 511 Aug 21, 2019 01:44 PM
Last Post: YouthInAsia
[.NET] by blake.haleApr 8, 2018 10:31 AM 8 638 Apr 8, 2018 10:31 AM
Last Post: adenfr