C++ at Glance

Basic Syntax

#include <iostream>
using namespace std;

int main() {
  //code goes here
  return 0;
}



cout

cout along with insertion operator is used to print on console.
cout<<



cin

cin along with extraction operator is used to take input from user.
cin>>



Comments

Comments are used to make your code more readable. Everything in comments are ignored by the compiler.
//this is a single lined comment.
/*this is a 
multi-lined comment*/



Escape Sequences

//escape sequences
\n          //adds a new line
\t           //adds four spaces
\b          //backspace
\a          //beep sound
\f           //form feed
\r           //carriage return
\            //adds backslash
\'           //adds single quote
\?         //adds question mark
\0         //null character



Variables

Variables are used to store data of specified data type.
int sum = 10;



Data Types

Data type specifies the type of data, stored in a variable.
int sum = 10; //stores integer
float average = 90.33; //stores numbers with decimal
double percentage = 80.3452; //stores numbers with decimal but precision is double as compared to float
char letter = 'A'; //stores single character
bool isTrue = true; //stores true or false
string intro = "My name is Sajeel"; //stores sequence of characters
void myFunc();//it represent the absence of data type. Mostly used with functions and pointers



String and its methods

Strings are sequence of characters.
//declaring string
string fname = "John";
string lname = "Doe";

//concatenating strings
//using "+"
const name = fname + lname;

//using append method
string name = fname.append(lname);

//finding length of string
//using .length() method
cout << myString.length();

//using .size() method
cout << myString.size();

//accessing characters of string
cout << myString[0]; //it will access the character at first index and so on

//Changing characters
mystring[0] = "A"; //change the first character to "A"
cout << myString[0];

//taking string input ignoring spaces
getline (cin, myString);



Operators

Arithmetic Operators
+ //addition
- //subtraction
* //multiplication
/ //division
% //modulus
++ //increment
-- //decrement

Assignment Operators
= //assign value
+= //add and assign
-= //subtract and assign
/= //divide and assign
*= //multiply and assign
%= //taking modulus and assign

Comparison Operators
== //checks equality
!= //checks unequality
> //checks greater than
>= //checks greater than or equal to
< //check less than
<= //checks less than or equal to

Logical Operators
&& //returns true if all are expressions true
|| //returns true if only one expression is true
! //returns true if false and vice versa

Bitwise Operators
& //bitwise AND
| //bitwise OR
~ //bitwise NOT
>> //shift right 
<< //shift left



Conditional statements

//if statement
if(condition){
    //code
}
//if-else statement
if(condition){
    //code
}
else{
    //code
}
//else-if statement
if(condition){
    //code
}
else if(condition){
    //code
}
else{
    //code
}

//goto statement
int main(){
  //code
  label:
  // code
  goto label;
  // code
}

//Ternary Operator
variable = condition ? ifTrue : ifFalse;



Switch Statements

switch(expression){
    case const-exp:
        //code
        break;
    case const-exp:
        //code
        break;
    .
    .
    .
    default:
        //code
        break;
}



Loops

//for Loop:
for( initialization ; condition ; change-variable-value ){
    //code
}

//while Loop:
initialize variable
while(condition){
    //code
    //change-variable-value
}

//do-while Loop:
initialize variable
do{
    //code
    //change-variable-value
}while(condition);



Continue And Break Statements

//break statement
for( initialization ; condition ; change-variable-value ){
  if(condition){
    break;  //moves out of the loop when condition is true
  }
}

//continue statement
for( initialization ; condition ; change-variable-value ){
  if(condition){
    continue;  //skip the iteration when condition is true
  }
}



Arrays

//initializing arrays
int arrayName[size] = {data1, data2,.....}
//printing array
for(int i=0 ; i < ( sizeof(array)/ sizeof(int) ) ; i++){
    cout<<array[i];
}
//taking input in array
for(int i=0 ; i < ( sizeof(array)/ sizeof(int) ) ; i++){
    cin>>array[i];
}
//changing array elements at specified index
int myArray [3] = {1, 2, 3};
myArray[0] = 5; //now array has 5 at first index



Structures and Unions

//initializing struct
struct struct_name{
    //data members and member functions
};
int main(){
    //making instance of struct
    struct struct_name var_name;
    //accessing data members of struct  
    var_name.datamember;
    //accessing member functions of struct
    var_name.memberFunction();
}
//initializing union
union union_name{
    //data members 
};
int main(){
    //making instance of union
    union union_name var_name;
    //accessing data members of union  
    var_name.datamember;
}



Pointers

