Using vector

September 27, 2008

So what is a vector?
It’s an array with that difference that we can change it’s size during the time that the program is functioning
Let’s show a short program with it:

  1. #include <vector>
  2. include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. vector<int> example;
  7. for(int i=0;int i<=6;++i)
  8. {
  9. example.push_back(i);
  10. cout<<example[i]<<endl;
  11. }
  12. getchar();
  13. return 0;
  14. }

First we include a file at the top to be able to creat vector arrays
The next part you don’t understand is probably this:


vector<int> example;

Well it’s actually really simple. In this line we define a vector with int values
What is the size?
Well actually there is no size because the vector allocates it’s memory dynamicly that means it can change it’s memory while the program is running and that is what makes it so nifty
The next part you don’t understand is probably this:


for(int i=0;int i<=6;++i)
{example.push_back(i);
cout<<example[i]<<endl;
}

What does this code do?
Well if you don’t know the concept of loops it might me strange(I’ll explain them in my next tutorial) but basicly it repeats itself 7 times until the i value is bigger than 7 and while it repeats itself it does:


example.push_back(i);
cout<<example[i]<<endl;

First it insert’s the value of i into the table(at the very and of the vector array) and then it print’s it on the screen

Well I hope I explained the problem
greetings patryksharks321

C++ and if statements

September 14, 2008

In the last tutorial describing variables I mentioned the

if

statement in the context of the bool variableFirst let’s show write a example program:

#include <iostream>
using namespace std;
int main()
{int age;
cout<<"To enter this page with erotic contents you have to be adult"<<endl;
cout<<"What is your age<<endl;
cin>>age;
if(age<18)
{
cout<<"Sorry your to young"<<endl;
}
if(age=>18)
{
cout<<"Haha we got you, You pervert"<<endl;
}
getchar();
return 0;}

So now that we have written the code let’s explain what it does:
First it print’s out a question about our age,then it takes input from the keyboard about our age
This should all be clear if you read my previous tutorial titled “Taking input within a program”
Now let’s see the main part of our program that is the

if

What it does is checkes if we are above 18 years old,18 years old or beneath the age of 18 and then according to the if statement prints something

if

statement looks something like this:

if(condition)
{
code to do….
}

The condition can be a comparisson of to variables,comparison of a variable with data of the type of which it is(for example int x; if(x>1) )and many others

Well I hope I explained the problem
greetings patryksharks321

Variables in C++

September 14, 2008

This tutorial will describe the diffrent type of variables in c++ but first let’s tell ourselfs what a variable is?

It’s a pointer to some memory in your program that cointans some specific type of data

Now to go on with this lesson let’s explain what kind of variables we have:

  • numeric
  • text
  • logical
  • defined by the user

Let’s start from the begining

1.Numeric variables have 2 main representatives:
-int
-float
Int can contain varialbes from about -32,000 to 32,000 without numbers after a dot and takes 4 bajts of memory(32 bit)
Float can have up to 6 digits in it and can go after the dot that means 3.5555 and so on

2.Text variables divide also into 2 types:
-char
-string
Char is a single sign(number,letter and so on)
String is a object defined in the string.h class that contains as many signs as we want to and it modifies it’s size itself(there is no limit to it)

3.Logical variables are represented by bool that can contain 2 values:
-true
-false
It is especially helpful with the

if

statement which I will explain in some further tutorials

4.Variables defined by user are variables that user created himself to make the program shorter and more efficient(it might sound weird but I’ll explain it soon) such as:classes and structures

Well that would be it
greetings patryksharks321

Taking input within a program

August 25, 2008
The last program we created printed a text on the screen and we explained ourselfs what we had to do to get that effect.

This tutorial will describe how to take input from a user and next print that input out on the screen
So let’s start with a bit of hard code

#include <iostream>
#include <string>
using namespace std;
int main()
{string name;
int age;
cout<<”What your name?”<<endl;
cin>>name;
cout<<”How old are you?”<<endl;
cin>>age;
cout<<”Your name is”<<”and you are<<”years old”<<endl;
getchar();
return 0;
}

Now let’s start explaining:
-#include <iostream> we now from the last program
-#include <string> let’s you operate on strings of letters through the string class

now the important part

string name;
int age;

Here we define a string variable which can contain a number of singel letters and a integer/numeric value
another line you probably don’t understand is

(…)
cin>>name;
(…)
cin>>age;

These basicly tells the computer to wait for the user to type in a string of letters in the first case and some numbers in the second(they can only be whole numbers not 4,5 or so if you want such than you need to define the variable as float)
The rest you should understand I think

Well I hope I was able to make it clearer if somebody needed it
greetings patryksharks321

Explaining your first program in C++

August 25, 2008
So let’s take the program piece by piece a part
First element is:

#include
This element tell us that we want to include into our program some external function/methods or classes but what are we including? Well that comes up next
iostream
It should be but let’s talk we just want to describe the components
What does iostream take care of?
Iostream allows us to print on the screen and take text/values from your keyboard for example:cout(output)/cin(input)
oki let’s go on

using namespace std;

This allows us to use cout without having to write every time before it std

Now we get down to the real code at last

int main()
{
(…)
}

The int main contains our program and is itself a function that returns a int value(numeric)

cout<<”Hello World”<<endl;

This part allows us to output text on the screen by using cout and <<
We were outputing a fragment of text not a value so we used ” ” if hadn’t used it we would have gotten an error because we didn’t define a variable called Hello World

getchar();

this function wait’s for us to enter a key and by that we can see the output of the program(if we hadn’t done that it would blink fast and turn of)

return 0;

this is the integer value the int main() function returns to signal the programs ended succesfully

I hope it might help some beginners
greetings patryksharks321

Gettings started – Part One(installing a compiler)-Windows

July 2, 2008

This tutorial should lead through installing a C++ IDE on Windows and show you how to write your first program in this language

  1. Download and install
  2. First program

Downloading and installing an IDE

So if you want to start programming in C++ you’ll have to get a tool for it.I propose dev-cpp for this purpose:
Dev-C++ is a free integrated development environment (IDE) distributed under the GNU General Public License for programming in C/C++. It is bundled with the open source MinGW compiler. The IDE is written in Delphi.

Then let’s start.

1.Downloading and installing

Point one go to the download page
http://www.bloodshed.net/dev/devcpp.html

After downloading the file start the .exe and basicly click next,next etc.(you might want to choose where you have your files kept for safety but that is your one choice by default they are installed in C:\devcpp)

2. Your first program

Open the dev-cpp and click on the file tab at the top and choose new file

type this in it:

#include <iostream>
using namespace std;
int main()
{cout<<”Hello World<<std::endl;
getchar();
return 0;
}

About C++

July 2, 2008

C++

This subpage is dedicated to c++ based programming

what is c++:

C++ is something between a high-level programming language(like Python mainly for system applications) and a low-level programming language(Assembler for machine coding).C++ is a combination of Objected orientated programming,procedural programming and generic programming

why learn C++:

  • - It’s handy to learn C++ if you intend to take working as a programmer,web-admin,web-developer because:
  • -it’s one of the most popular languages on the world
  • -many people know it so if you have problems somebody will be able to help you almost for sure
  • -it’s got a great object orientated programming side which helps if you write longer applications
  • -there are many libraries to learn for c++ that can help you with such things as:web-development,game-making,graphics and so on

greetings


Follow

Get every new post delivered to your Inbox.