C++ Programming Code Examples
C++ > Beginners Lab Assignments Code Examples
Function Overloading
/* Function Overloading
If a C++ class have multiple member functions, having the same name but different parameters (with a change in type, sequence or number), and programmers can use them to perform a similar form of operations, then it is known as function overloading. In this case, two functions can have same identifier (name) and if either number of arguments or type of arguments passed to functions are different; then overloading is possible within a C++ program. In other words, it is the ability to create multiple functions with same name and slightly different implementation and depending on the context (type, sequence, and a number of the value passed), the appropriate function gets invoked. */
#include <iostream>
using namespace std;
long add(long, long);
float add(float, float);
int main()
{
long a, b, c;
float e, f, g;
cout << "Enter two integers\n";
cin >> a >> b;
c = add(a, b);
cout << "Sum of integers: " << c << endl;
cout << "Enter two floating point numbers\n";
cin >> e >> f;
g = add(e, f);
cout << "Sum of floats: " << g << endl;
}
long add(long c, long g)
{
long sum;
sum = c + g;
return sum;
}
float add(float c, float g)
{
float sum;
sum = c + g;
return sum;
}
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 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 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.
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.
#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.
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.
Increments ++ & Decrements -- operator are overloaded in best possible way, increase the value of a data member by 1 if "++ operator" operates on an object and decrease value of
A function is like a black box. It takes in input, does something with it, and then spits out an answer. We have some "terminology" to refer to functions: A function, call it f, and that uses
The "main() function" is called when the c++ program starts after initialization of the non-local objects with static storage duration. It's the primary entry point of any C++ Program
Implement the binary search to find a peak in the array. If the 'middle element' is more than its both Neighbors, 'it is the Peak'. Otherwise, split the array and check the same. A function