int var = 10;
//initialization of pointer
int *ptr = &var;
// &var is the reference to variable named var



Functions

//declaring a function
returnType functionName(dataType parameters){
    //code
}
//calling a function
functionName(arguments);
//recursion
returnType functionName(dataType parameters){
    //code
    functionName(arguments);
    //code
}

//Function Overloading
int sum(int a, int b){
  return a+b;
}
float sum(float a, float b, int c){
  return a+b;
}
int main(){
  sum(5, 10);  
  sum(5.0, 10.0, 20);  
}



Built-in Functions

// built-in functions in stdio.h
var = getchar();
int putchar(int c);
gets(var_name);
puts(var_name); 

//built-in functions in string.h
int strlen(string);
int strcmp(string1 , string2 );
int strncmp (string1, string2, n);
strcpy(string1, string2);
strncpy(string1, srting2, n);
strcat(string1,string2);
strncat(string1, string2, n);

//built-in functions in math.h
pow(x , y);
sqrt(x);
floor(x);
ceil(x);
fmod(x, y);
cos(x);
sin(x);
tan(x);
log(x);
log10(x);



Object Oriented Programming
Classes and Objects

// Syntax for class
class class_name{
  //access modifier
  public / private / protected:
  // class members
};
int main(){
  //Syntax for creating object
  class_name obj_name;
}



Constructors and destructors

class A{
  public:
  //constructor
  A(){
    //code goes here
  }

  //destructor
  ~A(){
    //code goes here
  }
};



Friend Functions

class className{
  friend returnType funcName(dataType params...);
};
returnType funcName(dataType params...) {
  // Function definition with access to private and protected members of the class
}



Friend Classes

class MyClass
{
    friend class FriendClass;
};



Encapsulation

class class_name{
  private:
    //data members
  public:
    //member functions to access data members
};



Inheritance

//Single Inheritance
class BaseClass {
  // Base class members
};
class DerivedClass : public BaseClass {
  // Derived class members
};


//Multiple Inheritance
class Shape { 
  protected:
    int length;
    int width;
  public:
    // Parametrized constructor
    Shape(int length, int width) {
      this->length = length;
      this->width = width;
    }
};
class Triangle : public Shape {
  public:
    // Parametrized constructor
    Triangle(int base, int height) : Shape(base, height) {
  }
};


//Multi-level Inheritance
class Shape { 
  protected:
    int length;
    int width;
  public:
    // Parametrized constructor
    Shape(int length, int width) {
      this->length = length;
      this->width = width;
    }
};
class Triangle : public Shape {
  public:
    // Parametrized constructor
    Triangle(int base, int height) : Shape(base, height) {
  }
};


//Hierarchical Inheritance
class Shape { 
  protected:
    int length;
    int width;
  public:
    // Parametrized constructor
    Shape(int length, int width) {
      this->length = length;
      this->width = width;
    }
};
class Triangle : public Shape {
  public:
    // Parametrized constructor
    Triangle(int base, int height) : Shape(base, height) {
  }
};


//Hybrid Inheritance
class Shape { 
  protected:
    int length;
    int width;
  public:
    // Parametrized constructor
    Shape(int length, int width) {
      this->length = length;
      this->width = width;
    }
};
class Triangle : public Shape {
  public:
    // Parametrized constructor
    Triangle(int base, int height) : Shape(base, height) {
  }
};



Polymorphism

// Compile time Polymorphism
class MathOperations {
  public:
      int add(int a, int b) {
          // code goes here
      }
      // Overloaded function
      int add(int a, int b, int c) {
          // code goes here
      }
};
Run time Polymorphism
class Animal {
  public:
    virtual void make_sound() {
      // code goes here
    }
};
  
class Dog : public Animal {
  public:
    // Overriding make_sound() function
    void make_sound() override {
      // redefining make_sound() function
    }
};
  
class Cat : public Animal {
  public:
    // Overriding make_sound() function
    void make_sound() override {
      // redefining make_sound() function
    }
};



Abstraction

class Animals{
  public:
    // pure virtual function
    virtual void behavior() = 0;
};

class Lion : public Animals{
 public:
   void behavior(){
     // redefining behavior() function for Lion class
";
   }
};

class Donkey : public Animals{
 public:
   void behavior(){
    // redefining behavior() function for Donkey class
";
   }
};



Exceptions

try {
  // here goes the code and if there is an error throw exception
  throw exception; 
}
catch () {
  // Block of code to handle errors
}



Templates

try {
  // here goes the code and if there is an error throw exception
  throw exception; 
}
catch () {
  // Block of code to handle errors
}