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

C++ Programming Basics Lesson 1

Uploaded by

bosagsm226
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C++ Programming Basics Lesson 1

Uploaded by

bosagsm226
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ Programming Basics: Lesson 1

Introduction to C++
C++ is a general-purpose programming language created by Bjarne Stroustrup. It is widely used in
system/software development and game development.
C++ is an extension of the C language and includes object-oriented, generic, and functional
features.
In this lesson, we'll start with the basics: creating a simple program, understanding variables, and
learning about data types.

Writing a Simple Program


Every C++ program starts with a main function. The structure is as follows:

#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}

- #include <iostream> includes the input-output stream library to allow us to use 'cout' for displaying
text.
- using namespace std; lets us avoid writing 'std::' before every standard command.
- int main() is the entry point of any C++ program. The code inside this function runs when we
execute the program.

Variables
Variables are used to store data in C++. A variable has a name, a type, and a value.
To declare a variable, specify its type, followed by the variable name, and (optionally) assign a
value.

Examples:
int age = 25; // Declares an integer variable named age with value 25
float height = 5.9; // Declares a floating-point variable named height
char grade = 'A'; // Declares a character variable named grade
string name = "John"; // Declares a string variable named name

Basic Data Types


1. int: Used for integers (whole numbers). Example: int age = 25;
2. float: Used for single-precision floating-point numbers. Example: float price = 19.99;
3. double: Used for double-precision floating-point numbers. Example: double pi = 3.14159;
4. char: Used for single characters. Example: char letter = 'A';
5. string: Used for sequences of characters (requires #include <string>). Example: string name =
"Alice";
6. bool: Used for true/false values. Example: bool isHappy = true;

Exercise
1. Write a simple C++ program to declare variables of different types (int, float, char, string, bool)
and display them using cout.
2. Try changing the values of these variables and observing the output to see how they work in
different contexts.

You might also like