C++ Programming Code Examples
C++ > Code Snippets Code Examples
Variable Scope Example
/* Variable Scope Example */
#include <iostream>
using namespace std;
int subtract (int a, int b);
int global = 5;
int main(void)
{
int a, b;
a = 5;
b = 3;
cout << "The value of main's a is: " << a << endl
<< "The value of main's b is: " << b << endl
<< "The value of global is: " << global << endl;
global = 2 + subtract(a,b);
cout << "The value of main's a now is: " << a << endl
<< "The value of global now is: " << global << endl;
return 0;
}
int subtract(int a, int b)
{
cout << "The value of subtract's a is: " << a << endl
<< "The value of subtract's b is: " << b << endl;
a = a - b + global;
return a;
}
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 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.
Arithmetic Operator is used to performing mathematical operations such as addition, subtraction, multiplication, division, modulus, etc., on the given operands. For example: 6 + 3 = 9, 5 - 3 = 2, 3 * 4 = 12, etc. are the examples of arithmetic operators. Let's discuss the different types of Arithmetic Operators in the C programming. Plus Operator is a simple Plus (+) Operator used to add two given operands. We can use Plus Operator with different data types such as integer, float, long, double, enumerated and string type data to add the given operand. The minus operator is denoted by the minus (-) symbol. It is used to return the subtraction of the first number from the second number. The data type of the given number can be different types, such as int, float, double, long double, etc., in the programing language.
#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.
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 shutdown and restart you computer using C++, just call the function system() of stdlib.h library which will call the cmd or terminal. So to shutdown and 'restart your computer', just
Since Floyd's triangle is right 'angled-triangle' formed by the natural numbers. C++ program ask you to enter the range to print the Floyd's Triangle upto the 'given range'. First enter the
You have to ask to the user to enter array size array elements to store all the array elements in one dimensional and then print the array in "one dimension" using one for loop as shown
C++ Program should have two functions one which takes radius as parameter and second takes diameter as parameter. If enters wrong option then tell users its 'valid selection' and