C++ Cheat Sheet Free pdf (Part – 1)

Chapter 1: C++ Basics

Introduction about C++

C++ is a general-purpose programming language created as an extension of C, supporting object-oriented programming.

Introduction about OOPs

Object-Oriented Programming (OOP) is a paradigm based on objects and classes, encapsulating data and behavior.

C Vs C++

C is procedural, while C++ supports both procedural and object-oriented programming.

First Program in C++

#include <iostream>
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Comment in C++

// This is a single-line comment
/* This is a
   multi-line comment */

Character Set in C++

C++ uses ASCII or Unicode character sets, depending on implementation.

Expression and Statements in C++

Expressions compute values; statements perform actions.

int a = 5; // Statement with an expression

Token in C++

Tokens include keywords, identifiers, literals, operators, and separators.

int num = 10; // 'int', 'num', '=', '10', ';' are tokens

Data Types in C++

Basic data types include int, float, double, char, bool.

int age = 25;

Variables in C++

Variables store data values.

float salary = 45000.50;

Chapter 2: Input/Output Statements

Standard Output (COUT)

std::cout << "Hello, World!" << std::endl;

Standard Input (CIN)

int age;
std::cin >> age;

The Standard Error Stream (CERR)

std::cerr << "Error occurred!" << std::endl;

The Standard Log Stream (CLOG)

std::clog << "Log entry" << std::endl;

Stream Manipulators

std::cout << std::hex << 255; // Output in hexadecimal

Parameterized Manipulators

std::cout << std::setw(10) << 123; // Width of 10

Chapter 3: Control Statements

Introduction

Control statements manage the flow of a program.

if Statement

if (x > 0) {
    std::cout << "Positive";
}

if-else Statement

if (x > 0) {
    std::cout << "Positive";
} else {
    std::cout << "Non-positive";
}

Nested if-else Statement

if (x > 0) {
    if (x < 10) {
        std::cout << "Single digit positive";
    }
}

else-if ladder Statement

if (x > 0) {
    std::cout << "Positive";
} else if (x == 0) {
    std::cout << "Zero";
} else {
    std::cout << "Negative";
}

Intro about Loops

Loops repeatedly execute a block of code.

While Loop

int i = 0;
while (i < 5) {
    std::cout << i << " ";
    i++;
}

For Loop

for (int i = 0; i < 5; i++) {
    std::cout << i << " ";
}

Do-While Loop

int i = 0;
do {
    std::cout << i << " ";
    i++;
} while (i < 5);

Nesting of Loop

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        std::cout << i << ", " << j << std::endl;
    }
}

Jumping Statement

Includes break, continue, and goto.

for (int i = 0; i < 10; i++) {
    if (i == 5) break;
    std::cout << i << " ";
}

Chapter 4: Functions

Basics of Functions

Functions perform specific tasks and return a value.

Defining a Functions

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

Categories of a Function

Includes standard library functions and user-defined functions.

Parameters Passing Mechanism

Parameters can be passed by value or by reference.

void increment(int &a) {
    a++;
}

Inline Functions

Defined with inline to request inlining.

inline int square(int x) {
    return x * x;
}

Nesting of Function

Calling one function within another.

int add(int a, int b) {
    return a + b;
}
int main() {
    int sum = add(2, 3);
}

Recursion

A function that calls itself.

int factorial(int n) {
    if (n <= 1) return 1;
    else return n * factorial(n - 1);
}

Storage Classes

Defines scope, visibility, and lifetime of variables.

static int count = 0; // Static variable

Inbuild Function

Standard functions provided by the C++ library.

#include <cmath>
double root = sqrt(16.0); // sqrt is a standard library function

Chapter 5: Arrays

Introduction Arrays

Arrays store multiple values of the same type.

One Dimensional Array

int numbers[5] = {1, 2, 3, 4, 5};

Two Dimensional Array

int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

Multi-Dimensional Array

Arrays with more than two dimensions.

int cube[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};

Passing Arrays to Function

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        std::cout << arr[i] << " ";
    }
}

Character Array

char name[] = "John";

Array of String

std::string names[] = {"John", "Jane", "Doe"};

Chapter 6: Pointer

Basics of Pointers

Pointers store memory addresses.

Using, Declaring, Initialization of Pointer

int x = 10;
int *p = &x;

Operations on Pointers

int y = *p; // Dereferencing

Pointer and Arrays

int arr[3] = {1, 2, 3};
int *p = arr; // Points to first element

Array of Pointers

int a = 1, b = 2;
int *arr[] = {&a, &b};

Pointer and Const

const int *ptr = &x; // Pointer to a constant integer

Pointers and String Literals

const char *str = "Hello";

Pointers to Pointers

int **pp = &p;

Void Pointers

void *vp;

Invalid Pointers and Null Pointers

int *p = nullptr; // Null pointer

Pointers and Functions

Pointers can be passed to functions.

void increment(int *p) {
    (*p)++;
}

Passing Pointers to Functions

void printValue(int *p) {
    std::cout << *p;
}

Return Pointer From Function

int* getPointer() {
    int *p = new int(10);
    return p;
}

Accessing Structure Member with Pointer

struct Person {
    char name[50];
    int age;
};
Person p = {"John", 30};
Person *ptr = &p;
std::cout << ptr->name;

Dynamic Memory

int *p = new int(10);
delete p;

Chapter 7: Structure

Basics of Structure

Structures group different types of variables.

Accessing Structure Member

struct Person {
    char name[50];
    int age;
};
Person person1 = {"John", 30};
std::cout << person1.name;

Arrays OF Structure

Person people[2] = {{"John", 30}, {"Jane", 25}};

Nested Structure

struct Address {
    char city[50];
    char state[50];
};
struct Person {
    char name[50];
    Address address;
};

Structure with Function

void printPerson(Person p) {
    std::cout << p.name << ", " << p.age;
}

Pointer to Structures

Person *ptr = &person1;

Bit Field

struct {
    unsigned int age : 3;
} person;

Union

union Data {
    int i;
    float f;
    char str[20];
};
Data data;

Enumerated Data Types

enum Color {RED, GREEN, BLUE};
Color color = RED;

Type Aliases (typedef/using)

typedef struct {
    char name[50];
    int age;
} Person;
Person person1;

Chapter 8: Object and Classes (Part – 1)

Basics of Object and Class

Classes are user-defined types that represent objects.

How to Write a Class

class Person {
public:
    char name[50];
    int age;
};

Access Specifiers

class Person {
private:
    char name[50];
public:
    void setName(const char *n) { strcpy(name, n); }
};

Creation of Objects

Person person1;

Memory Allocation of Objects

Objects can be allocated on the stack or heap.

Person *person2 = new Person();
delete person2;

Accessing Data Member of Class

person1.age = 30;

Member Function in Class

void printName() {
    std::cout << name;
}

Function Overloading

class Math {
public:
    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }
};

Constructor

class Person {
public:
    Person() { std::cout << "Constructor called"; }
};

Destructor

class Person {
public:
    ~Person() { std::cout << "Destructor called"; }
};

Dynamic Memory Allocation for Objects

Person *p = new Person();
delete p;

Pointers to Objects

Person p;
Person *ptr = &p;

Second Part – 2nd Part

Here is the Link of the Second (2nd) Part of C++ Cheat Sheet.

PDF Download File

Here is the Download button for Install the Pdf of C++ Cheat Sheet.

FREE DOWNLOAD

Send download link to:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top