Post: [C++] Basic's of [C++]
04-23-2011, 03:20 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Hey guys

Im here to teach the people of nextgenupdate the basic's of c++

I will update this thread as i learn more stuff .


I do no that Drackos has a basics of c++ thread .

But it is only 15 % through at the moment so i thought while people are waiting for him to update it i will make one to keep you updated .

If you are having any problems please just message me i will do my best to help

[edit] Guide will take longer due to internet problems ( stupid virgin ) [edit]
[multipage=Setting up your compiler]

For This guide i will be using devcpp

Download it from here

You must login or register to view this content.

install it then load it up

goto file -> New source file

then press ctrl+s then save your soucre file somewhere

to compile your program all you have to do is press ctrl + 9
[multipage=Basic Program]

    #include <iostream>

using namespace std;

int main()
{
cout << "Hello world "<<endl;
system("pause");
return 0;
}


You have propaly just looked at this and gone :S

I will now explain it to you step by step

    #include <iostream>

What this does is it includes the header 'iostream' into your program this allows you to do the basic function's like 'cout'


    using namespace std;

Using namespace std; lets the compiler know that you are using the functions which are in your header 'iostream'


    int main()


This is the funciton name and type
'int' standing for integer this tells the compiler that you are going to be returning a number at the end of the function .

main - is the name of the function .

    {

This tells the compiler that it is the start of the function

    cout << "Hello world "<<endl;


This use's the function 'cout' which is in the header which we included

The " mark's where the start of the text is and the other " marks the end of the text

endl this end's the line .

    ;


This tells the compiler it is the end of the function which we used .

    system("pause");


this is another function which is from a built in header ( already include by your compiler )
    return 0;


Rember that are function is a int well because it is a int we have to return a integer .

     } 


this tells the compiler it is the end of our function

This is the most easy program to make

Comment marks are ways to wright text in your program's
source code with out it effecting your program to do this it is as simple as doing this
     // then wright your text here 

once you press enter to go to a new line u have to put some more comment marks


[multipage=Varibles]

Part 1 Using varibles

    int Myvarible;


This is how you define a varible

Varibles are used in most every program

just think of any game where you have health there is a varible for the health

A varible is a way to store information e.g a integer

int is the type of varible

myvarible is the name you can put anything in there (except numbers ).

    #include <iostream>
using namespace std;

int main()
{
int myvarible;
myvarible = 12;
cout << " Hello the integer in myvarible = "<<myvarible <<endl;
system("pause");
return 0;
}


As you can see most of this is the same as the basic program in page 2

i will only explain the things which have been added

    int myvarible;


This is the declartion of the varible .

    myvarible = 12;


This is setting the varible myvarible to the integer 12

    cout << " Hello the integer in myvarible = "<<myvarible <<endl;


the point which we are intrested in is

    <<myvarible<<endl;


This is displaying what ever is in myvarible not the name of the varible

Go and compile this and a box should pop up saying

Hello the integer in myvarible = 12

Part 2 Add varibles together

Okay so know you know how to display varibles it is time to add two varibles together

So lets say i have 3 varibles

    int Num1;
int Num2;
int Sum;


So lets say i put a integer in Num1 varible and Num2 varible

    Num1 = 12;
Num2 = 20;


In c++ it is the easy to add to varibles together it is a simple as this

    Num1 + Num2 = Sum; 


Num1 + Num2 = Sum;
So this is the same as
12 + 20 = 32
so now to display it all it is

    cout << "Num1 add Num2 = " <<Sum<<endl;


Build the program yourself using the basic hello world program and the stuff which you have learnt form this tutorial .

Part 3 User input

So lets say we were going to make a program for over 16's

We need to make them to enter there age we can use varibles to do so .

To get started let declare a varible called userage and lets make it a integer

    int userage;


so now we have our varible we need to user to input a vaule

    cout << "Please Enter your age"<<endl;
cin >> userage;


So here we can see that we are using cout to output text to the screen .

cin is a new function to you.

wat cin does is it lets the user input data to the program .

so
    cin>>userage;

This is letting the user enter a number from there keypad
and then the number will be then stored into the varible userage

Now we need to tell the compiler we want to test if the varible userage is higher than 16 to do this we use a statement called a if statement .

it is used like the following

    if(userage > 16)
{
cout << " You are allowed to use this program"<<endl;
}else{
cout << "Get off this program it is only for over 16's"<<endl;
}


As you can see it is like a function

The next tutorial i will explain more about if statments

compile this and test it out .

    #include <iostream>
