Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Pointers Code Examples

Simple Program for Swap Numbers Using Pointers In C++

/* Simple Program for Swap Numbers Using Pointers In C++ Swap Definition In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables. Usually, this is done with the data in memory Using a temporary variable The simplest and probably most widely used method to swap two variables is to use a third temporary variable: */ /*##Simple Example Program for Swap Numbers Using Pointers In C++*/ /*##Simple swap Programs, pointers Example C++ Programming*/ /*##Pass By Referense Function Example Program In C++ Programming*/ // Header Files #include <iostream> #include<conio.h> using namespace std; // Declare Swap Function Using Pointer void swap_numbers(int *value1, int *value2) { int temp; temp = *value1; *value1 = *value2; *value2 = temp; } int main() { // Declare Variables int number1, number2; cout << "Simple Example Program for Swap Numbers Using Pointers In C++\n"; // Read User Input cout << "Enter value of Swap Number # 1: "; cin>>number1; cout << "Enter value of Swap Number # 2: "; cin>>number2; //Print Values before Swapping cout << "Before Swapping : Number # 1=" << number1 << ", Number # 2=" << number2 << "\n"; //Call Swap Function By Passing Reference swap_numbers(&number1, &number2); //Print Values after Swapping cout << "After Swapping : Number # 1=" << number1 << ", Number # 2=" << number2 << "\n"; getch(); return 0; }

The pointer in C++ language is a variable, it is also known as locator or indicator that points to an address of a value. In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type. You should see a pointer as a symbolic representation of a memory address. With pointers, programs can simulate call-by-reference. They can also create and manipulate dynamic data structures. In C++, a pointer variable refers to a variable pointing to a specific address in a memory pointed by another variable.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.


Example of using this 'pointer' is to return the "Reference of current object" so that you can chain "function calls", this way you can call all the functions for the current object in one go.




To calculate "area and circumference" of any circle in "C++", you have to ask to the user to enter the radius of circle, place the radius in a variable say r and make two variable, one for




C++ Program to find the trnasitive closure of a given graph. In mathematics, the transitive closure of a binary relation R on a set X is the "transitive relation" R+ on set X such that R+