Untitled Document
Untitled Document
**Code:**
```cpp
#include <iostream> // Include standard I/O library
int main() {
std::cout << "Hello, World!" << std::endl; // Print text to console
return 0; // Return 0 to indicate successful execution
}
```
- `#include <iostream>`: This line tells the compiler to include the standard
input-output stream library, which allows you to print text to the console.
- `int main()`: The starting point of every C++ program. The `int` before `main`
means the program returns an integer when it finishes.
- `std::cout`: This is the output stream used to print text to the console.
`std::endl` inserts a newline at the end.
- `return 0;`: The program returns 0 to indicate successful execution.
**Code Example:**
```cpp
#include <iostream>
int main() {
int age = 25; // Integer variable
float height = 5.9f; // Float variable
double weight = 72.5; // Double variable
char grade = 'A'; // Character variable
bool isStudent = true; // Boolean variable
return 0;
}
```
**Output:**
```
Age: 25
Height: 5.9
Weight: 72.5
Grade: A
Is student: 1
```
if (number > 0) {
std::cout << "Number is positive." << std::endl;
} else {
std::cout << "Number is not positive." << std::endl;
}
return 0;
}
```
return 0;
}
```
### 5. **Functions**
Functions are blocks of code that perform a specific task. They help you
organize and reuse code.
**Function Definition:**
```cpp
#include <iostream>
int main() {
int sum = add(3, 4); // Calling the function
std::cout << "Sum: " << sum << std::endl;
return 0;
}
```
### 6. **Arrays**
An array is a collection of variables of the same type.
**Example:**
```cpp
#include <iostream>
int main() {
int numbers[] = {1, 2, 3, 4, 5}; // Array initialization
return 0;
}
```
**Class Example:**
```cpp
#include <iostream>
class Person {
private:
std::string name;
int age;
public:
Person(std::string n, int a) : name(n), age(a) {}
void displayInfo() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
int main() {
Person person1("Alice", 30); // Create an object of the Person class
person1.displayInfo(); // Call method on the object
return 0;
}
```
Would you like to dive deeper into any of these topics or have specific questions
on them?