Introduction to C++
Start your programming journey by understanding what C++ is, why it is used and how it differs from C.
START DAY 01#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Course Introduction
Learn C++ from the foundation
This course is designed to take you from the fundamentals of C++ programming to advanced concepts. Each day focuses on a specific topic followed by practice and MCQ tests.
Introduction to C++
Understand the foundation of C++ programming.
What is C++?
C++ is a cross-platform language that can be used to create high-performance applications.
C++ was developed by Bjarne Stroustrup, as an extension to the C language.
Why Use C++?
- C++ is one of the world's most popular programming languages.
- C++ can be found in today's operating systems, Graphical User Interfaces and embedded systems.
- C++ is an object-oriented programming language which gives a clear structure to programs.
- C++ allows code to be reused, lowering development costs.
Difference Between C and C++
C++ was developed as an extension of C, and both languages have almost the same syntax.
The main difference between C and C++ is that C++ supports classes and objects, while C does not.
Lesson Summary
C++ Data Types
Learn the basic data types used in C++ programs.
C++ Data Types
Data types specify the type of data that a variable can store.
In this lesson, we will learn four basic data types: int, char, string, and float.
int — Integer
int is used to store whole numbers.
Examples: 1, 2, 3, 10, 100
int a = 10;
int b = 20;
char — Character
char is used to store a single character.
It can store letters such as a to z and A to Z.
A character is written inside single quotes (' ').
char grade = 'A';
char letter = 'b';
string — Text
string is used to store a sequence of characters.
A string can contain a word, name, or sentence.
A string is written inside double quotes (" ").
string name = "Gautam";
string message = "Hello World";
float — Decimal Value
float is used to store decimal values.
Examples: 1.23, 4.24, 10.5
float a = 1.23;
float b = 4.24;
Write a C++ Program to Add Two Numbers
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
int sum = a + b;
cout << "Sum = " << sum;
return 0;
}
Sum = 30
Here, a + b adds the two numbers and stores the result in sum.
Write a C++ Program to Subtract Two Numbers
#include <iostream>
using namespace std;
int main() {
int a = 20;
int b = 10;
int difference = a - b;
cout << "Difference = " << difference;
return 0;
}
Difference = 10
Write a C++ Program to Multiply Two Numbers
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
int product = a * b;
cout << "Product = " << product;
return 0;
}
Product = 200
Write a C++ Program to Divide Two Numbers
#include <iostream>
using namespace std;
int main() {
float a = 20;
float b = 5;
float quotient = a / b;
cout << "Quotient = " << quotient;
return 0;
}
Quotient = 4
Data Types at a Glance
| Data Type | Used For | Example |
|---|---|---|
| int | Whole numbers | 10 |
| char | Single character | 'A' |
| string | Text / Sentence | "Hello" |
| float | Decimal values | 1.23 |
+ → Addition
- → Subtraction
* → Multiplication
/ → Division
C++ Data Types
C++ Data Types
Learn char, string and float with simple C++ examples.
char — Character
char is used to store a single character.
It can store characters from a to z and A to Z.
Example
#include <iostream>
using namespace std;
int main() {
char letter = 'A';
cout << "Character = " << letter;
return 0;
}
Character = A
string — Text
string is used to store a sequence of characters.
Characters can be combined to make a word, name or sentence. This is called a string.
Example
#include <iostream>
using namespace std;
int main() {
string message = "Hello World";
cout << message;
return 0;
}
Hello World
float — Decimal Value
float is used to store decimal values.
Examples: 1.23, 4.24, 10.5
Complete Code Example
#include <iostream>
using namespace std;
int main() {
float a = 20;
float b = 5;
float quotient = a / b;
cout << "Quotient = " << quotient;
return 0;
}
Quotient = 4
C++ Data Types
Swap Two Numbers
Learn how to swap two numbers using a third variable.
Swap Two Numbers Using a Third Variable
Write a C++ program to swap two numbers using a third variable.
Suppose: a = 10 and b = 20.
Store the Value of a in c
First, store the value of a in the third variable c.
c = a;
Store the Value of b in a
Now, store the value of b in a.
a = b;
Store the Value of c in b
Finally, store the original value of a, which is saved in c, into b.
b = c;
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10
The third variable c temporarily stores the original value of a.
C++ Program to Swap Two Numbers
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
int c = 0;
c = a;
a = b;
b = c;
cout << "a = " << a << endl;
cout << "b = " << b;
return 0;
}
a = 20
b = 10
Swap Two Numbers
MCQ Test Series
Test your understanding after completing the lessons.
C++ Introduction Test
Basic questions based on Day 01.
Practice Area
Practice questions coming soon.
After completing each day, practice questions will be added here.