C++ Programming Code Examples
C++ > Code Snippets Code Examples
Returning a Reference to a Pointer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Returning a Reference to a Pointer
Instead of returning a pointer or a reference,
you can return a reference to a pointer.
To declare such a function, precede its name with * and &.
Here is an example: */
double *& ShowNumber()
{
}
/* In the body of the function, you can do whatever is appropriate.
An important rule with this type of function is that it must return either a global or a static variable.
In other words, the variable that is returned must conserve its value when the function exits.
Here is an example that returns a static variable: */
double *& ShowNumber()
{
double n = 1550.85;
static double *v = &n;
return v;
}
/* When calling the function, precede its name with an asterisk. Here is an example: */
//---------------------------------------------------------------------------
#include <iostream>
using namespace std;
double *& ShowNumber()
{
double n = 1550.85;
static double *v = &n;
return v;
}
//---------------------------------------------------------------------------
int main()
{
double Number = *ShowNumber();
cout << "Number: " << Number << endl;
return 0;
}
//---------------------------------------------------------------------------
This would produce:
Number: 1550.85
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.
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.
Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. In C++, static is a keyword or modifier that belongs to the type not instance. So instance is not required to access the static members. In C++, static can be field, method, constructor, class, properties, operator and event. Advantage of C++ static keyword: Memory efficient. Now we don't need to create instance for accessing the static members, so it saves memory. Moreover, it belongs to the type, so it will not get memory each time when instance is created.
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.
#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 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.
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.
Class for addition, subtraction, multiplication and division for 'complex numbers'. Class has 4 functions to perform arithmetic operations. It takes 2 "complex numbers" input from user
First think what is the factorial of a number? How 'mathematically' it can be calculated. If you got this info then it will be very easier to make a C++ code logic to find the "Factorial".
Find all 'factors' of an integer using for loop & if statement. Takes a positive integer from an user & displays all the factors of that number. In this program, an integer entered by user is