Skip to content

Shiang-Tian/C-plus-plus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++

This repository is the basic things for C++

// iostream stands for input output stream
#include <iostream>
main ()
{
  //cout stands for Console Output, "this is test" will be sent to the Console Output
  cout << "this is test";
}

This won't be able to run cause "cout" is inside a namespace, you need to add "using namespace" or "std::", see the following code:

Add "using namespace"

// iostream stands for input output stream
#include <iostream>
using namespace std;

main ()
{
  //cout stands for Console Output, "this is test" will be sent to the Console Output
  cout << "this is test";
}

Add "std::" in front of the cout

// iostream stands for input output stream
#include <iostream>

main ()
{
  //cout stands for Console Output, "this is test" will be sent to the Console Output
  std:: cout << "this is test";
}

Now you can run your code :)

Table of Contents
  1. Basics
  2. Operators
  3. Conditions
  4. Arrays
  5. Loops
  6. Functions

Basics

Variables

  • Variables can't have the same name
  • Variables can't start from the number
  • We can't use spaces
  • Our variables should be self-descriptive
  • Variables can't be constructed of special characters, keywords
  • Variables should be nouns

Types of Variables

  //short means short integer
  short
  
  //float allocates 4 bytes of memory
  float
  
  //double allocates a bytes of memory
  //double can save more digits after the point
  double
  
  //char is the abbreviation of the character
  char
  
  //boolean | true or false, so false is always 0, every other number is true, 1 represents true
  bool

Operators

Arithmetic and Assigment Operators

  • Addition operation
  • Substraction operation
  • Multiplication
  • Division
  • Incrementation
  • Decrementation
  • POST Incrementation
  • PRE Incrementation
  • POST Decrementation
  • PRE Decrementation

Relational Operators

1. The result of relation operation is always 0 or 1. 1 represents true, just like in bool variable. 0 represents false
2. We can also use exclamation marks to change value from true to false

Logical Operators

  • AND conjunction: &&
  • OR disjunction: ||

Bitwise Operators

  • Bitwise AND --> &
  • Bitwise OR --> |
  • Bitwise NOT --> ~ (tilde)
  • Bitwise XOR --> ^ (caret) exclusive OR
  • Bitwise left shift <<
  • Bitwise right shift >>

Conditions

Switch

  1. Variable types you can use:
  • int
  • char
  1. Variable types you can't use:
  • double
  • string

You can't use "double" cause it has some precision number after the number, and it's hard to compare

Conditional Operators

  1. If a is greater than b, then assign the message that is in " ". Else, a is less or equal to b
 (a > b) ? "a > b" : "a <= b"
  1. If e is greater than f, then chose e, else chose f. After chosing the largest one, then add 10 to it.
((e > f ? e : f) + 10 )

Calculator

  • Add: +
  • Substract: -
  • Multiply: *
  • Divide: /

Arrays

Arrays

The way of naming an array: TYPE NAME[SIZE_NUMBER_OF_ELEMENTS]

  • int array[4]

Multidimensional Arrays

int biArrays[3][4] = {0};

//checking the address of the array
//The address of biArrays[0] and biArrays[0][0] are the same
cout << &biArrays[0] << endl;
cout << &biArrays[0][0] << endl;

Loops

For Loop

//infinite loop
for (;;)
    cout << "lala" << endl;

While and Do While

While

const int SIZEOFARRAY = 10;
int q = 0;
int array[SIZEOFARRAY];
//We can get the same result using the methods below
//1.
while (q < SIZEOFARRAY)
{
  array[q] = 10 * q;
  cout << array[q] << endl;
  q++;
}
//2.
while (q < SIZEOFARRAY)
{
  array[q] = 10 * q;
  cout << array[q++] << endl;
}

This won't be executed at all

while (q)
  cout << "lalala";

Do While

do
{
  cout << "lala";
}
while(q);

Exercises and Nested Loop

//15 rows and 12 columns
for (int i = 1; i <= 15; i++)
{
  for (int j = 1; j <= 12; j++)
  {
    cout.width(4);
    cout << i * j;
  }
  cout << endl;
}

Break and Continue

Break

Everything after BREAK WON'T be executed and we are leaving the actual loop

We won't see the rows after the fifth row

for (int i = 1; i <= 10; i++) 
{
  if(i == 5)
    break;
  for (int j = 1; j <= 10; j++)
  {
    cout.width(4);
    cout << i*j;
  }
  cout << enddl;
}

We won't see the columns after the fifth column

for (int i = 1; i <= 10; i++)
{
  for (int j = 1; j <= 10; j++)
  {
    if (j == 5)
      break;
      
    cout.width(4)
    cout << i*j;
  }
  cout << endl;
}

Continue

Everything after CONTINUE instruction WON'T be executed but loop won't end because of it (we will skip the codes after CONTINUE)

We won't see the fifth row of all 10 rows

for (int i = 1; i <= 10; i++)
{
  if (i == 5)
    continue;
  
  for (int j = 1; j <= 10; j++)
  {
    cout.width(4)
    cout << i * j;
  }
  cout << endl;
}

We won't see the fifth column of all 10 columns

for (int i = 1; i <= 10; i++)
{
  for (int j = 1; j <= 10; j++)
  {
    if (j ==5)
        continue;
    cout.width (4);
    cout << i * j;   
  }
  cout << endl;
}

Functions

Pre-function Scope of Variables

Global variables can be used everywhere, but local variables only can be used in the bracket

Functions

Inline function

double add (double a, double b) {return a + b;}

Functions - return, and invoke

What return does? What does it mean to return value?

You return values FROM function, not TO the function.
When computer invokes a function so when it meets a function name with added parentheses so something like that:
functionName();
computer JUMPS to the declaration of the function.
So invoking means jumping and executing code from body (between curly brackets) of function. Computer invokes every instruction inside its body and then it comes back to the place of invocation (name of the function). The process of coming back is called returning. return instruction allows you to return with a value and relpace the invoked function with the returned value.

So when you have a function for example:

int addition (int a, int b)
{
  int result;
  result = a + b;
  return result;
}

and you invoke it like that:
addition (4, 5);
Computer jumps to the body of the function. Executes all instructions from line 3-4. And when it "meets" return keyword it returns to the place where the function was invoked. When computer returns to that place it replaces name of the function including everything that was inside parentheses by the value that was returned.
In our case, addition(4, 5) is gonna be replaced by 9, because 4 + 5 is equal to 9.

So when you write:

addition (4, 5);

You are really writing as a result:

9;

That's why when you write:

cout << addition (4, 5);

It's the same as if you had written:

cout << 9;

To sum up:
return keyword allows you to return to the place where function was invoked and return the value that is placed after return keyword.

Overloading Funcitons

About

This repository is the basic things for C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages