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++ > Beginners Lab Assignments Code Examples

program to swap two numbers using built in swap function from c++ standard library

/* program to swap two numbers using built in swap function from c++ standard library What is swapping? It means exchange things with each other For more information have a look at swapping concept by Wikipedia Calling Methods: swap(); std:swap(); C++ code which take two numbers from user and swap using built in swap function. Program takes two input from user and displays numbers before and after swapping Note: If we remove std:: from swap at line number 20 than built in function will not run but The Function which has written above main will run If the function above the main is not written and we remove std:: from built in function than built in function will run When we call a function in a program compiler look into our code if compiler not found the function body which has called it look into standard library where he found the body it runs it. */ #include <iostream> using namespace std; void swap() { cout<<"this is my swap"; } int main() { int firstNum , secondNum; cout<<"Enter value for First Number: "; cin>>firstNum; cout<<"Enter value for Second Number: "; cin>>secondNum; cout<<"\n\n"; cout<<"Values BEFORE Calling Built in Swap Function"<<endl<<endl; cout<<"\tFirst Number = "<<firstNum<<endl; cout<<"\tSecond Number = "<<secondNum<<endl; cout<<"\n\n"; std::swap( firstNum , secondNum ); cout<<"Values AFTER Calling Bulit in Swap Function"<<endl<<endl; cout<<"\tFirst Number = "<<firstNum<<endl; cout<<"\tSecond Number = "<<secondNum<<endl; return 0; }

Exchange values of two objects. Exchanges the values of a and b. C++ Utility swap() function swaps or say interchanges the values of two containers under reference. The function std::swap() is a built-in function in the C++ Standard Template Library (STL) which swaps the value of two variables. This function does not return any value.

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.

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

The function in C++ language is also known as procedure or subroutine in other programming languages. To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability. Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check... Function declaration, is done to tell the compiler about the existence of the function. Function's return type, its name & parameter list is mentioned. Function body is written in its definition. Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them:

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.


To "delete element" from an array in C++, you have to first ask to the user to enter the array size then ask to enter the array element, ask to enter the element which is "to be deleted".