using namespace std;
int main()
{
int userage;
cout << "Please enter your age "<<endl;
cin >>userage;
if(userage > 16)
{
cout << "You are allowed to use the program "<<endl;
}else{
cout << "You arnt allowed on this program "<<endl;
}
system("pause");
return 0;


[multipage=Arrays]
I am not very keen on arrays but i will try and do my best on it



You must login or register to view this content.
Last edited by ii LeDgEnz x ; 04-24-2011 at 06:14 PM.
04-23-2011, 03:25 PM #2
xEnhancer
Cracking WI-FI
Originally posted by ii
Hey guys

Im here to teach the people of nextgenupdate the basic's of c++

I will update this thread as i learn more stuff .


I do no that Drackos has a basics of c++ thread .

But it is only 15 % through at the moment so i thought while people are waiting for him to update it i will make one to keep you updated .

If you are having any problems please just message me i will do my best to help

[multipage=Basic Program]


Niice ! Because i want to know more about C++ !
04-23-2011, 06:50 PM #3
system("pause"); is NOT part of the iostream. It is part of the stdlib. What if we were on macs? This function wouldn't work and the learners will be left hanging and confused. Try using functions that work for all systems.
04-23-2011, 07:26 PM #4
Originally posted by MoBaTeY View Post
system("pause"); is NOT part of the iostream. It is part of the stdlib. What if we were on macs? This function wouldn't work and the learners will be left hanging and confused. Try using functions that work for all systems.


I am sorry but with this tut a am basing it on a windows os amd sorry i will change it now
04-23-2011, 10:21 PM #5
schnzrs
Do a barrel roll!
Just don't use the system function. Only use it when you NEED to. I have never understood why all beginners of C++ always use this function at then end of their programs. Pretty much what is happening is your program will call the OS, an OS shell is opened, memory is allocated for the command, the shell will execute the pause command (which is pretty much a print function and a getch function), then the pause command waits for any keystroke, when a keystroke is registered memory is deallocated, the OS shell is then exited, the program resumes, and then quits (since in your case since it's the end of your C++ program).

Also, like MoBaTeY said, the pause command is only on windows.

tl;dr Don't use system("pause"), just use cin.
04-23-2011, 10:25 PM #6
smushpie
Can’t trickshot me!
I just finished my C++ course, the thing I have trouble understanding is the inheritance concept and the able to use constructors/destructors and other things as well :(
04-23-2011, 10:57 PM #7
Originally posted by smushpie View Post
I just finished my C++ course, the thing I have trouble understanding is the inheritance concept and the able to use constructors/destructors and other things as well :(


Inheritance is use in classes it is used to pass varibles through classes
E.g i have a class called animals then i have another class derived from that called dogs then another one derive from
The called spaniel . If i have a varible in dogs i can use it in spaniel but not any where else so only in thoses three classes or more derived classes

Contructors are confussing basicly what they do is add a line of code before a function and a destructor is just a line of code before the function ends its hard to understand them but you need to just keep studying them
04-23-2011, 11:13 PM #8
schnzrs
Do a barrel roll!
Originally posted by ii

Contructors are confussing basicly what they do is add a line of code before a function and a destructor is just a line of code before the function ends its hard to understand them but you need to just keep studying them

Not really.

A constructor is pretty much like a function that is run when an object is created.

A deconstructor is the opposite. It is run when an object is destroyed.
04-24-2011, 01:34 PM #9
Originally posted by schnzrs View Post
Just don't use the system function. Only use it when you NEED to. I have never understood why all beginners of C++ always use this function at then end of their programs. Pretty much what is happening is your program will call the OS, an OS shell is opened, memory is allocated for the command, the shell will execute the pause command (which is pretty much a print function and a getch function), then the pause command waits for any keystroke, when a keystroke is registered memory is deallocated, the OS shell is then exited, the program resumes, and then quits (since in your case since it's the end of your C++ program).

Also, like MoBaTeY said, the pause command is only on windows.

tl;dr Don't use system("pause"), just use cin.

I use system pause because it is easyier than cin to understand

---------- Post added at 02:34 PM ---------- Previous post was at 02:32 PM ----------

Originally posted by schnzrs View Post
Not really.

A constructor is pretty much like a function that is run when an object is created.

A deconstructor is the opposite. It is run when an object is destroyed.


Sorry im a bit rusty on c++ at the moment because i havent really done much of it later as i have the holiday off
04-25-2011, 03:23 PM #10
vSaBoTeuR x
< ^ > < ^ >
Dev-C++ sucks. Use the Code::Blocks IDE.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo