0% found this document useful (0 votes)
19 views6 pages

CPP Full Guide

Uploaded by

Shreyansh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

CPP Full Guide

Uploaded by

Shreyansh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ Complete Guide

1. Introduction to C++

• Developed by Bjarne Stroustrup in 1979 at Bell Labs.


• Extension of C with Object-Oriented Programming (OOP) features.
• Combines low-level programming (like C) with high-level abstractions.
• Portable, efficient, and widely used for system software, game development, real-time
simulations, AI, competitive programming, etc.

2. Features of C++

• Object-Oriented: Supports classes, inheritance, polymorphism, abstraction, and encapsulation.


• Platform Independent: Write once, compile anywhere.
• Performance: Near hardware-level speed with optimization.
• Memory Management: Manual (using new and delete ) and automatic (RAII).
• Multi-Paradigm: Procedural + OOP + Generic programming.
• Standard Template Library (STL): Predefined data structures and algorithms.
• Rich Library Support: Extensive built-in functions.

3. Structure of a C++ Program

#include <iostream>
using namespace std;

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

Explanation:

• #include <iostream> – Header for input/output.


• using namespace std; – Allows using standard functions without std:: .
• main() – Entry point.
• cout – Output to console.
• return 0; – End of program.

1
4. Data Types

• Basic: int , float , double , char , bool


• Derived: array , pointer , function
• User-defined: struct , class , enum , typedef

5. Variables & Constants

int age = 18;


const float PI = 3.14;

• Variables store data.


• Constants hold fixed values.

6. Operators

• Arithmetic: + , - , * , / , %
• Relational: == , != , < , >
• Logical: && , || , !
• Bitwise: & , | , ^ , << , >>
• Assignment: = , += , -=
• Ternary: condition ? expr1 : expr2

7. Control Statements

if (x > 0) cout << "Positive";


else cout << "Negative";

for (int i = 0; i < 5; i++) cout << i;

while (n > 0) { cout << n; n--; }

do { cout << n; } while (n > 0);

switch(choice) {
case 1: cout << "One"; break;
default: cout << "Other";
}

2
8. Functions

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

• Types:
• Built-in functions
• User-defined
• Inline functions ( inline keyword)
• Function overloading
• Recursion

9. Object-Oriented Programming (OOP)

Classes & Objects

class Car {
public:
string brand;
int speed;
void drive() { cout << brand << " is driving"; }
};

Car obj;
obj.brand = "BMW";
obj.drive();

Constructors & Destructors

class Student {
public:
Student() { cout << "Object created"; }
~Student() { cout << "Object destroyed"; }
};

Inheritance

class Animal { public: void eat() { cout << "Eating"; } };


class Dog : public Animal { public: void bark() { cout << "Barking"; } };

Polymorphism

• Compile-time (Overloading)

3
• Run-time (Overriding with virtual functions)

class Base { public: virtual void show() { cout << "Base"; } };


class Derived : public Base { public: void show() override { cout <<
"Derived"; } };

Encapsulation

• Using private/protected/public access specifiers.

Abstraction

• Achieved using abstract classes and interfaces.

10. Pointers

int x = 10;
int *ptr = &x;
cout << *ptr; // 10

• Special variables that store memory addresses.

11. Arrays & Strings

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


string name = "Shreyansh";

• Multi-dimensional arrays supported.


• string class has methods like .length() , .substr() , .append() .

12. File Handling

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

• ifstream for reading.


• ofstream for writing.
• fstream for both.

4
13. Exception Handling

try {
int a = 10, b = 0;
if (b == 0) throw "Division by zero!";
cout << a / b;
} catch (const char* e) {
cout << e;
}

14. Templates (Generic Programming)

template <class T>


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

• Functions and classes can be defined with generic types.

15. Standard Template Library (STL)

• Containers: vector , list , map , set , stack , queue


• Algorithms: sort() , find() , binary_search()
• Iterators: Pointers for traversing containers.

16. Memory Management

• Dynamic Allocation: new , delete


• Example:

int *ptr = new int(5);


delete ptr;

• Smart Pointers ( unique_ptr , shared_ptr , weak_ptr )

17. Advanced Concepts

• Lambda Functions

5
auto add = [](int a, int b) { return a + b; };

• Namespaces to avoid conflicts.


• Multithreading ( #include <thread> )
• Move semantics & Rvalue references (C++11)
• Auto keyword for type deduction.

18. Applications of C++

• Game Development (Unreal Engine)


• Operating Systems (Windows, Linux kernel parts)
• Compilers
• Database Software (MySQL)
• High-performance applications (Stock trading, Simulations)
• Embedded Systems

19. Advantages

• High performance.
• Flexibility.
• Large community support.
• Reusable code with OOP.

20. Disadvantages

• Manual memory management (risk of leaks).


• Steeper learning curve than Python/Java.
• Compilation time is longer for large projects.

✅ C++ remains one of the most powerful and versatile programming languages, balancing
performance with abstraction.

You might also like