+ All Categories
Home > Education > Project two c++ tutorial

Project two c++ tutorial

Date post: 26-Jun-2015
Category:
Upload: tunde-salaam
View: 327 times
Download: 1 times
Share this document with a friend
Popular Tags:
16
Babatunde Salaam C++ tutorials Contents Introduction ……………………………………………………………………………………… Programme structure …………………………………………………………………………. Variables …………………………………………………………………………………………… Assignments ………………………………………………………………………………………. Input and output………………………………………………………………………………… Loops ………………………………………………………………………………………………… Functions …………………………………………………………………………………………… Arrays ………………………………………………………………………………………………… Recursion ……………………………………………………………………………………………. Selection………………………………………………………………………………………………..
Transcript
Page 1: Project two c++ tutorial

Babatunde Salaam

C++ tutorialsContents

Introduction ………………………………………………………………………………………

Programme structure ………………………………………………………………………….

Variables ……………………………………………………………………………………………

Assignments ……………………………………………………………………………………….

Input and output…………………………………………………………………………………

Loops …………………………………………………………………………………………………

Functions ……………………………………………………………………………………………

Arrays …………………………………………………………………………………………………

Recursion …………………………………………………………………………………………….

Selection………………………………………………………………………………………………..

Page 2: Project two c++ tutorial

Babatunde Salaam

Introduction

This tutorial is for people who want to learn C++ programing from scratch or who has limited understanding about C++.

In the past few years programming in C++ has change and this tutorial will update people on the new futures that have being added to C++ in the past years.

This tutorial can also be useful for other language users as the difference is not massive.

The tutorial is divided into various parts that’s will help leaners to understand the language in a very easy way

All programs code that will be presented in this tutorial can be modify by readers and can also be change into any form that best suits the reader as this is a very good way for readers to learn.

At the end of this tutorial you should have full understanding about C++ programme and be able to write a full C++ program.

Page 3: Project two c++ tutorial

Babatunde Salaam

Programme structure

#includes<iostream>: this is writing at the beginning of every C++ code and it and authoritative instruction. The #includes<iostream> tells the pre-processor to include to include the iostream standard file. The #includes<iostream>: is like a header file that has the rules of how the code you have writing works, it also contains everything needed for input/output

Using namespace std; all codes in C++ are declared within namespace. The name space allows the programmer to declare cout without retyping std before every count. For example, if the programmer does not put using namespace at the beginning of the code then he or she has to put STD at the beginning of every in and out put.

Int main () this is the line where the main function is being declared. This is the point where every C++ programme starts their execution. Any other function writing before or after this does not affect the job of the int main(), and the () stands for function declaration.

Cout << “Happy birthday”: the happy birthday is a statement and that is how every statement is wringing in C++. And this statement must be visible after the code has being declared. Cout shows the output of the statement. The purpose of cout in this code is to output happy birthday and also the cout is already declared iostream.

endle; the endle means end of line which tells the reader that the line has ended and every statement in C++ must always end with semicolon (;) because it shows the end of statement.

return 0; this shows the end of function and also declaring the programme completed with no error.

Page 4: Project two c++ tutorial

Babatunde Salaam

Variables (int)

In programming we want to be able to perform other task rather than just writing simple programmes like “happy birthday”. As a programmer we should be able to write programmes that will be useful to everyone in their day to day life activity. For example, writing a programme that allows people to calculate.

This type of programmes can be done by using variable. Variable in C++ allows programmer to store data instead of repeating it again. For example, in the programme above I was able to save my “price”, “paid” and “change” as a variable using int.

The two programmes are the same but written in a different way, the second programme allows users to input their own values unlike the first one which already has the value inputted to it by the programmer.

Int: the int can be describe as a variable where the values are stored. For example in the first programme I was able to declare the value for price and paid using int.

Page 5: Project two c++ tutorial

Babatunde Salaam

In the first programme, the programmer did not use cin because he/she did not request the user to input any value and also because the input has already been giving as a variable with the value unlike the second programme.

cout: this shows the output of a statement as describe in the previous topic.

change = paid-price; this is the formula that is used to calculate the change.

Page 6: Project two c++ tutorial

Babatunde Salaam

Assignment (=)Assignment is part of the C++ operators and since we understand how variables work and the usefulness in the previous chapter we can now start learning assignment and their usefulness.

