C++ Syntax

From TRCCompSci - AQA Computer Science
Revision as of 12:01, 13 June 2019 by Admin (talk | contribs) (Functions)
Jump to: navigation, search

'C++' is an extended version of 'C', it essentially added more features to allow for Object Oriented Programming. 'C#' is a continuation of 'C++' and was originally developed to replace 'C++', therefore 'C', 'C++', and 'C#' are very similar in many ways and have a few differences.

Comments

C++ implements comments by:

/* This is a comment */

/* C++ comments can also
   * span multiple lines
*/

// Single line comment also

Write to Console

Writing to the console is a little more complicated than other languages:

#include <iostream>
using namespace std;

main() {
   cout << "Hello World"; // prints Hello World
   
   return 0;
}

You can also use 'endl' to have a new line:

#include <iostream>
using namespace std;

int main() {
   cout << "Hello World" << endl;
   cout << "Hola Mundo"  << endl;
   
   return 0;
}

You can also concatenate using the '<<' symbols:

#include <iostream>
using namespace std;

main() {
   cout << "Hello World " << 0 <<endl; // prints Hello World followed by a 0
   
   return 0;
}

This method can be used to concatenate variable into your output (ie replace '0' with a variable name).

Escape Characters

These can also include escape characters in the string, eg '\n':

#include <iostream>
using namespace std;

main() {
   cout << "Hello\nWorld"; // prints Hello followed by a new line and then World
   
   return 0;
}

Here is a list of the escape characters available:

Escape sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

Read from Console

C++ can read from the keyboard (ie Console.ReadLine() in C# or input() in Python), This uses the 'cin' command. The example is for will read an integer (notice the direction of the '>>' is swapped for inputs):

#include <iostream>
using namespace std;

int main ()
{
  int i;
  cout << "Please enter an integer value: ";
  cin >> i;
  cout << "The value you entered is " << i;
  return 0;
}

Strings are handled differently, because using 'cin' a space is considered to be terminating character. using this method you can only enter a single word. So you need to do this instead:

#include <iostream>
#include <string> //added this include for handling strings
using namespace std;

int main ()
{
  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr); //getline is passed cin and the string to read in
  cout << "Hello " << mystr << endl;

  return 0;
}

Variables

The main Data types within C++ are:

Data Type Keyword
Boolean bool
Integer int
Floating Point float
Double floating point double
Character char

Notice, string is not one of the main types (if you think about it, a string is an array of char).

Some of the main types above can also be preceded with:

  • signed
  • unsigned
  • short
  • long

Syntax

Just like C# you must declare a variable by typing the data type followed by the name of the variable:

#include <iostream>
using namespace std;
 
int main () {
   // variable declaration:
   int a;
 
   // actual initialization
   a = 10;
 
   cout << a;
 
   return 0;
}

Notice in the code above that assignment/initialisation is also the same as in C#, this gives the variable a value and without it the variable will be null.

Local vs Global

Where you declare your variable determines the scope of the variable. A variable declared within a method will be local and only exist within the method. Declaring a variable outside a method will give it global status. This is the same as C# and is demonstrated below:

#include <iostream>
using namespace std;
 
// Global variable declaration:
int g;
 
int main () {
   // Local variable declaration:
   int a, b;
 
   // actual initialization
   a = 10;
   b = 20;
   g = a + b;
  
   cout << g;
 
   return 0;
}

If Statement

C++ If statements are exactly the same as C#.

Operators

Within if statements and also within loops, the following relational and conditional operates exist (these are the same as C#):

Relational

Symbols Explanation
== Equal
!= Not equal
<= Less than or equal
>= Greater than or equal
< Less than
> Greater than

Conditional

Operator Description Example
&& Called Logical AND operator. If both the operands are non zero then condition becomes true. (A && B) is false
|| Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.

Loops

Just like other languages Lua has the standard 3 types of loop. While will run 0 or more times (may never run), Repeat (do..while) will run at least once, For will run an exact number of times:

Each of the loop structures in C++ are exactly the same as in C#.

Functions

C++ uses the following structure for functions,

return_type function_name( parameter list ) {
  body of the function
}

If the function has a return_type it will need to return a value of that type:

int TimesTwo(int num)
{
   return num*2;
}

You could call this function by using the function name and by providing it with its parameters:


cout << TimesTwo(10) << endl;
int a;
a = TimesTwo(15);
cout << a << endl;