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++ > Code Snippets Code Examples

Absolute Value Calculation

/* Absolute Value Calculation This is a simple function that calculates the absolute value of a number: */ #include <iostream> using namespace std; double Abs(double Nbr) { // return (Nbr >= 0) ? Nbr : -Nbr; if( Nbr >= 0 ) return Nbr; else return -Nbr; } int main() { double Number = -88; double Nbr = Abs(Number); cout << "The absolute value of " << Number << " is " << Nbr << endl; return 0; } Here is an example of running the program: The absolute value of -88 is 88

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.

Absolute value. Returns the absolute value of parameter n ( /n/ ). In C++, this function is also overloaded in header <cmath> for floating-point types (see cmath abs), in header <complex> for complex numbers (see complex abs), and in header <valarray> for valarrays (see valarray abs). Basically the abs function evaluates the absolute value of the given value i.e. value after removing all the signs of negative and positive from the number. Which means it will always return a positive number. Function returns the absolute value of n. abs() function - In C the input is of type 'int' whereas in C++ input is of type 'int, long int or long long int'. In C the output is of 'int' type and in C++ the output has the same data type as 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.

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.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

The C++ comments are statements that are not executed by the compiler. The comments in C++ programming can be used to provide explanation of the code, variable, method or class. If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code. By the help of comments, you can hide the program code also. There are two types of comments in C++: • Single Line comment. • Multi Line comment

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



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


This C++ Program checks whether a directed graph is weakly connected or not. We can do "DFS" V times starting from every vertex. If any DFS, doesn't visit all vertices, then graph

C++ Language code to compute the area of a triangle using determinants. The 'plus/minus' in this case is meant to take whichever sign is needed so the 'answer is positive'. Do not say


A C++ program starts its execution from this method "main". Wherever this main method is, the main method will be executed first. '()' is used at the end as main is a method. Main

We have two classes XYZ and ABC. The XYZ class has two "private data members" ch and num, this class declares 'ABC' as Friend Class. This means that 'ABC' can access the private

In C++ programming, you have to ask to the user to enter the desired decimal number to convert it into hexadecimal number to print the equivalent value in hexadecimal format