C++ Sheet
C++ Sheet
© Copyright by Interviewbit
Contents
#include <iostream>
using namespace std;
// main() is where program execution begins.
int main() {
// This is where you write your code
return 0;
}
This is the basic structure of C++. You will have to use this structure almost every time
you write a C++ code. It contains the main function where the program execution
begins.
2. Output
3. Input
int variable;
cin takes the input from the screen and stores it in the variable.
4. Comments
//This is a comment
/* C++ comments can also
* span multiple lines
*/
if (condition) {
// This block of code will get executed if the condition is True
}
if(2<3){
cout << "2 is less than three";
}
If-else statement
If-else is an extension of the if statement. If the conditions provided with if are not
true, the statements in the else block are executed.
if(2>3){
cout<< "2 is greater than 3";
}
else{
cout<< "3 is greater than 2";
}
else if
if can be paired with else if for additional conditions.
if(2>3){
cout<< "2 is greater than 3";
}
else if(2==3){
cout<< "2 is equal to 3";
}
else{
cout<< "3 is greater than 2";
}
Switch case
switch (grade) {
case 9:
cout << "Freshman\n";
break;
case 10:
cout << "Sophomore\n";
break;
case 11:
cout << "Junior\n";
break;
case 12:
cout << "Senior\n";
break;
default:
cout << "Invalid\n";
break;
}
6. Data Types
Char
This stores characters such as ‘A’, ‘a’, ‘1’ etc. It takes 1 byte of the system’s memory.
Integer
This is the most commonly used data type. It is used to store integers and takes 4
bytes of memory.
Float
float pi = 3.14;
boolean b = false;
7. Arrays
int main() {
string str[4] = {"Volvo", "BMW", "Volkswagen", "Ford"};
for(int i=0;i<4;i++){
cout << str[i]+ " ";
}
return 0;
}
Instead of defining individual variables for each item, arrays are used to hold these
values of the same data type in a single variable. It stores the values in a contagious
block of memory, that’s why we need to specify the number of values it is going to
hold beforehand.
Declare an array by specifying the variable type, the array name enclosed in square
brackets, and the number of elements it should store.
8. Vectors
#include <vector>
int main() {
vector<int> grade(3);
grade[0] = 90;
grade[1] = 80;
grade[2] = 70;
return 0;
}
A vector in C++ is a dynamic list of things that can expand and shrink in size. It can
only hold values of the same type. It is important to #include the vector library in
order to use vectors.
vector<string> wishlist;
wishlist.push_back("Furniture");
wishlist.push_back("Basket");
wishlist.pop_back();
cout << wishlist.size(); // returns the output 1
10. Functions
When a function is called, it is a collection of statements that are all executed at the
same time. Every function has a name that is used to refer to it when it is called. A
function typically contains the following parts:
Return value: Return value is the value that the function return at the end of the
function. A function must return the value of the same data type as mentioned
in the function declaration.
Parameters: The parameters are the placeholders of the values passed on to
them. In the above code, a and b are the function parameters.
Declaration: It contains the name of the function, its return type, and
parameter list.
11. String Functions
append function
This function concatenates one string a er another string. The output of the above
code will be: Ramesh Babu
You can also change any individual character of a string in C++.
length function
12. Math Functions
The above function returns the square root of the given value.
13. Iterative statements
Suppose you want to write “Write it hundred times” hundred times. One way to do
this is to use cout 100 times. But don’t worry, there’s an easy way. You can use
iterative statements to write the same statements 100 times.
while loop
int i=1;
while(i <=100){
cout << "Write it hundred times \n";
i++;
}
The above code will print the statement 100 times (i.e. until the value of i is less than
101).
do-while loop
int i=1;
do{
cout << "Write it hundred times \n";
i++;
}
while(i<=100);
The do-while loop is very similar to the while loop. The difference is that in the while
loop the condition is checked first; and in the do-while loop, the condition is checked
a er certain tasks have been performed.
for loop
A for loop repeats a code block for a set number of times. It is divided into three
sections:
1. Initializing a counter.
2. Condition
3. The counter increment/decrement
14. Object-Oriented Programming
Class
class City {
// Attribute
string name;
int population;
public:
// Method
void increase_population() {
population++;
}
};
Objects
City Mumbai;
An object is an instance of a class that encapsulates its data and member functions.
The above code creates an instance of the class City named Mumbai.
15. File handling
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");
// Write to the file
MyFile << "File Handling in C++";
// Close the file
MyFile.close();
}.
File handling refers to reading and writing data in files. C++ provides us with functions
that allow us to do so. The above code creates a file and writes text in it.
getline();
Conclusion
There has been a lot of change in the world of C++ programming since its inception,
and it is becoming ever more important to be aware of the new syntax that is being
introduced. This article provides a summary of the most popular syntaxes in C++ and
has been designed to lay out all the basics for those who are early on in their
programming journey. For those who are more experienced, this article will provide
an overview of what is happening in the world of C++. We wish you a happy coding
journey!
Useful Resources
Practice C++ Programs Online
C++ MCQ with Answers
C vs C++
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions