Advertise here.

Sunday, 19 August 2012

C++ Programming Tutorial Lesson #10 - Classes

Creating a class The class keyword is used when creating a class followed by the class name. All other parts of the class are put inside curly brackets. The last curly bracket must be followed by a semi-colon. Here is an example of how to create a class called Student. class Student{}; A class can have private members which other parts of the program can't access and public members which can be accessed from outside the class. The private and public keywords are used for this. class Student{private:   char name[50];public:   int...

C++ Programming Tutorial Lesson #9 - Reading and writing text and data files

Writing text files A text file is a file that stores words and sentences in plain text. You must include the fstream.h header file before you can use files. You must then create a ofstream object which will let you open a file and output data to it. When creating the object you must put the name of the file you want to open in brackets. If the file does not exist it will be created and if it already exists it will be cleared. ofstream f("test.txt"); You write to a file in a similar way as you write to the screen. All you have to do is replace...

C++ Programming Tutorial Lesson #8 - Structures

What is a structure? A structure is used to group variables together under one name. If you have a student structure for example then you will group the student number and student name together. How to create and use a structure The first thing to do when creating a structure is to use the struct keyword followed by the structure name. You need to put curly brackets after that which will contain the things that make up the structure. Always remember to put a semi-colon after the curly brackets. Here is an example of a structure called student. #include<iostream>struct...

C++ Programming Tutorial Lesson #7 - Functions

What is a function? A function is a sub-program that your program can call to perform a task. When you have a piece of code that is repeated often you should put it into a function and call that function instead of repeating the code. Creating your own function You declare a function in a similar way as you create the main function. You first put void then the function name and some brackets. The function code goes between curly brackets after that. Here is a function that prints Hello. void PrintHello(){   cout << "Hello\n";} Calling...

C++ Programming Tutorial Lesson #6 - Arrays

What is an array? An array is a group of variables that uses one name instead of many. To access each of the individual variables you use a number. How to use an array To declare an array put square brackets after the name of the array. These square brackets must have the number of variables or elements inside them. Here is an example of how to declare an array of integers which has 3 elements int arr[3]; You can set the values of an array by using the number of the element you want to set in the square brackets behind it. Array elements...

C++ Programming Tutorial Lesson #5 - Pointers

What is a pointer? A pointer is a variable that holds a memory address. It is called a pointer because it points to the value at the address that it stores. Using pointers If you want to declare a pointer variable you must first choose what data type it will point to such as an int or a char. You then declare it as if you were declaring a variable in the normal way and then put a * in front of its name to show that it is a pointer. Here is an example of how to declare a pointer to an integer. int *pi; You can store the address of another...

C++ Programming Tutorial Lesson #4 - Loops

A loop lets you repeat lines of code as many times as you need instead of having to type out the code a whole lot of times. For Loop The for loop is used to repeat code a certain number of times between 2 numbers. You have to use a loop variable to go through each loop. The first part of a for loop initializes the loop variable to the number from which the loop starts. The second part is the condition that the loop must meet to exit the loop. The third part is used to increment the value of the loop variable. Here is an example of how to print...

C++ Programming Tutorial Lesson #3 - Decisions

if statement The if statement is used to test the values of variables and do something depending on the result. For example you can check if a value that the user has entered is equal to a certain value. int age;cout << "Enter your age: ";cin >> age;if (age == 18)   cout << "\nYou are 18 years old"; In the above example you will see that we used a == instead of a = because == is used for testing equality and = is used to assign a value to a variable. If the age entered by the user equals 18 then it prints "You...

C++ Programming Tutorial Lesson #2 - Variables and Constants

What are variables? A variable is a space in memory where a program stores data. Variables can store numbers and letters among other things. Declaring variables Before you can use a variable you must declare it. When you declare a variable you choose its data type and give it a name. Here is an example of how to declare a variable of the data type integer with the name i. int i; Here is a table of the basic data types that C++ supports. NameStores booltrue or false charCharacters intNumbers floatNumbers doubleNumbers You can declare 2...

C++ Programming Tutorial Lesson #1 - First Program

Requirements Before we start programming in C++ we need a compiler. We will be using a command line compiler for this tutorial. I will be using Borland C++ Compiler 5.5 but you can use any other ANSI/ISO compliant compiler such as gcc. Hello World Program Our first C++ program will print the message "Hello World" on the screen. Open a text editor and start by typing the following line: #include<iostream> The above line includes the...