Lesson 3 of 60 – Features of C++
5%

Features of C++

C++ is a powerful programming language that provides many features for developing efficient, structured, and large-scale software applications. It combines procedural programming features with object-oriented and generic programming features.

Note: C++ provides features such as classes, objects, inheritance, polymorphism, templates, pointers, references, exception handling, and the Standard Template Library (STL).

1. General-Purpose Language

C++ is a general-purpose programming language. It can be used to create different types of software applications.

  • Desktop applications
  • Games
  • System software
  • Embedded applications
  • High-performance applications

2. Object-Oriented Programming

C++ supports object-oriented programming, commonly called OOP. Programs can be organized using classes and objects.

Important OOP concepts include:

  • Classes
  • Objects
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

3. Procedural Programming

C++ supports procedural programming. A program can be divided into functions and a sequence of instructions.

#include <iostream>

void greet() {
    std::cout << "Hello";
}

int main() {
    greet();
    return 0;
}

4. High Performance

C++ is designed for efficient execution and provides direct control over many system resources.

This makes it suitable for applications where performance is important, such as games, system software, and other performance-sensitive programs.

5. Compiled Language

C++ source code is normally compiled into machine code before execution. A compiler translates the source program into an executable form.

Source Code
     ↓
   Compiler
     ↓
Machine Code
     ↓
  Execution

6. Classes and Objects

C++ allows programmers to create classes and objects. A class defines a type, while an object is an instance of that class.

class Student {
public:
    int age;
};

int main() {
    Student s1;
    s1.age = 20;

    return 0;
}

7. Encapsulation

Encapsulation means combining data and functions inside a class and controlling access to them.

class Student {

private:
    int age;

public:
    void setAge(int a) {
        age = a;
    }
};

Access specifiers such as private and public help control access to class members.

8. Inheritance

Inheritance allows one class to derive from another class. It can be used to create a relationship between classes and reuse existing functionality.

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

class Dog : public Animal {
};

9. Polymorphism

Polymorphism means that the same interface or operation can work with different forms of objects.

C++ supports forms of polymorphism through features such as function overloading and virtual functions.

10. Abstraction

Abstraction means exposing important functionality while hiding unnecessary implementation details.

C++ supports abstraction through classes, access control, and abstract classes.

11. Function Overloading

C++ allows multiple functions to have the same name when their parameter lists are different.

void display(int x) {
}

void display(double x) {
}

This feature is called function overloading.

12. Operator Overloading

C++ allows certain operators to be given special meanings for user-defined types through operator overloading.

For example, a class can define how the + operator works with its objects.

13. Templates

Templates support generic programming. They allow functions and classes to work with different data types.

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

The same function template can be used with compatible types.

14. Generic Programming

Generic programming allows algorithms and data structures to be written in a way that can work with different data types.

Templates are one of the main mechanisms used for generic programming in C++.

15. Pointers

C++ supports pointers. A pointer is a variable that stores the memory address of another variable.

int number = 10;

int* ptr = &number;

std::cout << *ptr;

Pointers are useful for memory-related operations and dynamic data structures.

16. References

C++ supports references, which provide another name or alias for an existing variable.

int number = 10;

int& ref = number;

ref = 20;

After the assignment, number also contains 20.

17. Dynamic Memory Management

C++ provides facilities for dynamic memory allocation. Modern C++ also provides smart pointers to help manage dynamically allocated objects safely.

Traditional dynamic allocation uses operators such as new and delete.

int* p = new int(10);

delete p;

18. Exception Handling

C++ provides exception handling to deal with exceptional situations during program execution.

try {
    // Code that may cause an exception
}
catch (...) {
    // Handle the exception
}

The main keywords are try, catch, and throw.

19. Standard Template Library

The Standard Template Library, commonly called STL, provides ready-to-use containers, algorithms, iterators, and other components.

Examples include:

  • vector
  • map
  • set
  • stack
  • queue

20. File Handling

C++ provides file-stream classes for reading from and writing to files.

Common classes include:

  • std::ifstream – reading from files
  • std::ofstream – writing to files
  • std::fstream – reading and writing files

21. Portable Programming

C++ programs can be developed for different operating systems when the code uses portable language and library features and the required compiler and platform support are available.

This makes C++ useful for software intended to run on multiple platforms.

22. Low-Level Memory Access

C++ provides features such as pointers, references, and manual memory management that allow programmers to work closely with memory.

This level of control is useful in systems and performance-sensitive programming.

23. Multiple Programming Styles

C++ supports multiple programming approaches.

  • Procedural programming
  • Object-oriented programming
  • Generic programming
  • Functional-style programming features

This flexibility allows programmers to choose suitable techniques for different problems.

24. Constructors and Destructors

C++ classes can have constructors and destructors.

A constructor is used when an object is initialized, while a destructor is used when an object is destroyed.

class Student {
public:

    Student() {
        // Constructor
    }

    ~Student() {
        // Destructor
    }
};

25. Virtual Functions

C++ provides virtual functions that support dynamic polymorphism. They are commonly used with inheritance and base-class pointers or references.

class Animal {
public:
    virtual void sound() {
        std::cout << "Animal sound";
    }
};

26. Lambda Expressions

Modern C++ supports lambda expressions, which provide a convenient way to create small callable functions.

auto greet = []() {
    std::cout << "Hello";
};

greet();

27. Smart Pointers

Modern C++ provides smart pointers to help manage dynamically allocated objects automatically.

Common smart pointers include:

  • std::unique_ptr
  • std::shared_ptr
  • std::weak_ptr

28. Modern C++ Features

Modern C++ standards have introduced many useful features. Examples include:

  • auto
  • Range-based for loops
  • Lambda expressions
  • Smart pointers
  • Move semantics
  • Structured bindings
  • Concepts
  • Ranges

29. Major Features at a Glance

Feature Purpose
Classes Create user-defined types
Objects Create instances of classes
Inheritance Reuse and extend class functionality
Polymorphism Support different forms of behavior
Templates Write generic code
Pointers Work with memory addresses
References Create aliases for existing objects
STL Use reusable containers and algorithms
Exception Handling Handle exceptional situations

30. Why C++ is Powerful

C++ combines high-level programming features with detailed control over resources. It supports procedural, object-oriented, and generic programming techniques.

Its combination of performance, flexibility, extensive language features, and a standard library makes C++ suitable for many types of software development.

#include <iostream>

int main() {

    std::cout << "C++ is powerful!";

    return 0;
}

📌 Key Points

  • C++ is a general-purpose programming language.
  • C++ supports procedural programming.
  • C++ supports object-oriented programming.
  • C++ supports generic programming through templates.
  • C++ provides classes and objects.
  • C++ supports inheritance, polymorphism, and abstraction.
  • C++ provides pointers and references.
  • C++ supports dynamic memory management.
  • C++ provides exception handling.
  • The Standard Template Library provides reusable containers and algorithms.
  • Modern C++ includes features such as lambda expressions and smart pointers.
  • C++ is suitable for many performance-sensitive applications.

🧠 Quick Quiz

Question: Which C++ feature allows a class to inherit properties and behavior from another class?