Difference between revisions of "C++ Syntax"

From AQA Computer Science
Jump to: navigation, search
(Escape Characters)
(Functions)
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
'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=
 
=Comments=
 
C++ implements comments by:
 
C++ implements comments by:
Line 84: Line 86:
 
| \? || ? character
 
| \? || ? character
 
|-
 
|-
| \A || Alert or bell
+
| \a || Alert or bell
 
|-
 
|-
 
| \b || Backspace
 
| \b || Backspace
Line 139: Line 141:
  
 
=Variables=
 
=Variables=
Lua uses variables similar to python, you don't need to specify a data type:
+
The main Data types within C++ are:
===Numbers===
+
 
<syntaxhighlight lang=lua>
+
{| class="wikitable"
num = 42  -- All numbers are doubles.
+
|-
</syntaxhighlight>
+
! 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
  
===Strings===
+
===Syntax===
<syntaxhighlight lang=lua>
+
Just like C# you must declare a variable by typing the data type followed by the name of the variable:
s = 'walternate'  -- Immutable strings like Python.
 
t = "double-quotes are also fine"
 
u = [[ Double brackets
 
      start and end
 
      multi-line strings.]]
 
-- String concatenation uses the .. operator:
 
message = 'Winter is coming, ' .. line
 
</syntaxhighlight>
 
  
===Empty / Null===
+
<syntaxhighlight lang=c++>
<syntaxhighlight lang=lua>
+
#include <iostream>
t = nil -- Undefines t; Lua has garbage collection.
+
using namespace std;
 +
 +
int main () {
 +
  // variable declaration:
 +
  int a;
 +
 +
  // actual initialization
 +
  a = 10;
 +
   
 +
  cout << a;
 +
 +
  return 0;
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Global Variables===
+
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.
The default in Lua is that all variables are global (in python all variables are local):
 
<syntaxhighlight lang=lua>
 
  -- Variables are global by default.
 
  thisIsGlobal = 5  -- Camel case is common.
 
</syntaxhighlight>
 
  
===Local Variables===
+
===Local vs Global===
To specify a variable is only local, you need to include 'local' before the variable name:
+
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:
  
<syntaxhighlight lang=lua>
+
<syntaxhighlight lang=c++>
  -- How to make a variable local:
+
#include <iostream>
  local line = io.read()  -- Reads next stdin line.
+
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;
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 217: Line 254:
  
 
=Functions=
 
=Functions=
===Declaring a function===
 
You can declare a function using the command 'function', the parameters will be within the round brackets '()'. The use of 'local' is not really required:
 
<syntaxhighlight lang=lua>
 
local function add(first_number, second_number)
 
print(first_number + second_number)
 
end
 
  
add(2, 3) -- calling a method
+
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:
 +
 
 +
<syntaxhighlight lang=c++>
 +
int TimesTwo(int num)
 +
{
 +
  return num*2;
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Returning a value===
+
You could call this function by using the function name and by providing it with its parameters:
You can return a value from a function by using 'return'
 
<syntaxhighlight lang=lua>
 
local function add(first_number, second_number)
 
return first_number + second_number
 
end
 
  
print(add(5, 6))
+
<syntaxhighlight lang=c++>
 +
cout << TimesTwo(10) << endl;
 +
int a;
 +
a = TimesTwo(15);
 +
cout << a << endl;
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 12:01, 13 June 2019

'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;