0% found this document useful (0 votes)
17 views7 pages

C++ Beginner Course With Code Examples: Lesson 1: What Is C++?

The document is a beginner's guide to C++ programming, covering essential topics such as setting up the environment, program structure, data types, variables, arithmetic operations, conditional statements, loops, functions, arrays, pointers, and classes. It includes code examples for each lesson to illustrate concepts and concludes with two mini projects: a calculator and a data storage system. This course aims to provide foundational knowledge for new programmers in C++.

Uploaded by

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

C++ Beginner Course With Code Examples: Lesson 1: What Is C++?

The document is a beginner's guide to C++ programming, covering essential topics such as setting up the environment, program structure, data types, variables, arithmetic operations, conditional statements, loops, functions, arrays, pointers, and classes. It includes code examples for each lesson to illustrate concepts and concludes with two mini projects: a calculator and a data storage system. This course aims to provide foundational knowledge for new programmers in C++.

Uploaded by

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

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.

Lesson 2: Setting up Your Environment

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
}

Lesson 3: Structure of a C++ Program

A typical C++ program structure:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
// This is a comment
cout << "C++ program structure is simple!";
return 0;
}

Lesson 4: Data Types

Example of declaring variables:

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";

cout << "Name: " << name << "\n";


cout << "Age: " << age << "\n";
cout << "Height: " << height << "\n";
cout << "Grade: " << grade << "\n";
cout << "Student: " << isStudent << "\n";

return 0;
}

Lesson 5: Variables and Basic Arithmetic

Example of arithmetic operations:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int a = 10, b = 5;

cout << "Addition: " << a + b << "\n";


cout << "Subtraction: " << a - b << "\n";
cout << "Multiplication: " << a * b << "\n";
cout << "Division: " << a / b << "\n";
cout << "Modulo: " << a % b << "\n";

return 0;
}

Lesson 6: Conditional Statements

Example using if and else:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int age;
cout << "Enter your age: ";
cin >> age;

if (age >= 18) {


cout << "You are an adult.\n";
} else {
cout << "You are a minor.\n";
}

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

Example of a function that adds two numbers:

cpp
Copy code
#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

int main() {
int x = 10, y = 5;
cout << "Sum: " << add(x, y) << "\n";
return 0;
}

Lesson 9: Arrays and Strings

1. Array Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int numbers[5] = {10, 20, 30, 40, 50};

for (int i = 0; i < 5; i++) {


cout << "Element " << i << ": " << numbers[i] << "\n";
}
return 0;
}

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;
}

Lesson 10: Pointers

Basic pointer example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int number = 10;
int* ptr = &number;

cout << "Value: " << number << "\n";


cout << "Pointer Address: " << ptr << "\n";
cout << "Pointer Value: " << *ptr << "\n";

return 0;
}

Lesson 11: Classes and Objects

Example of a simple class:

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;
}

Lesson 12: Mini Project 1 - Calculator

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
char op;
double num1, num2;

cout << "Enter operator (+, -, *, /): ";


cin >> op;

cout << "Enter two numbers: ";


cin >> 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;
}

Lesson 13: Mini Project 2 - Data Storage System

cpp
Copy code
#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<string> data;
string input;

cout << "Enter data (type 'exit' to stop):\n";


while (true) {
cin >> input;
if (input == "exit")
break;
data.push_back(input);
}

cout << "\nStored Data:\n";


for (string item : data) {
cout << item << "\n";
}

return 0;
}

You might also like