[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
newcsharpdotentlink » realnumbers » CppVariable » BCPL » CurlFAQ » TurboCpp » QbasicFAQ_LoadImages » Brainfuck » CppStl » WhatLinksHere » CPP-Beginners-Tutorial-II

C++ Beginners Tutorial II

-------------------------------

This is the 2nd part of the C++ beginners tutorial. It covers the following issus: operators, functions and pointers.

Operators

Operators are a core feature of C++. The list below shows some basic operators supported by C++:

 //boolean operators 
! != == && || & | 
 //mathematical operators 
+ - % / x += -= %= /=  < > <= >= %= 
 //special operators 
[ ] -> ->* .* ++ -- << >> ?: ^ ::


I will discuss some of the operators in the 3rd part of this tutorial which covers classes and templates because they are only used with classes and templates.

One thing that is important to know is, when using operators is the fact that in C++ exists a so called hierarchy of operators.

Mathematical operators

int a = 2; 
int b = 3;
int  c = 0;
int  d = 2;
c = a + (b*d); //is 8
c = b % a; //is 1 
c = (a+b)*d; //is 10


In this example, the code within the braces will be calculated first, because multiplaction goes before addition. So the result of b would be 8 because 2 + (2*3) is 8. If we would set the braces around the additional part, the result would be 10 because (2+3) * 2 is 10.

Bit operators

The bit operators follow logical rules.

bool a = true;
bool b = false;
return a || b; //will be true
return a && b; //will be false because all conditions associated by && have to be true to resolve the whole term to true
return !a; //will be false because the opposite of true is false


Special operators

C++ supports a variety of special operators.

int a = 1;
int b = 2;
string* str;
a++; //will change the value of a to 2, but it will have a return of the old value of a (1) (post-increment)
++a; //will change the value of a to 2 too and it will return the new value (2) (pre-increment)
b--; //will change the value of b to 1 and returning 2 as above described
--b; //will change the value of b to 1 and returning 1 the same way as before
return str[1]; //will be t because str is an array and s the 2nd item
return a >= b ? true : false; 
//will be false because a is not greater than b
//this operator is a short form of the
//if else statement
if(a >= b)
 return true;
else
 return false;


The example above demonstrates the use of increment, decrement and the array operators. Increment and decrement are used to increment or decrement a value. Hence their name. The array [ ] operator is used to work with arrays. The last operator is a short form of the if-else statement and has the same impact.

Functions

Functions are another elementary feature of C++. C++ supports the following types of functions:

//function with no parameters but return value
int myfunc() { return int; } 
//function with paramter and return value
int myfunct(int x) { return x; } 
//function with no parameters and no return value
void myfunc() {}
//function with unspecified numbers of arguments
int myfunc(...)
//function with standard parameters
int myfunc(int x = 1) 


These are the elementary types of functions in C++. If you want to use functions within your code consider the following basic rules.

Return values

Functions usually have a return value except when they are defined as void.

Parameters

Functions may have one or more parameters. Parameters appear within the braces after the name of the function. Parameters are usually defined as variables. Another name for parameter is argument.

Unspecified number of parameters

Functions can have an unspecified number of paramters. You can define unspecified parameters by putting (...) 3 dots within the braces.

Standard parameters

C++ supports the use of standard parameters. Standard paramters are set as follows: int myfunc(int x = 3). In this case, we have defined a standard parameter of x = 3 .

last edited (November 4, 2006) by bilderbikkel, Number of views: 37910, Current Rev: 27 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Site Management by Lars Hagelin at Kontantkort.se.