Posts

Showing posts from January, 2021

Return By Value, Return By Pointer, Return By Reference

 Return By Value, Return By Pointer, Return By Reference Return by value: In this the function returns some kind of value like the variable, it is the most simplest way to return the value. remember it will return a copy of the variable, and it also doesn't affect the scope of the variable. int * myFunction() { . . . } Example: int doubleValue(int x) { int value = x * 2; return value; // A copy of value will be returned here } Return by Pointer/Address: return by pointer also known as return by address. in this type the address of the variable is returned. return by address can only return the address of a variable, not a literal or an expression (which don’t have addresses). if you try to return the address of a variable local to the function, your program will exhibit undefined behavior. Consider the following example: int* doubleValue(int x) { int value = x * 2; return &value; // return value by address here } in the above example the scope of the va

call by value, pointer, reference

 Call By Value, Pointer, References Here in this post, we have discussed the different parameter which we are pass in our user-defined function. The above all parameters are used for different situations. Call by value, call by reference, and call by pointer are three basic parameters which we often use in our function. Note : If we don't change our value and only we want to change its duplicate value then we only pass the call by value parameter in our functions. And if we want to change the original value of our data then we will pass by the pointer or pass by the references.   Call By Value Call By Reference While calling a function, we pass the copy of the values of variables to it. Such functions are known as “Call By Values”. While calling a function, instead of passing the values of variables, we pass the address of variables(location of variables) to the function known as “Call By References. In this method, the value of each variable in the calling function is copied into

Control Statements

 Control Statements (if, if- else, switch statement, loops)

Scope Resolution Operator

Scope Resolution Operator Scope Resolution Operator is used in C++. Scope Resolution Operator is used for different purposes for different reasons. There are many purposes like avoiding the name conflict, Access the global variable, and other different use. scope resolution operator is denoted by _::__. Now we will see one by one the different purposes of the scope resolution operator in oop. scope resolution operator to access global variables when the local and global variables have the same name. #include<iostream>   using namespace std;      int x;  // Global x      int main()  {    int x = 10; // Local x    cout << "The value of a global variable of the x is " << ::x;    cout << "\nThe value of a local variable of the x is " << x;      return 0;  }  Output : The value of a global variable of the x is 0. The value of a local variable of the x is 10. Scope Resolution Operator and Functions have some relation in oops. This operator

Implicit And Explicit Type Conversion

Image
Implicit And Explicit Type Conversion The type conversion is the conversion of a variable data type from one to another. suppose we are adding two numbers one integer and the other floating point number for Example:- 2 + 3.5, the answer will be 5.5, what we have done here is we know that 2 can also be written as 2.0 and then we perform the addition with 3.5 and we get the answer 5.5.  The same thing happens in the C++, the compiler knows that 2 which is integer and the 3.5 which is floating point number so it will automatically convert the 2 which is an integer type into float and then perform the addition.  This is known as implicit type conversion,  the implicit type conversion is the conversion that takes place automatically by the compiler. You might be thinking that why does the compiler not convert the number 3.5 in to floating point number? write. in the above example you might not have the question 🙋 , but in the following case you would definitely have the question, 2.0 + 1 i

Scope Resolution Operator

 Scope Resolution Operator Blocks and scopes are often used in the programming languages. we also know that the same variable names can be used to have different meaning in different blocks. the scope of the variable is from the point of the deceleration to the end of the block containing the deceleration, a variable declared in a block is said as local to that block. For example. ... { x = 10; ...    ... } ... ... { int x = 20; ... ... } In the above two blocks the x declared are two different variables at different memory location, we can not access the the variable outside the block or the other variable in side the different block. The Scope resolution operator is used for many other purpose, which are listed below: For accessing the global variable when there is a local variable with the same name.  Defining the function outside the class. Accessing a class’s static variables. Referring to a class inside another class. In case of multiple Inheritance. N

Reference Variable In C++

 Reference variable in C++ A reference variable is an alias to the existing variable, or it is just the other name to the variable. the reference variable points to the same memory address as the variable. any change done using the reference variable will change the original variable. we can use any of them to refer to the variable. To understand this concept think that a variable is labeled by the name, let's say 'i' in the memory location, the reference variable is just the second label to that memory location. so we can access the value stored in that variable using the original variable or the reference variable. for Example: int i = 17; int& reference = i; Here the & is not read as as address or ampersand it is read as reference. here is an example how the reference variable works. #include <iostream> using namespace std ; int main () { // declare simple variable int i ; // declare reference variable int & r = i ;

Datatypes In C++

Image
 Datatypes in C++ Datatypes determines the type and size of the variable. the below image shows the different types of the datatypes in C++. The primitive datatypes also known as Built-in Datatypes are the basic datatypes, Integer (int): As its name suggests this type stores integer values,  its size is 2 or 4 bytes. depending upon the architecture of the computer. Float (float) & Double (double): Again the name suggests, this type store floating point values, the size of the float is 4 bytes, and the size of the double is 8 bytes. Character (char): this type stores the single character, they are enclosed in single quotes (' '). the size of the char is 1 byte. apart from the above four datatypes two more datatypes boolean (bool), and wide character constant (wchar_t) were added in C++. Wide Character constant (wchar_t): Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1. It is used to represent characters that require more memor

The Main Function, The Heart Of The Program

 Main Function In C++ Program : The Heart Of The Program. Here we are  discussed about the main function. Which are very important topic and we will also see about how we use main function with various method. How we use main function in C++ with arguments  and what's the benefits of the use of   main method in C++ First question is why  do you need a main function in C++ ? The main Function Declaration :   We don't declare the main function. The main function is inbuilt function into the specific language. In every C++ program we must write the program with the main function. If we write the program the compiler gives an error. Compiler gives this type of the error like  (Dynamic-link libraries and static libraries don't have a  main  function.) that's why we said that the main function is the heart of the every program. The main function is where your source code begin with the execution.  Before the execution of the source code we check there all of the member are d

Operators in C++

 Operators in C++ All the C operators are valid in C++ also. C++ introduce some new operators like insertion operator (<<), extraction operator (>>), and scope resolution operator (::) etc. some of thee new operator added to C in C++ are: ::*      pointer-to-member declarator ->*      pointer-to-member operator .*      pointer-to-member operator delete     memory release operator new     memory allocation operator endl     line feed operator setw     field width operator the different types of the operators which are common in C and C++ are as follows: Arithmetic operator (+, -, *, /, %) Logical operator (&&, ||, !) Relational operator (<, <=, >, >=, ==, !=) Assignment operator (=, +=, -=, *=, /=, %=) Conditional operator (expression ? expression : expression) Bitwise operator (&, |, ^, ~, <<, >>) in C++ we can also add our own meaning to the existing operators depending upon the types of argument used, this process is known as operato