C++ Brief Syntax Guide

Assuming you have programming experience and basic knowlegde on how to use terminal:)

Table of Contents

BASICS

  1. SIMPLE HELLO WORLD PROGRAM
  2. COMPILING C++ CODE
  3. EXECUTING C++ CODE
  4. DATA TYPES
  5. BASIC ARITHMETIC
  6. CONDITIONAL STATEMENT
  7. FUNCTIONS
  8. LOOPS
  9. POINTER todo

ADVANCED

  1. CLASS AND OBJECT
  2. GETTER & SETTER

BASICS

SIMPLE HELLO WORLD PROGRAM

#include <iostream>
using namespace std;
int main() {
	/* 
	 * printf() in c
	 * endl means end line, simillar to "\n"
	 */
	cout << "Hello World!" << endl;
	return 0;
}
	#or
#include <iostream>
int main() {
	/* printf() in c*/
	std::cout << "Hello World!" << endl;
	return 0;
}

COMPILING C++ CODE

#Terminal
gcc <file name>.cpp -lstdc++ -o <file name>
	#or
g++ <file name>.cpp -o <file name>
#e.g
gcc helloWorld.cpp -o helloWorld

EXECUTING C++ CODE

#If used -o flag
	./<file name>  #e.g ./helloWorld
#else
	./a.out

DATA TYPES

/* Common data types */
/* Numbers */
int x = 10;	// For Integer, sizeof(int) = 4 bytes
float x = 10; 
double x = 10; // For double, it has higher d.p precision than float
/* Characters */
char c = 'A'; // For Character, sizeof(char) = 1 byte
string c = "Hi, I am UxU"; // For String, its is an array of chararaters
/* Boolean */
bool isTrue = 1; // 1 = true, 0 = false
/* 
 * If you want to know the size in bytes 
 */
cout << sizeof(<insert data type>) << endl;

BASIC ARITHMETIC

#include <iostream>
#include <cmath>
using namespace std;
int main() {
	/* 
	 * Try it yourself
	 * init integer x and y 
	 */
	int x = 2, y = 4;
	cout << x + y << endl; // Addition
	cout << x - y << endl; // Substraction
	cout << x / y << endl; // Division
	cout << x * y << endl; // Multiply
	cout << x % y << endl; // Mod
	/* Requires cmath lib */
	cout << sqrt(y) << endl; // Square root 
	cout << pow(x,y) << endl; // x^y = 2^4
	return 0;
}

CONDITIONAL STATEMENT

#include <iostream>
using namespace std;
int main() {
	/*
	 * 1 is true
	 * 0 is false
	 */
	bool isFalse = false;
	if (isFalse) cout << "False" << endl; // False will be printed
	else cout << "true" << endl; 
	return 0;
}

FUNCTIONS

#include <iostream>
using namespace std;
int add(int x, int y); // Declare function
int main() {
	/* init 2 numbers */
	int num1 = 10, num2 = 20;
	cout << add(num1, num2) << endl;
	return 0;
}
int add(int x, int y) {
	return x + y;
}

LOOPS

#include <iostream>
using namespace std;
int main() {
	/* init one count variable */
	int count1 = 0, count2 = 0;
	/* For loop */
	int i;
	for(i = 0; i < 10; i++) {
		count1 += i;
	}
	/* While loop */
	int j = 0;
	while(j < 10) {
		count2 += j;
		j ++; // increament the loop counter
	}
	cout << count1 << endl;
	cout << count2 << endl;
	return 0;
}

POINTER

to be added... I missed it out

ADVANCED

CLASS AND OBJECT

#include <iostream>
using namespace std;
class Human {
	// This is a class
};
int main() {
	Human h; // h is an object of class Human
	return 0;
}

CONSTRUCTOR

class Human {
public:
	/* constructor */
	Human(string name, int age){
		(*this).name = name;
		(*this).name = age;
	}
	string name;
	int age;
};

GETTER & SETTER

class Human {
public:
	/* constructor */
	Human(string name, int age){
		setName(name);
		setAge(age);
	}
	/* getters */
	string getName() {
		return this -> name;
	}
	int getAge() {
		return this -> age;
	}
private:
	string name;
	int age;
	/* setters */
	void setName(string name) {
		(*this).name = name;
	}
	void setAge(int age) {
		(*this).name = age;
	}
};