In C++ most operators that are used are mostly sings which can be found on keyboard which is better because it safes time typing up and also can be used in any country especially none English countries.

In the programme above, the statement assign 20 to the variable boy and also the same for the variable girl (boy = 20, girl = 10) the equal (=) sign is the sign that enables the variables to be assign to each value. When using the assignment sign a variable has to be declared, for example, the boy and girl are variable because they were already declared as an integer. The assignment operation always takes place from right to left, and never the other way.

Page 7: Project two c++ tutorial

Babatunde Salaam

Cin (Input) and cout (output)As a programmer, we want to be able to interact with the users by enabling them to be able to Input values or letter into the code, this means we have to trust them not to change the programme. To be able to allow users to input letters or value of their choice into a programme we have to make use of the cout and cin which means input and out.

Basically programmers let user input values or letter of their choice.

A perfect example is a calculator; users can input any number or value of their choice into a calculator and get a calculated output.

The programme above is an example of cin(input) and cout (output) . The programme allows users to input the value of their choice which allows them to have an out value they have entered.

The cin is already inputted by the programmer (cin>>boy, cin>> girl)

Page 8: Project two c++ tutorial

Babatunde Salaam

Loops (while and do-while loop)As a programmer, there are statements that we would like to repeat and typing them over and over again can take time. Using loop can help programmer avoid situations like this because loop can help repeat a statement as many times as possible.

There two different types of loops.

The while loop

The main function of a while loop is to statement while the condition set in expression is true.

The program enable the user to input numbers and the while loop enables them to reapet the calculation process if they want. The program has given them the option to press yes if they wish to try the calculation again or no if they do not want to.

At the start of the statement, while (running) was included in the program so as to enable the statement to repeat every time the user wishes to repeat the calculation.

if (userInput == ‘n’) this is statement means that if the use input no then the programme should stop running (running = false ;)

Page 9: Project two c++ tutorial

Babatunde Salaam

The do while-loop

The do while loop execute the code that the while loop contains without checking if the condition I true. The do while-loop has same function as a while loop except that it gives the programme conditions. For example the above program tells the user to input any number between 10 and 50. Any time a number between those two values are entered the program keeps running until the value 10 is entered.

The do-while loop is usually used when the condition that has to determine the end of the loop is determined within the loop statement itself, for example entering a value between 10 and 50 is a condition that has already been determine in the loop statement so the user has to stick with it.

Page 10: Project two c++ tutorial

Babatunde Salaam

FunctionsWhen writing code in C++ it often occurs that their few codes in which we use more than once in our program. Repeating them often consumes time so therefore we use function which is a block of code which can be named and can be reuse anywhere in our code.

int area (int pi, int r) is an example of a function in the program above. The value for pi has being declared before writing the function. This was done because pi has a fixed value so it has to be declared first.

The functions where later used in the statement when the formula to calculate the area was declared.

We can keep using this pi and r because they are already declared as a function at the beginning of the code.

Page 11: Project two c++ tutorial

Babatunde Salaam

ArraysAn array is used to store data collection of data. An array can also be refer to as collection of variable stored using int. different values can be saved In an array without having to declare different variables every time. For example we can save 10 different numbers in an array.

An example of how an array can be declared can be seen in the program below.

Float number [6] ={2,4,6,8,9,10} this is an example of an array been declared, the [6] stands for the number of arrays that been declared and the number{2,4,6,8,9,10} are the numbers stored in the array which can later be refer to in the program.

Recursion

Page 12: Project two c++ tutorial

Babatunde Salaam

Recursion in C++ is when a function calls itself. Sometimes when writing a recursion programme or code, it seems like the function will never end, but this can be change sometimes. Therefore we can control the amount of time we want our function to reoccur.

The case in which we end our recursion is called a base case.

Recursion is similar to loop because it repeat the same code, but it is easer to express ideas in recursion.

This is an example of a simple recursion programme that’s counts for ever until it stop by the user.

Recurse (count +1); this tells the user the increment you want the recursion to be in. for example, in this programme I want the increment to be 1.

Recurse (10); this tells the user where the count is going to start from. For example, in this programme I want the count to start form 10.

Page 13: Project two c++ tutorial

Babatunde Salaam

This is an example of recursion with base, this programme helps to find the factor of a doll.

This is similar to the first recursion programme that was writing except that this has an end to its calculation.


Recommended