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

Write a program which calculates addition of two numbers using pointers.

/* Write a program which calculates addition of two numbers using pointers. Program has three functions which receives two pointers reference. Three functions returns int, float and double sum of numbers. This c++ tutorial use the following concepts. */ pointers in c++ functions passing reference to function C++ source code #include <iostream> using namespace std; int Sum(int *, int *); float Sum1(int *,int *); double Sum2(int *, int *); int main() { int a,b; cout<<"Enter 1st Value:"; cin>>a; cout<<"Enter 2nd Value:"; cin>>b; cout<<"...............addition.............." <<endl <<endl; cout<<"The integer sum of these numbers is:"<<Sum(&a,&b)<<endl; cout<<"The float sum of these numbers is:"<<Sum1(&a,&b)<<endl; cout<<"The double sum of these numbers is:"<<Sum2(&a,&b)<<endl; cout<<endl <<endl; cout<<endl <<endl; return 0; } int Sum(int *x, int *y ) { int sum; sum=(*x + *y); return sum; } float Sum1(int *x,int *y ) { float sum; sum=(*x + *y); return sum; } double Sum2(int *x, int *y ) { double sum; sum=(*x + *y); return sum; }

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.

#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.

A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.

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.

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.

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.

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.

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.



This is a C++ Program to check whether tree is Subtree of another tree. Given two binary trees, check if the first tree is subtree of the second one. A subtree of a tree T is a tree S

Perform addition, subtraction, multiplication and division of any two numbers in 'C++', you have to enter the 'two number' and enter the operator to perform the particular operation

Genereate first "Hash" and genereate second hash. Initialize table and "Find" element from the table. Function to insert element into the table. Function to rehash the table. Retrieve





Merge-Sort is based on an algorithmic design pattern called Divide & Conquer. It forms tree structure. The height of the tree will be log(n) And we merge n element at every level of the

Program sample, using a stack data strucure, computing whether the given "Parantheses" expression is 'valid' or not by check whether each parentheses is closed and nested in the