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.
C++ is a general-purpose programming language. It can be used to create different types of software applications.
C++ supports object-oriented programming, commonly called OOP. Programs can be organized using classes and objects.
Important OOP concepts include:
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;
}
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.
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
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;
}
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.
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 {
};
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.
Abstraction means exposing important functionality while hiding unnecessary implementation details.
C++ supports abstraction through classes, access control, and abstract classes.
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.
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.
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.
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++.
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.
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.
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;
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.
The Standard Template Library, commonly called STL, provides ready-to-use containers, algorithms, iterators, and other components.
Examples include:
C++ provides file-stream classes for reading from and writing to files.
Common classes include:
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.
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.
C++ supports multiple programming approaches.
This flexibility allows programmers to choose suitable techniques for different problems.
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
}
};
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";
}
};
Modern C++ supports lambda expressions, which provide a convenient way to create small callable functions.
auto greet = []() {
std::cout << "Hello";
};
greet();
Modern C++ provides smart pointers to help manage dynamically allocated objects automatically.
Common smart pointers include:
Modern C++ standards have introduced many useful features. Examples include:
| 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 |
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;
}
Question: Which C++ feature allows a class to inherit properties and behavior from another class?