C++ Beginner Course With Code Examples: Lesson 1: What Is C++?
C++ Beginner Course With Code Examples: Lesson 1: What Is C++?
C++ is a powerful programming language used for system programming, game development,
and more. It supports both procedural and object-oriented programming, making it versatile.
1. Install an IDE: Download and install an IDE like Code::Blocks or Visual Studio Code.
2. Write Your First Program:
cpp
Copy code
#include <iostream> // Required for input and output
using namespace std;
int main() {
cout << "Hello, World!"; // Print to the screen
return 0; // End the program
}
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
// This is a comment
cout << "C++ program structure is simple!";
return 0;
}
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
bool isStudent = true;
string name = "John";
return 0;
}
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
return 0;
}
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
return 0;
}
Lesson 7: Loops
1. For Loop:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Number: " << i << "\n";
}
return 0;
}
2. While Loop:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << "Number: " << i << "\n";
i++;
}
return 0;
}
3. Do-While Loop:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << "Number: " << i << "\n";
i++;
} while (i <= 5);
return 0;
}
Lesson 8: Functions
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 5;
cout << "Sum: " << add(x, y) << "\n";
return 0;
}
1. Array Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
2. String Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
string name = "Alice";
cout << "Name: " << name << "\n";
name = "Bob";
cout << "Updated Name: " << name << "\n";
return 0;
}
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int number = 10;
int* ptr = &number;
return 0;
}
cpp
Copy code
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
void display() {
cout << "Brand: " << brand << ", Year: " << year << "\n";
}
};
int main() {
Car myCar;
myCar.brand = "Toyota";
myCar.year = 2020;
myCar.display();
return 0;
}
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
char op;
double num1, num2;
switch (op) {
case '+':
cout << "Result: " << num1 + num2;
break;
case '-':
cout << "Result: " << num1 - num2;
break;
case '*':
cout << "Result: " << num1 * num2;
break;
case '/':
if (num2 != 0)
cout << "Result: " << num1 / num2;
else
cout << "Division by zero error!";
break;
default:
cout << "Invalid operator!";
}
return 0;
}
cpp
Copy code
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> data;
string input;
return 0;
}