0% found this document useful (0 votes)
3 views

cpp_programming

C++ is a versatile programming language that supports procedural, object-oriented, and generic programming, making it suitable for various applications like system programming and game development. Key features include high performance, a rich standard library, and portability across platforms. The document covers the basic structure of a C++ program, data types, control structures, functions, OOP principles, pointers, and file handling.

Uploaded by

virajgadhe07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

cpp_programming

C++ is a versatile programming language that supports procedural, object-oriented, and generic programming, making it suitable for various applications like system programming and game development. Key features include high performance, a rich standard library, and portability across platforms. The document covers the basic structure of a C++ program, data types, control structures, functions, OOP principles, pointers, and file handling.

Uploaded by

virajgadhe07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ Programming

# C++ Programming

## Introduction
C++ is a powerful general-purpose programming language that supports procedural, object-oriented,
and generic programming. It is widely used in system programming, game development, real-time
applications, and more.

## Features of C++
- **Object-Oriented Programming (OOP)**: Supports encapsulation, inheritance, and polymorphism.
- **High Performance**: Efficient memory management and low-level system access.
- **Rich Standard Library**: Provides a wide range of functions for data manipulation and algorithm
implementation.
- **Portability**: Can run on different platforms with minimal modifications.

## Basic Structure of a C++ Program


```cpp
#include <iostream>
using namespace std;

int main() {
cout << "Hello, World!" << endl;
return 0;
}
```
### Explanation:
1. `#include <iostream>`: Includes the input-output stream library.
2. `using namespace std;`: Allows usage of standard library functions without prefixing `std::`.
3. `int main()`: The main function where execution begins.
4. `cout << "Hello, World!"`: Prints output to the console.
5. `return 0;`: Indicates successful program termination.
## Data Types in C++
- **Basic Types**: `int`, `char`, `float`, `double`, `bool`
- **Derived Types**: `array`, `pointer`, `reference`
- **User-Defined Types**: `struct`, `class`, `enum`

## Control Structures
### Conditional Statements
```cpp
if (x > 0) {
cout << "Positive number";
} else {
cout << "Non-positive number";
}
```
### Looping Statements
- **For Loop**:
```cpp
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
```
- **While Loop**:
```cpp
int i = 0;
while (i < 5) {
cout << i << " ";
i++;
}
```

## Functions in C++
Functions help in modular programming and code reuse.
```cpp
int add(int a, int b) {
return a + b;
}

int main() {
cout << add(5, 3);
return 0;
}
```

## Object-Oriented Programming (OOP)


C++ supports OOP principles, including:
- **Encapsulation**: Wrapping data and functions into a single unit (class).
- **Inheritance**: Deriving new classes from existing ones.
- **Polymorphism**: Ability to use the same function in different ways.

### Example of a Class in C++


```cpp
class Car {
public:
string brand;
int year;

void show() {
cout << "Brand: " << brand << ", Year: " << year;
}
};

int main() {
Car myCar;
myCar.brand = "Toyota";
myCar.year = 2022;
myCar.show();
return 0;
}
```

## Pointers in C++
Pointers store memory addresses and are essential for dynamic memory allocation.
```cpp
int x = 10;
int *ptr = &x;
cout << "Value: " << *ptr;
```

## File Handling in C++


C++ provides file handling capabilities through the `<fstream>` library.
```cpp
#include <fstream>
using namespace std;

int main() {
ofstream file("example.txt");
file << "Hello, C++ File!";
file.close();
return 0;
}
```

## Conclusion
C++ remains a dominant language due to its efficiency, flexibility, and robust features. Learning C++
opens doors to various fields, including software development, embedded systems, and game
programming.

You might also like