![]() |
C++ PROGRAMMING LANGUAGE |
Introduction to C++
C++ is a powerful, high-performance programming language that extends C by adding object-oriented features. Developed by Bjarne Stroustrup in the early 1980s, C++ combines the efficiency and flexibility of C with abstractions that facilitate complex software development. It’s widely used in systems programming, game development, real-time simulations, and high-performance applications.
Basic Concepts and Syntax
1. Syntax and Structure
```cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
```
In this example, `#include <iostream>` includes the standard input-output stream library. The `std::cout` statement prints text to the console.
2. Variables and Data Types
C++ supports various data types, including `int`, `float`, `double`, `char`, and `bool`. It also supports user-defined types such as `struct` and `class`.
```cpp
int age = 25;
float salary = 50000.50;
char grade = 'A';
bool isEmployed = true;
```
3. Control Structures
C++ uses standard control flow constructs such as `if`, `else`, `for`, `while`, and `switch`. These structures manage the flow of execution based on conditions and iterations.
```cpp
for (int i = 0; i < 5; ++i) {
std::cout << i << std::endl;
}
```
**Object-Oriented Programming (OOP)**
1. Classes and Objects
C++ introduces classes, which are blueprints for creating objects. A class encapsulates data and methods that operate on that data.
```cpp
class Person {
public:
std::string name;
int age;
void display() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
```
An object is an instance of a class:
```cpp
Person person1;
person1.name = "John";
person1.age = 30;
person1.display();
```
2. Inheritance
C++ supports inheritance, allowing one class to derive properties and behaviors from another. This facilitates code reuse and hierarchical relationships.
```cpp
class Employee : public Person {
public:
double salary;
void display() {
Person::display();
std::cout << "Salary: " << salary << std::endl;
}
};
```
3. Polymorphism
This feature allows methods to do different things based on the object’s type. It’s achieved through function overloading and overriding.
```cpp
class Shape {
public:
virtual void draw() {
std::cout << "Drawing a shape." << std::endl;
}
};
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing a circle." << std::endl;
}
};
```
Advanced Features
1. **Templates**:
C++ templates enable the creation of generic functions and classes. They allow code to handle different data types without being rewritten.
```cpp
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
```
You can use the `max` function with various data types:
```cpp
int main() {
std::cout << max(10, 20) << std::endl; // For integers
std::cout << max(10.5, 20.5) << std::endl; // For doubles
return 0;
}
```
2. Standard Template Library (STL)
STL provides a collection of algorithms and data structures such as vectors, lists, and maps. It facilitates efficient data manipulation and algorithm implementation.
```cpp
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {4, 2, 8, 6};
std::sort(numbers.begin(), numbers.end());
for (int num : numbers) {
std::cout << num << std::endl;
}
return 0;
}
```
3. Exception Handling
C++ provides mechanisms for handling errors through exception handling using `try`, `catch`, and `throw`.
```cpp
try {
int result = 10 / 0; // This will throw an exception
} catch (std::exception &e) {
std::cout << "Exception occurred: " << e.what() << std::endl;
}
```
Memory Management
C++ offers manual memory management capabilities. You can dynamically allocate and deallocate memory using `new` and `delete`.
```cpp
int* ptr = new int(10); // Allocating memory
std::cout << *ptr << std::endl;
delete ptr; // Deallocating memory
```
However, this responsibility for memory management comes with the risk of memory leaks and other issues if not handled properly.
Conclusion
C++ is a versatile and robust language suitable for a wide range of programming tasks. Its combination of procedural, object-oriented, and generic programming features makes it powerful for both system-level and application-level development. Understanding its core concepts, syntax, and advanced features is crucial for leveraging its full potential.
This overview covers key aspects of C++ and its functionalities in a concise manner. If you need further details on specific topics, feel free to ask!
0 Comments