C++ Cheat Sheet

C++ Cheat Sheet

C++ Cheat Sheet will guide you to work on C++ with basics and advanced topics. Cheat Sheet for students, engineers, and professionals.

Introduction

Bjarne Stroustrup came up with the idea of creating a powerful language that combines Simula67 and C. As a result, at AT&T Bell Laboratories developed C++ that supports object-oriented features, with the power of both languages. C++ was also initially known as ‚ÄúC with Classes‚ÄĚ, since classes were added, also considering it as an extension of C.

Features

  • Polymorphism
  • Classes
  • Objects
  • Encapsulation
  • Abstraction
  • Inheritance
  • OOPs Overview

Object oriented was introduced to solve the issues with traditional programming. OOP protects data from accidental modification by considering data as a critical element. Objects are what OOP makes it different. Programs are divided into objects.

Data Types

C++ Data Types

Variables

Variable names in C++ are the names given to locations in memory. These locations can be an integer, characters, or real constants. The following examples demonstrate the usage of variables in C++:

C++ Variables

User Input

The cin is used in C++ for User Input.  It is used to read the data input by the user on the console:

Output

Decision Making Statements

With C++ decision making statements, easily make decisions based on different conditions:

  • if: The¬†if decision making statement executes the code, if the condition is true:
  • if‚Ķelse: The¬†if‚Ķelse decision making statement executes some code if the condition is true and another code if the condition is false:
  • if‚Ķelse if‚Ķelse: The¬†if‚Ķelseif‚Ķelse¬†statement executes code for different conditions. If more than 2 conditions are true, you can use else for the false condition:

Loops

C++ loops execute a block of code and this code executes while the condition is true:

  • while loop: The block of code executes only when the condition is true:
  • do‚Ķwhile loop: The condition is checked at the bottom of the loop:
  • for loop: The for loop is used when you can set how many times a statement is to be executed.

Arrays

An array is a collection of elements, of fixed size and type, for example, a collection of 5 elements of type int:

  • Declare an array:

  • Initialize an array:

  • Two-Dimensional Arrays:

Two Dimensional Arrays Rows and Columns Structure

Functions

A function is an organized block of reusable code that avoids repeating the tasks again and again:

  • Create a Function:

  • Call a Function:

  • Function Parameters:

  • Multiple Parameters:

Recursion

When a function calls itself, it is called Recursion in C Language. In another sense, with Recursion, a defined function can call itself. Recursion is a programming approach, that makes code efficient and reduces LOC. The following figure demonstrates how recursion works when we calculate Factorial:

Recursion in C++

Overloading

In C++, we can easily create more than one function with a similar name but different parameters. Let’s say we have the following function:


We can create more functions with the same name:

Structures

Structures in C++ allow you to store data of different types. You can easily store variables of types, such as int, float, char, etc:

  • Define a Structure:

  • Declare a Structure while defining it:

  • Declare a Structure with struct:


    Declare e1, e2, and e3 of type emp:
  • Access Structure Members:

Classes and Objects

A class is a template for an object, whereas an object is an instance of a class.

  • Create a Class: To create a class, use the class¬†keyword in C++. The public is an access specifier in C++. For the public access specifier, the members can be accessed from outside the class easily. In the below example, the class is Rectangle and the class members are length and width:
  • Create an Object: An object is an instance of a class i.e., an object is created from a class. Object represents real-life entities, for example, a Bike is an object. Let us create three objects of the above class Rectangle in C++:

Constructors

A Constructor in C++ is always public and has the same name as the class name. Constructor gets called automatically when we create an object of a class.

  • Create a Constructor:

Destructors

A Destructor in C++ destructs an object. It has the same name as the class name and gets automatically called when an object gets created like Constructors. Let us see the syntax of Destructors with our class name Studyopedia. It is prefixed by a tilde sign as shown below:

Remember, the following points about Destructors:

  • Define Destructor only once in a class
  • The access specifier concept does not work on Destructors in C++
  • The Destructors cannot have parameters, unlike Constructors.

Inheritance

The Inheritance concept in C++ brings the code reuse functionality by using the inherit feature. In Inheritance:

  • Base class:¬†The base class is the parent class.
  • Derived class:¬†The derived class is the child class since it is derived from the parent class.

The Father Class i.e. the Base class:

The Kid class i.e. the Derived class:

Access members:

Types of Inheritance:

  • Multilevel Inheritance

  • Multiple Inheritance

Pointers

The Pointer is a variable that is used to store the memory address as its value. However, to get the memory address of a variable, use the & operator:

  • The Address Operator: Access the address of a variable. Defined by¬†&, the ampersand sign.
  • The Indirection Operator:¬†Access the value of an address. Defined by¬†*, the asterisk sign.

Create and Declare a Pointer:

Example

Output

What next?

After completing C++, follow the below tutorials:


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


C++ File Handling
C++ Introduction
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading