C++ -->
- Prev. C with Classes
- Mother of All Language ( C/C++ )
- Fast
- Middle Level Language
- Embed System Language
Used in -->
- Advanced Graphics
- Embedded Systems
- Video Games
Input and Output :- Let's see a program first then we'll understand each line of code in detail.
1 #include<iostream>
2 using namespace std;
3
4 int main(){
5 int age ;
6
7 cout << "Enter Your Age : ";
8 cin >> age ;
9
10 cout << "WOW! You are " << age << " Years Old." << endl;
11
12 return 0;
13 }
"Don't Worry if you won't understand any of these things , We'll talk about them in depth later on this Repo"
In 1st line , we have included a header file for functionalities to work properly . If we don't add header file in C/C++ , the whole code meant nothing .
In 2nd line , we have added a standard namespace which consist of all important and really usefull funtionalities.
In 4th line , we have created a main function having int as its's return-type .
In 5th line , there is a variable declared of int data type.
In 7th line , we used the standard output of C++ . The Text Written in between the Quotation marks " " will be displayed upon the screen.
In 8th line , we used standard input of C++ . We can store data provided by user in Variables using this .
In 10th line , we have binded up everything and printed them.
In 12th line , we have return-type for the main funtion and the parenthesis for ending a function.
Variables are containers for storing data values. Variables in C++ is a name given to a memory location. It is the basic unit of storage in a program.
The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location. In C++, all the variables must be declared before use.
syntax :-
// Declaring a single variable
type variable_name;
// Declaring a variable with value
type var_name = value;
// Declaring multiple variables:
type var1_name, var2_name, var3_name;
// Declaring multiple variable with value
type var1 = value1,var2 = value2;
Data Types
- int --> Stores integers -- 2 or 4 bytes
- char --> Stores single characters -- 1 byte
- bool --> Stores True or False / 1 or 0 -- 1 byte
- float --> Floating Point Number , 6 or 7 Decimal Places -- 4 bytes
- double --> Stores floating point numbers , 15 Decimal Places -- 8 bytes
- string --> Sentence or couple of words , stores text -- Changes Accordingly
const keyword - Used to Convert Value of any Variable Read Only , It can't be modified once assigned. If using const keyword , use all capitals for Naming of Constant Variable (It Increases in readability of Code)
Provides a Solution for Preventing name conflicts in large projects. Each Entity needs a unique name . A namespace allows for identically named entities as long as the namespaes are different.
We use "using namespace std;" to get rid of writing "std::" before each statement like "std::cout" , "std::string"
We can also create Custom namespace to use them in our program
syntax :-
namespace name_of_namespace {
// value of Variables
// EG :
int x = 5;
std::string Jai = "Jai Mata Di";
}
that can be used in place of type specifiers such as int , float , and double , etc.
syntax :-
typedef <current_name> <new_name>;
We must use "_t" or "_type" as suffix for the typedef variables.
Example :-
typedef std::string str_t;
using keyword is also used for aliasing of DataTypes.
syntax :-
using <new_name> = <current_name>;
Example :-
using str = std::string;
Operators in C++ are divided into following groups :
- " + " Addition --> Adds together two values --> x + y
- " - " Subtraction --> Subtracts one value from another --> x - y
- " * " Multiplication --> Multiplies two values --> x * y
- " / " Division --> Divides one value by another --> x / y
- " % " Modulus --> Returns the division remainder --> x % y
- " ++ " Increment --> Increases the value of a variable by 1 --> ++x
- " -- " Decrement --> Decreases the value of a variable by 1 --> --x
This is important in programming, because it helps us to find answers and make decisions. The return value of a comparison is either 1 or 0, which means true (1) or false (0).
- " == " -- Equal to --> x == y
- " != " -- Not equal --> x != y
- " > " -- Greater than --> x > y
- " < " -- Less than --> x < y
- " >= " -- Greater than or equal to --> x >= y
- " <= " -- Less than or equal to --> x <= y
As with comparison operators, you can also test for true (1) or false (0) values with logical operators.
- " && " Logical AND --> Returns true if both statements are true --> x < 5 && x < 10
- " || " Logical OR --> Returns true if one of the statements is true --> x < 5 || x < 4
- " ! " Logical NOT --> Reverse the result, returns false if the result is true --> !(x < 5 && x < 10)
Bitwise operators :- Bitwise operators perform operations on integer data at the individual bit-level.
These operations include testing, setting, or shifting the actual bits.
& Bitwise AND Operator
| Bitwise OR Operator
^ Bitwise XOR Operator
~ Bitwise Complement Operator
<< Bitwise Shift Left Operator
>> Bitwise Shift Right Operator
Type Conversion :- Conversion of a value from one type to another .
- Implicit --> Automatic by Compiler
- Explicit --> Precode value with new data type (Done by Programmer)
There are three types of conditional statements exists in CPP Language.
- If-Else statement
- Switch statement
if(condition){
// Statements to execute if
// condition is true
}
Functions are block of code which can be reused many times.
Declaration :-
<return_type> <Function_name>(parameters);
Definition :-
<return-type> <Function-name>(parameters if any){
//block of code
}
Function must be declared before main and can be defined after main or before main() fun , we can both declare and define function at the same time.
Example :-
/* Function Example 1 :
Print Namaste for India and Bonjour for French*/
#include<iostream>
using namespace std;
void Greet(int n); // Function Declaration before main()
// Function Decration and Definition at same time
void GoodBye( ){
cout << "Good Bye ! " << endl>>;
}
int main( ){ // Calling Main Function
cout << "Enter 1 for Indian " << endl;
cout << "Enter 2 for French " << endl;
printf("Enter Your Choice : ");
cin >> n;
Greet(n); // Calling Greet() Function
GoodBye(); // Calling GoodBye() Function
return 0;
}
// this function has a parameter of int data-type
void Greet(int n){ // Function Definition
if (n == 1){
printf("Namaste India");
} else if (n == 2) {
printf("Bonjour");
} else{
printf("Invalid Input");
}
}
-
Higher Level language - Easy to write and Understand as a normal human and Slower . Python , JS , Java , C# , Ruby , etc.
-
Middle Level Language - Comparatively not easy to write and understand but Faster in Speed . C , C++ , Oberon , Pascal , etc.
-
Low Level Language - Not Possible to write and understand cause this was written in Binary Form . Assembly Language or Machine Language