C++ is a powerful and versatile programming language that has been widely used for developing a wide range of applications. It is an extension of the C programming language and offers additional features that make it more efficient and flexible. In this article, we will explore some of the key features of C++ and provide examples to illustrate their usage.
1. Object-Oriented Programming (OOP):
One of the most significant features of C++ is its support for object-oriented programming. OOP allows developers to organize their code into reusable objects, making it easier to manage and maintain complex projects. Here’s an example:
“`cpp
#include
using namespace std;
class Rectangle {
int width, height;
public:
void setDimensions(int w, int h) {
width = w;
height = h;
}
int getArea() {
return width * height;
}
};
int main() {
Rectangle rect;
rect.setDimensions(5, 3);
cout << “Area: ” << rect.getArea() << endl;
return 0;
}
“`
In this example, we define a `Rectangle` class with member variables `width` and `height`. We then create an object of the `Rectangle` class and use its member functions to set the dimensions and calculate the area.
2. Inheritance:
C++ supports inheritance, allowing classes to inherit properties and behaviors from other classes. This feature promotes code reusability and enables the creation of hierarchical relationships between classes. Here’s an example:
“`cpp
#include
using namespace std;
class Shape {
protected:
int width, height;
public:
void setDimensions(int w, int h) {
width = w;
height = h;
}
};
class Rectangle : public Shape {
public:
int getArea() {
return width * height;
}
};
int main() {
Rectangle rect;
rect.setDimensions(5, 3);
cout << “Area: ” << rect.getArea() << endl;
return 0;
}
“`
In this example, we have a base class `Shape` with member variables `width` and `height`. The `Rectangle` class inherits from `Shape` and extends it by adding a `getArea()` function. The `Rectangle` class can access the `width` and `height` variables defined in the `Shape` class.
3. Polymorphism:
C++ supports polymorphism, which allows objects of different classes to be treated as objects of a common base class. This feature enables code flexibility and facilitates the implementation of dynamic behavior. Here’s an example:
“`cpp
#include
using namespace std;
class Shape {
public:
virtual int getArea() = 0;
};
class Rectangle : public Shape {
private:
int width, height;
public:
Rectangle(int w, int h) {
width = w;
height = h;
}
int getArea() {
return width * height;
}
};
int main() {
Shape* shape = new Rectangle(5, 3);
cout << “Area: ” << shape->getArea() << endl;
delete shape;
return 0;
}
“`
In this example, we have a pure virtual function `getArea()` defined in the `Shape` class. The `Rectangle` class inherits from `Shape` and provides an implementation of the `getArea()` function. We create a pointer of type `Shape` that points to a `Rectangle` object, allowing us to call the `getArea()` function through the base class pointer.
4. Templates:
C++ supports templates, which enable the creation of generic functions and classes. Templates allow developers to write code that can work with different data types, providing flexibility and code reuse. Here’s an example:
“`cpp
#include
using namespace std;
template
T getMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
int num1 = 10, num2 = 20;
cout << “Max: ” << getMax(num1, num2) << endl;
double num3 = 3.14, num4 = 2.71;
cout << “Max: ” << getMax(num3, num4) << endl;
return 0;
}
“`
In this example, we define a template function `getMax()` that can work with different data types. We can call this function with integers or doubles, and it will return the maximum value.
These are just a few of the many features that make C++ a powerful and versatile programming language. By leveraging these features, developers can create efficient and maintainable code for a wide range of applications.