Features Of C++
Features of C++
C++ is a programming language used to create software, games, operating systems, applications, and many other things. It was developed by Bjarne Stroustrup as an extension of the C language.
1. Object-Oriented Programming (OOP)
C++ supports object-oriented programming.
Think of an object like a real-world thing—for example, a Car. A car has properties such as color and speed, and actions such as start and stop.
C++ allows us to create these things using classes and objects.
Code Example
class Car {
public:
void start() {
cout << "Car started";
}
};
2. Simple and Easy to Learn
C++ has many similarities with the C programming language. Its syntax is relatively straightforward, especially for basic programs.
Code Example
cout << "Hello World";
This simply tells the computer to display Hello World.
3. Fast and Efficient
C++ is a fast programming language because it is compiled directly into machine code.
This makes it useful for applications where speed is important, such as:
- Games 🎮
- Operating systems
- Browsers
- Embedded systems
4. Platform Independent
C++ programs can be used on different operating systems such as:
- Windows
- Linux
- macOS
However, the program may sometimes need to be recompiled for the particular platform.
5. Supports Classes and Objects
C++ allows programmers to create classes and objects.
A class is like a blueprint.
An object is something created from that blueprint.
Code Example
class Student {
public:
string name;
};
Here, Student is a class. We can create objects such as:
Code Example
Student s1;
6. Encapsulation
Encapsulation means keeping data and the functions that work with that data together inside a class.
It also helps protect data from unwanted access.
Code Example
class Bank {
private:
int balance;
public:
void setBalance(int b) {
balance = b;
}
};
Here, balance is private, so it cannot be directly accessed from outside the class.
7. Inheritance
C++ supports inheritance.
Inheritance means that one class can get properties and functions from another class.
Think of it like a family: a Child can inherit some characteristics from a Parent.
Code Example
class Animal {
public:
void eat() {
cout << "Eating";
}
};
class Dog : public Animal {
};
8. Polymorphism
Polymorphism means "many forms."
It allows the same function or operation to behave differently in different situations.
For example, the function draw() could behave differently for a Circle and a Square.
C++ supports polymorphism through:
- Function overloading
- Operator overloading
- Virtual functions
9. Abstraction
Abstraction means showing only the important information and hiding unnecessary details.
For example, when you drive a car, you use the steering wheel and pedals. You don't need to know exactly how the engine works internally.
C++ provides abstraction using classes, functions, and access specifiers.
10. Function Overloading
C++ allows multiple functions to have the same name but different parameters.
Code Example
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
Both functions are called add(), but they accept different numbers of values.
11. Operator Overloading
C++ allows programmers to give special meanings to operators such as +, -, ==, etc.
For example, the + operator normally adds numbers, but it can also be used to add two objects when operator overloading is defined.
12. Dynamic Memory Allocation
C++ allows memory to be allocated during program execution.
It uses operators such as:
Code Example
new
delete
Example:
</> C++
try {
throw 10;
}
catch (int x) {
cout << "An error occurred";
}
This helps prevent a program from suddenly crashing when an expected error occurs.
14. Rich Standard Library
C++ has a large Standard Template Library (STL) that provides ready-made tools.
Some commonly used components are:
vectorstackqueuemapset- Algorithms such as
sort()
Code Example
vector<int> numbers = {3, 1, 2};
sort(numbers.begin(), numbers.end());
This makes programming easier and saves time.
15. Supports Multiple Programming Styles
C++ supports different programming approaches, including:
- Procedural programming
- Object-oriented programming
- Generic programming
This makes C++ a flexible language.
Quick Revision Table
| Feature | Simple Meaning |
|---|---|
| Object-Oriented | Uses classes and objects |
| Fast | Executes programs quickly |
| Platform Independent | Can be used on different operating systems |
| Encapsulation | Keeps data and functions together |
| Inheritance | One class can use features of another |
| Polymorphism | One thing can have many forms |
| Abstraction | Hides unnecessary details |
| Function Overloading | Same function name, different parameters |
| Operator Overloading | Gives operators special meanings |
| Dynamic Memory | Allocates memory while the program runs |
| Exception Handling | Handles runtime errors |
| STL | Provides ready-made data structures and algorithms |
In one line:
C++ is a powerful, fast, flexible programming language that supports procedural, object-oriented, and generic programming.
RANREV INFOTECH