0% found this document useful (0 votes)
15 views5 pages

Important C++ Concepts

The document provides an overview of essential C++ concepts, including basic syntax, functions, object-oriented programming, constructors and destructors, memory management, STL, templates, exception handling, operator overloading, file I/O, namespaces, and multithreading. Key features highlighted are function overloading, encapsulation, inheritance, polymorphism, and the use of templates for generic programming. C++ enhances C by incorporating object-oriented principles and advanced programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Important C++ Concepts

The document provides an overview of essential C++ concepts, including basic syntax, functions, object-oriented programming, constructors and destructors, memory management, STL, templates, exception handling, operator overloading, file I/O, namespaces, and multithreading. Key features highlighted are function overloading, encapsulation, inheritance, polymorphism, and the use of templates for generic programming. C++ enhances C by incorporating object-oriented principles and advanced programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Here’s a summary of important C++ concepts that you can use as notes:

1. Basic Syntax

 Variables & Data Types: Similar to C, but C++ supports additional types like bool and string.

 Control Flow: Uses if, else, while, for, switch, break, continue, just like C.

2. Functions

 Functions are similar to C, but C++ allows function overloading (same function name,
different parameters) and default arguments.

 Example of function overloading:

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

 double add(double a, double b) { return a + b; }

3. Object-Oriented Programming (OOP)

C++ is object-oriented, meaning you can define classes and objects:

 Classes and Objects: A class is a blueprint for objects, and an object is an instance of a class.

 class Person {

 public:

 string name;

 int age;

 void display() {

 cout << "Name: " << name << ", Age: " << age << endl;

 }

 };

o Create an object:

o Person p1;

o p1.name = "John";

o p1.age = 25;

o p1.display();

 Encapsulation: Bundling data and methods into a single class and restricting direct access to
some of an object's components (using private, protected, and public).

 Inheritance: A way to create a new class from an existing class, inheriting attributes and
behaviors.

 class Student : public Person {

 public:
 string school;

 };

 Polymorphism: Ability to use the same method name but with different behaviors. Achieved
via function overloading and function overriding.

 class Animal {

 public:

 virtual void sound() { cout << "Animal sound"; }

 };

 class Dog : public Animal {

 public:

 void sound() override { cout << "Bark"; }

 };

 Abstraction: Hiding implementation details and only showing functionality (using abstract
classes and interfaces).

 class Shape {

 public:

 virtual void draw() = 0; // pure virtual function

 };

4. Constructors and Destructors

 Constructor: Special function called when an object is created.

 class Car {

 public:

 Car() { cout << "Car created!" << endl; }

 };

 Destructor: Special function called when an object is destroyed.

 class Car {

 public:

 ~Car() { cout << "Car destroyed!" << endl; }

 };

5. Memory Management

 Similar to C, C++ uses new and delete for dynamic memory allocation and deallocation.
 Example:

 int* ptr = new int(5); // dynamically allocated integer

 delete ptr; // free the memory

6. STL (Standard Template Library)

 The STL is a collection of template classes and functions in C++ for handling collections like
arrays, lists, stacks, queues, etc.

 Common containers:

o vector: Dynamic array

o list: Doubly linked list

o map: Key-value pairs

o stack/queue: LIFO/FIFO data structures

Example of using a vector:

vector<int> v;

v.push_back(10); // Add 10 to vector

v.push_back(20);

cout << v[0] << endl; // Access first element

7. Templates

 Templates allow you to write generic code that works for any data type.

 Function templates:

 template <typename T>

 T add(T a, T b) {

 return a + b;

 }

 Class templates:

 template <class T>

 class Box {

 public:

 T value;

 Box(T v) : value(v) {}

 };

8. Exception Handling
 C++ supports exception handling through try, catch, and throw.

 try {

 throw "Some error!";

 }

 catch (const char* e) {

 cout << "Error: " << e << endl;

 }

9. Operator Overloading

 C++ allows you to overload operators to give them custom behavior with user-defined types
(classes).

 Example:

 class Complex {

 public:

 int real, imag;

 Complex operator + (Complex const& other) {

 Complex temp;

 temp.real = real + other.real;

 temp.imag = imag + other.imag;

 return temp;

 }

 };

10. File I/O

 File handling is similar to C, using fstream for input/output operations.

 #include <fstream>

 ofstream outFile("file.txt");

 outFile << "Hello, C++!" << endl;

 outFile.close();

 ifstream inFile("file.txt");

 string content;

 getline(inFile, content);
 cout << content << endl;

 inFile.close();

11. Namespaces

 C++ allows you to group code in namespaces to avoid name conflicts.

 namespace MyNamespace {

 int x = 5;

 }

 using namespace MyNamespace;

 cout << x; // Outputs 5

12. Multithreading

 C++11 introduced the thread class for working with threads.

 Example:

 #include <thread>

 void print() {

 cout << "Hello from thread!" << endl;

 }

 int main() {

 thread t(print);

 t.join(); // Wait for thread to finish

 }

C++ builds on C, adding object-oriented features, templates, and more advanced concepts. If there’s
any specific topic you want to explore further (like classes, memory management, or templates), let
me know!

You might also like