Introduction
What is C++?
C++ is a widely used programming language that works on different types of computer systems.
It’s used to create all sorts of software, from video games and mobile apps to operating systems and even the software that runs on cars and appliances.
Many tech interviews involve C++ due to its strong performance, making it a valuable skill to have.
Basic Syntax:
#include<iostream>
int main() {
// ... code ....
return 0;
}.
Here’s a breakdown of each component:
is a standard library that provides basic input and output services.
int main() { … }
where the program’s execution starts.
a return value of 0 from main typically indicates that the program executed successfully.
Important Points:
->Every line in C++ is ending with semicolon ;.
-> Do not forget to add the closing curly bracket } to actually end the main function.
-> The endl keyword serves the purpose of moving to a new line
moyasite
-> using namespace std can simplify code writing by eliminating the need to prefix each line with std::.
-> #include <bits/stdc++.h> contains all standard libraries of the header files. So if you include it in your code, then you need not have to include any other standard header files.
Comments in C++
Single line comment
// single line comment
Multi line comment
/*
Multi line comment
*/
Hello World Program
#include <bits/stdc++.h>
int main(){
std::cout << "Hello World!"; // Output - Hello World!
return 0;
}
This line uses the cout
object to print the text “Hello World!” to the standard output.
It is part of the C++ Standard Library’s input/output stream
(iostream) and is used for output operations.
Input and Output in C++
#include <bits/stdc++.h>
using namespace std;
int main(){
int a;
cin >> a; // Input - 5
cout << a*5 << endl; // Output - 25
return 0;
}
Using cin:
cin reads and accepts only the first word from the input.
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s; // Input - Hello World
cout << s; // Output - Hello
return 0;
}
Using getline:
To take a sentence as input, you can use getline(cin, string_name)..
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin, s); //Input - Hello World!
cout << s; //Output - Hello World!
return 0;
}
Conditional Statements:
Control statements in programming are like instructions that tell the computer what to do based on certain conditions. It’s like making a decision. If something is true, do one thing; if it’s false, do something else.
Following are the some decision-making statements:
if Statement:
The if statement is a decision-maker that executes a block of code if a specified condition is true and skips it if the condition is false.
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
if-else Statement
The if-else statement consists of two blocks, one for false expression and one for true expression.
Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
if-else-if Ladder
Use if-else if statements to choose from multiple options, executed sequentially; when a true condition is found, it’s executed, or the final else statement is executed if none are true.
Syntax:
if (condition)
{
// statements
}
else if (condition)
{
// statements
}
else{
// statements
}
Switch Statement:
The switch-case statement lets you choose between different code blocks based on a variable’s value, providing an alternative to a series of if-else if checks.
Syntax:
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
default:
statements;
}
Strings
Strings are sequences of characters used to represent text or data in programming. In C++, you can work with strings using the
#include <bits/stdc++.h>
using namespace std;
int main() {
string myString = "Hello, World!";
cout << myString << endl; // Output - Hello, World!
// accessing first letter of the string
cout << myString[0] << endl; // Output - H
// updating 4th letter of the string
myString[3] = 'K';
cout << myString << endl; // Output - HelKo, World!
return 0;
}
Basic String Operations:
Concatenation: You can concatenate strings using the + operator or the .append() method.
Length of a string: You can find the length of a string using the .length() or .size() methods.#include
#include<bits/stdc++.h>
using namespace std;
int main() {
string first = "Hello, ";
string second = "World!";
string combined = first + second; // Concatenation
cout << combined << endl; // Output - Hello, World!
cout << "Length: " << combined.length() << endl; // Output - Length: 13
return 0;
}
Loops:
In programming, loops repeat a set of instructions either for a predetermined number of times or until a specific condition is met, aiding in efficient execution of repetitive tasks.
Initialization Statement
The initialization in a loop is like setting things up before you start, such as creating and assigning values to special variables that only work within the loop.
Condition
The condition in a loop acts like a checkmark before each iteration; if it’s true, the loop continues, and if it’s false, the loop stops.
Iteration Execution
The update step in a loop serves as the final action before reevaluating the condition, whether the loop continues or stops, even if a “break” or an error occurred within the loop.
For Loop:
A for loop in programming is used to repeatedly execute a block of code a specific number of times, controlled by an initial condition, a condition for continuation, and an update statement.
Syntax:
for (initialization expr; condition expr; update expr)
{
// body of the loop
// statements we want to execute
}
While Loop:
A while loop in programming repeatedly executes a block of code as long as a specified condition remains true.
Syntax:
initialization expression;
while (condition_expression)
{
// statements
update_expression;
}
Do While Loop:
A do-while loop in programming executes a block of code at least once and then repeatedly as long as a specified condition remains true.
Syntax:
initialization expression;
do
{
// statements
update_expression;
} while (condition_expression);
Functions:
A function is a pre-defined set of instructions that processes input, performs a specific task, and provides an output, offering a convenient way to reuse code for repetitive tasks.
Syntax:
return_type function_name(parameters) {
// Function code or instructions
return result; // Optional return statement
}
Why do we use functions?
We use functions to break down a program into smaller, reusable parts, making it easier to understand, maintain, and avoid repeating code.
Passing Parameters to Functions
Pass by Value: When parameters are passed this way, the original values provided by the caller are duplicated into the function’s parameters, allowing changes inside the function without affecting the caller’s values.
Pass by Reference: In this approach, the main function’s and function’s parameters share the same memory, so changes in the function affect the main function’s parameters directly.
#include <bits/stdc++.h>
using namespace std;
void passbyreference(int& a) // pass by reference
{
a = a + 2;
cout << "Function: a = " << a << endl; // Output - Function: a = 12
}
int main() {
int a = 10;
passbyreference(a);
cout << "Main: a = " << a << endl; // Output - Main: a = 12
return 0;
}
Passing Array To Function:
Syntax:
return-type function_name(array-type array_name[]){} //passing array to function