C++ is a powerful, general-purpose programming language used to create software, games, desktop applications, operating systems, embedded systems, and many other types of applications.
C++ is an extension of the C programming language and supports both procedural and object-oriented programming.
C++ is a general-purpose programming language developed as an extension of the C language. It provides features such as classes, objects, inheritance, polymorphism, and templates.
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
The above program is a simple C++ program that displays a message.
C++ is useful for learning important programming concepts.
C++ is called a general-purpose language because it can be used to develop many different types of software.
C++ was developed from the C programming language. Many basic concepts of C are also found in C++, but C++ adds several additional programming features.
For example, C++ supports classes and objects, which are fundamental concepts of object-oriented programming.
C++ supports object-oriented programming, commonly called OOP. OOP organizes programs around objects and classes.
Important OOP concepts include:
C++ source code is normally compiled before it is executed. A compiler translates the source code into machine code that the computer can execute.
This compilation process helps C++ programs achieve high performance.
C++ source code is commonly saved using the .cpp file extension.
Example:
hello.cpp
The .cpp extension tells us that the file contains C++ source code.
Here is a simple C++ program:
#include <iostream>
int main() {
std::cout << "Welcome to C++";
return 0;
}
This program displays Welcome to C++ on the screen.
The #include directive is used to include a header file in a C++ program.
For example:
#include <iostream>
The iostream header provides functionality commonly used for input and output operations.
The iostream header is commonly used when a C++ program needs standard input and output functionality.
It provides objects such as std::cout and std::cin.
#include <iostream>
The main() function is the entry point of a normal C++ program. Program execution begins from the main function.
int main() {
return 0;
}
In the following declaration, int specifies that the main function returns an integer value.
int main() {
return 0;
}
The statement return 0; commonly indicates successful completion of the program.
The std::cout object is used to display output on the standard output stream.
#include <iostream>
int main() {
std::cout << "Hello C++";
return 0;
}
The insertion operator << is commonly used with std::cout to send data to the output stream.
std::cout << "Hello";
Here, the text Hello is sent to the standard output.
You can use multiple insertion operators in the same statement.
std::cout << "Name: " << "Rahul";
This prints both pieces of text in sequence.
The std::endl manipulator can be used to move the output to the next line.
std::cout << "Hello" << std::endl;
std::cout << "Welcome";
The escape sequence \n can also be used to create a new line.
std::cout << "Hello\n";
std::cout << "C++";
The second message will appear on the next line.
A statement is an instruction that tells the program to perform an operation.
std::cout << "Hello";
Most C++ statements end with a semicolon ;.
The semicolon is used to terminate many C++ statements.
int age = 20;
std::cout << age;
Forgetting a required semicolon can cause a compilation error.
C++ is case-sensitive. This means uppercase and lowercase letters are treated differently.
int age = 20;
int Age = 30;
Here, age and Age are different identifiers.
Comments are notes written inside source code. They are ignored by the compiler.
Single-line comment:
// This is a comment
Multi-line comment:
/*
This is a
multi-line comment
*/
A variable is a named memory location used to store a value.
int age = 20;
Here, age is an integer variable containing the value 20.
C++ provides several basic data types.
int age = 20;
double price = 99.50;
char grade = 'A';
bool passed = true;
The std::cin object is commonly used to take input from the user.
#include <iostream>
int main() {
int age;
std::cin >> age;
std::cout << "Age: " << age;
return 0;
}
The extraction operator >> is commonly used with std::cin to read input.
int number;
std::cin >> number;
Many names from the C++ standard library are available inside the std namespace.
std::cout << "Hello";
std::cin >> age;
The std:: prefix specifies that cout and cin belong to the standard namespace.
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Your age is: " << age;
return 0;
}
This program takes an age from the user and displays it.
C++ is used in many areas of software development.
In this C++ tutorial series, you will gradually learn C++ from basic concepts to advanced programming topics.
Question: Which language is C++ developed from?