c++ programming cheat sheet, c++ syntax examples, c++ reference guide, c++ programming basics, c++ classes and objects, c++ oops concepts, c++ functions examples, c++ templates guide, c++ stl cheat sheet, c++ beginner tutorial
c++ programming cheat sheet, c++ syntax examples, c++ reference guide, c++ programming basics, c++ classes and objects, c++ oops concepts, c++ functions examples, c++ templates guide, c++ stl cheat sheet, c++ beginner tutorial

C++ Programming Cheat Sheet — Complete Syntax, OOP Concepts, and Examples for Beginners

C++ Programming Language Cheat Sheet (Complete Syntax, OOP Concepts, and Examples)

C++ Programming Language Cheat Sheet — Complete Reference for Developers

C++ is a high-performance, general-purpose programming language developed by Bjarne Stroustrup as an extension of C. It supports object-oriented programming (OOP), templates, and Standard Template Library (STL) — making it one of the most versatile languages for software, game, and system development.

This C++ Cheatsheet provides an easy-to-follow, structured overview of all the key syntax, features, and examples you need to become proficient in C++ programming.


C++ Basics for Beginners

ConceptDetailsExample
File Extension.cppmain.cpp
CompilerGCC, Turbo C++, Clangg++ main.cpp -o main
ExecutionRun compiled file./main
Header FilePreprocessor directives#include <iostream>
NamespaceAvoids naming conflictsusing namespace std;

Example:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, C++ World!";
    return 0;
}

Data Types and Variables in C++

TypeKeywordSize (bytes)Example
Integerint4int age = 25;
Floating-pointfloat, double4 / 8double pi = 3.1416;
Characterchar1char grade = 'A';
Booleanbool1bool isValid = true;
StringstringVariablestring name = "John";

C++ Operators

TypeOperatorsExample
Arithmetic+ - * / %x + y
Relational== != > < >= <=if (a > b)
Logical`&&
Assignment= += -= *= /=a += 10
Bitwise`&^ ~ << >>`
Ternary? :result = (a > b) ? a : b;
c++ programming cheat sheet, c++ syntax examples, c++ reference guide, c++ programming basics, c++ classes and objects, c++ oops concepts, c++ functions examples, c++ templates guide, c++ stl cheat sheet, c++ beginner tutorial

Input and Output in C++

C++ replaces the old scanf and printf with stream-based I/O.

Example:

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;
    cout << "You are " << age << " years old.";
    return 0;
}

Conditional Statements

TypeSyntaxExample
ifif (condition)if(x>0){}
if-elseif (condition) elseif(a>b) cout<<a; else cout<<b;
else-if ladderMultiple conditionsif(x>0)... else if(x<0)... else...
switchswitch(expression)switch(choice) { case 1: break; }

Loops in C++

TypeSyntaxExample
forfor(init; condition; inc)for(int i=0;i<5;i++)
whilewhile(condition)while(i<10)
do-whiledo{ }while(condition);Executes once before checking

Example:

for(int i = 1; i <= 5; i++) {
    cout << i << " ";
}

Functions in C++

TypeSyntaxExample
No returnvoid greet()void greet() { cout<<"Hi"; }
With returnint sum(int a, int b)return a+b;
Inline Functioninline int square(int x)Small reusable code
Default Argumentint add(int a, int b=5)Optional parameters

Example:

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

Object-Oriented Programming (OOP) Concepts in C++

ConceptDescriptionExample
ClassBlueprint of objectsclass Car { };
ObjectInstance of a classCar c1;
EncapsulationData hidingprivate: keyword
InheritanceReusing base classclass Child : public Parent
PolymorphismSame name, different behaviorvirtual void show()
AbstractionHiding implementation detailsUsing abstract classes
Constructor/DestructorInitialize or cleanupCar() {}, ~Car()

Example:

class Student {
    string name;
public:
    Student(string n) { name = n; }
    void display() { cout << "Name: " << name; }
};

int main() {
    Student s("Alice");
    s.display();
}

Arrays and Strings in C++

Array Example:

int marks[5] = {85, 90, 75, 88, 92};
for(int i=0; i<5; i++)
    cout << marks[i] << " ";

String Example:

string name = "C++ Programming";
cout << "Length: " << name.length();

Pointers in C++

ConceptDescriptionExample
Pointer Declarationint *ptr;Stores address
Address-of&variableGives memory address
Dereference*ptrAccess value

Example:

int num = 10;
int *ptr = &num;
cout << *ptr; // Output: 10

File Handling in C++

ModeMeaning
ios::inRead mode
ios::outWrite mode
ios::appAppend mode

Example:

#include <fstream>
ofstream file("data.txt");
file << "Hello C++";
file.close();

STL (Standard Template Library)

ComponentExamplePurpose
Vectorvector<int> v;Dynamic array
Mapmap<int,string> m;Key-value pair
Setset<int> s;Unique elements
Stackstack<int> s;LIFO
Queuequeue<int> q;FIFO

Templates in C++

Example:

template <class T>
T add(T a, T b) {
    return a + b;
}

Usage:
Templates enable generic programming, allowing code reusability for multiple data types.


Exception Handling in C++

Example:

try {
    int x = 0;
    if(x == 0)
        throw "Division by zero!";
}
catch(const char* msg) {
    cout << msg;
}


FAQ — C++ Programming Language

Q1: What makes C++ different from C?
C++ adds Object-Oriented Programming, templates, and STL to the base C language.

Q2: Is C++ good for beginners?
Yes, it’s one of the best languages to understand low-level and high-level programming concepts.

Q3: What are real-world uses of C++?
Game engines, desktop applications, operating systems, and browsers use C++.

Q4: Which compiler should I use?
Use GCC, Clang, or Visual Studio for development.

Q5: Can I run C++ online?
Yes, use the PHPOnline C++ Compiler to run C++ programs instantly.

Related Article
50+ C++ Interview Questions and Answers 2023

Here are 50+ common C++ interview questions and answers that will help you get through one of the toughest interviews. Read more

Machine Learning Cheatsheet (Unsupervised & Reinforcement Learning)

Machine Learning (ML) is a crucial part of artificial intelligence, enabling systems to automatically learn from data.This Machine Learning Cheatsheet Read more

HTML Cheat Sheet — Reference Guide to HTML Tags, Attributes, and Examples

HTML cheat sheet, HTML tags reference, HTML attributes list, HTML examples for beginners, semantic HTML guide, HTML forms tutorial, HTML Read more

Python Cheat Sheet — Complete Syntax Reference and Programming Examples

This Python Cheat Sheet is your quick reference guide for writing efficient Python code. Whether you’re preparing for coding interviews, Read more

PHP Cheat Sheet — Complete Syntax Reference and Programming Examples for Beginners

PHP Cheat Sheet — Complete Syntax Reference & Examples This PHP Cheat Sheet serves as a quick, structured reference for Read more

JavaScript Cheat Sheet — Complete ES6 Syntax, Functions, and DOM Methods with Examples

JavaScript Cheat Sheet — Complete Syntax Reference & Examples JavaScript is the core scripting language of the web, enabling interactivity, Read more

CSS Cheat Sheet — Complete CSS3 Selectors, Properties, and Layout Examples

CSS Cheat Sheet — Complete CSS3 Syntax, Selectors & Layout Examples Cascading Style Sheets (CSS) is the language used to Read more

Java Cheat Sheet — Complete Java Syntax, Data Types, Loops, and OOP Concepts for Beginners

Java Cheat Sheet — Complete Java Syntax, Classes, and Examples Java is a powerful, object-oriented programming language widely used for Read more

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments