|
- On the main menu, click File -> New File...
- In the Create New File dialog box, set the Name to Inertia
- In the Type combo box, select h
- Click OK
|
|
|
|
|
|
|
|
- In all cases, replace the contents of the file with the following:
#ifndef _INERTIA_H_ #define _INERTIA_H_ double MomentOfInertia(const double b, const double h); double MomentOfInertia(const double R); double MomentOfInertia(const double b, const double h, const int); double GetBase(); double GetHeight(); double GetRadius(); #endif // INERTIA_H_ |
Source Files
|
A source file is used to actually define the behavior
of an object. This is also referred to as implementing the object.
The creation of a source file also depends on the programming environment. |
|
- On the main menu, click File -> New File...
- In the Create New File dialog box, set the Name to Inertia
- In the Type combo box, select cpp
- Click OK
|
|
|
|
|
|
|
|
|
|
- In all cases, replace the contents of the file with the following:
#include <iostream> using namespace std; #include "Inertia.h" // Rectangle double MomentOfInertia(const double b, const double h) { return b * h * h * h / 3; } // Semi-Circle double MomentOfInertia(const double r) { const double PI = 3.14159; return r * r * r * r * PI/ 8; } // Triangle double MomentOfInertia(const double b, const double h, const int) { return b * h * h * h / 12; } double GetBase() { double b; cout << "Base: "; cin >> b; return b; } double GetHeight() { double h; cout << "Height: "; cin >> h; return h; } double GetRadius() { double r; cout << "Radius: "; cin >> r; return r; } |
- To prepare the application for a test, in the Main.cpp file, type the
following:
#include <iostream> #include <string> using namespace std; #include "Inertia.h" void Announce(const string Figure) { cout << "Enter the dimensions of the " << Figure << "\n"; } int main() { double Length, Height, Radius; Announce("rectangle"); Length = GetBase(); Height = GetHeight(); cout << "Rectangle\n" << "Moment of inertia with regard to the X axis: "; cout << "I = " << MomentOfInertia(Length, Height) << "mm\n\n"; Announce("semi-circle"); Radius = GetRadius(); cout << "Semi-Circle\n" << "Moment of inertia with regard to the X axis: "; cout << "I = " << MomentOfInertia(Radius) << "mm\n\n"; Announce("triangle"); Length = GetBase(); Height = GetHeight(); cout << "\nTriangle\n" << "Moment of inertia with regard to the X axis: "; cout << "I = " << MomentOfInertia(Length, Height, 1) << "mm\n"; return 0; }
- Save the project and test it. Here is an example
Variable External Linkage
|
We saw that you could use different files in the same
program. In our header files, we declared only functions. In many cases,
you can also declare one or more variables in a (header) file and want to
access such a variable in another file. After declaring a variable in one
file, you can access it in any other file. This process is referred to as
external linkage.
To perform external linkage with a variable, in most
cases, there is not much to do: you simply declare and optionally
initialize a variable in one file, #include that file in another file, and
use the variable as you see fit. Consider the following example:
|
Header File: Exercise.h |
#ifndef Exercise_H
#define Exercise_H
int numberOfPagesInTheBook = 842;
#endif // Exercise_H
|
Source File: Exercise.cpp |
#ifdef __BORLANDC__
#pragma argsused
#endif
#include <iostream>
#include "Exercise.h"
using namespace std;
int main( int argc, char * argv[] )
{
cout << "This book contains " << numberOfPagesInTheBook << " pages";
return 0;
}
|
This would produce:
This book contains 842 pages
Imagine that, before accessing such a variable, you
declare another variable of the same data type and the same name. Consider
the following example:
|
Header File: Exercise.h |
#ifndef Exercise_H
#define Exercise_H
int numberOfPagesInTheBook = 842;
#endif // Exercise_H
|
Source File: Exercise.cpp |
#ifdef __BORLANDC__
#pragma argsused
#endif
#include <iostream>
#include "Exercise.h"
using namespace std;
int main( int argc, char * argv[] )
{
int numberOfPagesInTheBook;
cout << "This book contains " << numberOfPagesInTheBook << " pages";
return 0;
}
|
Since a file included, in this case Exercise.h, has a
variable with the same name as a locally declared variable, when you try
accessing it locally, the compiler would consider the local variable and
would ignore the external variable. The above code would produce:
This book contains 8856072 pages
(This result is from Borland C++BuilderX)
To indicate that the newly declared variable is simply
a reference to the external variable, when re-declaring it, start the
declaration with the extern keyword:
#ifdef __BORLANDC__
#pragma argsused
#endif
#include <iostream>
#include "Exercise.h"
using namespace std;
int main( int argc, char * argv[] )
{
extern int numberOfPagesInTheBook;
cout << "This book contains " << numberOfPagesInTheBook << " pages";
return 0;
}
This time, it is the external variable that will be
accessed.
|
References
|
Referenced Variables
|
A reference is a variable name that is a duplicate
of an existing variable. It provides a technique of creating more than
one name to designate the same variable. To declare a reference
variable, you use the reference operator expressed with the ampersand.
The syntax of creating or declaring a reference is:
DataType &RefernceName = VariableName;
To declare a reference, type the variable’s name
preceded by the same type as the variable it is referring to. Between
the data type and the reference name, type the ampersand operator
“&”. To specify what variable the reference is addressed to, use the
assignment operator “=” followed by the name of the variable. The
referred to variable must exist already. You cannot declare a reference
as:
int &mine;
The compiler must know what variable you are referring to. Here is an example:
#include <iostream>
using namespace std;
int main()
{
int number = 228;
int &nbr = number;
return 0;
}
The ampersand operator between the data type and the variable name can assume one of three positions as follows:
int& nbr; int & nbr; int &nbr;
As long as the & symbol is between a valid data type and a variable
name, the compiler knows that the variable name (in this case
Nbr) is a reference.
Once a reference has been initialized, it holds the same value as the variable it is
referring to. You can then display the value of the variable using either of both:
#include <iostream>
using namespace std;
int main()
{
int number = 228;
int & nbr = number;
cout << "Number = " << number << "\n";
cout << "Its reference = " << nbr << "\n\n";
return 0;
}
If you change the value of the variable, the compiler updates the value
of the reference so that both variables would hold the same value. In
the same way, you can modify the value of the reference, which would
update the value of the referred to variable. To access the reference,
do not use the ampersand operator; just the name of the reference is
sufficient to the compiler. This is illustrated in the following:
#include <iostream> using namespace std; int main() { int number = 228; // Regular variable int& nbr = number; // Reference cout << "Number = " << number << "\n"; cout << "Its reference = " << nbr << "\n"; // Changing the value of the original variable number = 4250; cout << "\nNumber = " << number; cout << "\nIts reference = " << nbr << "\n"; // Modifying the value of the reference Nbr = 38570; cout << "\nNumber = " << number; cout << "\nIts reference = " << nbr << "\n\n"; return 0; }
In the same way, you can use either a reference or the variable it is
referring to, to request the variable’s value from the user. Here is an
example:
#include <iostream> using namespace std; int main() { double price; double& refPrice = price; cout << "What's the price? $"; cin >> price; cout << "Price = $" << price << "\n"; cout << "Same as: $" << refPrice << "\n\n"; cout << "What's the price? $"; cin >> refPrice; cout << "Price = $" << price << "\n"; cout << "Same as: $" << refPrice << "\n"; return 0; }
If you change the value of the variable, the compiler updates the value
of the reference so that both variables would hold the same value. In
the same way, you can modify the value of the reference, which would
update the value of the referred to variable. To access the reference,
do not use the ampersand operator; just the name of the reference is
sufficient to the compiler. This is illustrated in the following:
#include <iostream> using namespace std; main() { int Number = 228; // Regular variable int& Nbr = Number; // Reference cout << "Number = " << Number << "\n"; cout << "Its reference = " << Nbr << "\n"; // Changing the value of the original variable Number = 4250; cout << "\nNumber = " << Number; cout << "\nIts reference = " << Nbr << "\n"; // Modifying the value of the reference Nbr = 38570; cout << "\nNumber = " << Number; cout << "\nIts reference = " << Nbr << "\n\n"; }
In the way, you can use either a reference or
the variable it is referring to, to request the variable’s value from
the user. Here is an example:
#include <iostream> using namespace std; main() { double Price; double& RefPrice = Price; cout << "What's the price? $"; cin >> Price; cout << "Price = $" << Price << "\n"; cout << "Same as: $" << RefPrice << "\n\n"; cout << "What's the price? $"; cin >> RefPrice; cout << "Price = $" << Price << "\n"; cout << "Same as: $" << RefPrice << "\n"; } |
Referenced Functions
|
A function can be made to return a reference to a
value. When defining such a function, make sure you type the reference
operator to its left. After processing the function in its body, you must
make sure you return a value that is a reference of the same type of the
function. Here is an example:
#ifdef __BORLANDC__
#pragma argsused
#endif
#include <iostream>
using namespace std;
int &GetNumberOfPages()
{
int pp = 842;
int &pages = pp;
return pages;
}
int main( int argc, char * argv[] )
{
cout << "This book contains " << GetNumberOfPages() << " pages";
return 0;
}
If the function is not modifying any value it may have
received, you can declare it as a constant:
#ifdef __BORLANDC__
#pragma argsused
#endif
#include <iostream>
using namespace std;
const int &GetNumberOfPages()
{
int pp = 842;
int &pages = pp;
return pages;
}
int main( int argc, char * argv[] )
{
cout << "This book contains " << GetNumberOfPages() << " pages";
return 0;
}
|
Static Variables and Functions
|
Static Variables
|
Consider the following program:
|
#include <iostream> using namespace std; void Starter(int y) { double a = 112.50; double b = 175.25; a = a / y; b = b + 2; cout << "y = " << y << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "b / a = " << b / a << "\n\n"; } int main() { Starter(2); Starter(2); Starter(2); Starter(2); return 0; }
When executed, this program would produce:
|
y = 2 a = 56.25 b = 177.25 b / a = 3.15111 y = 2 a = 56.25 b = 177.25 b / a = 3.15111 y = 2 a = 56.25 b = 177.25 b / a = 3.15111 y = 2 a = 56.25 b = 177.25 b / a = 3.15111
The Starter() function receives one argument passed
when it is called. The called function also receives the same argument every time.
Looking at the result, the argument passed to the function and the local
variables declared inside of the called function keep the same value every time
the function is called. That is, when the Starter() function is exited,
the values remain the same.
We know that, when a function is defined, any variable
declared locally belongs to the function and its influence cannot expand
beyond the body of the function. If you want a locally declared variable
to keep its changed value when its host function is exited, declare such a
variable as static.
To declare a static variable, type the static keyword
on the left of the variable’s data type. For example, if you plan to
declare a Radius variable as static in an Area() function, you could
write:
|
double Area() { static double Radius; }
When you declare a variable as static, it is initialized
with a 0 value. Otherwise, you can initialize it with a value of your
choice when declaring it. To make the local variables of our
Starter() function static, we can declare them as follows:
|
void Starter(int y) { static double a = 112.50; static double b = 175.25; a = a / y; b = b + 2; cout << "y = " << y << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "b / a = " << b / a << "\n\n"; }
This time, when executing the program, it would
produce:
|
y = 2 a = 56.25 b = 177.25 b / a = 3.15111 y = 2 a = 28.125 b = 179.25 b / a = 6.37333 y = 2 a = 14.0625 b = 181.25 b / a = 12.8889 y = 2 a = 7.03125 b = 183.25 b / a = 26.0622
Notice that, this time, each local variable keeps its
newly changed value when the function exits. Since a function’s argument
can receive different values as the function is called different times, we
can test our program by passing different values to its argument as
follows:
|
#include <iostream> using namespace std; void Starter(int y) { static double a = 112.50; static double b = 175.25; a = a / y; b = b + 2; cout << "y = " << y << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "b / a = " << b / a << "\n\n"; } int main() { Starter(2); Starter(5); Starter(14); Starter(25); return 0; }
The current version of the program would
produce:
|
y = 2 a = 56.25 b = 177.25 b / a = 3.15111 y = 5 a = 11.25 b = 179.25 b / a = 15.9333 y = 14 a = 0.803571 b = 181.25 b / a = 225.556 y = 25 a = 0.0321429 b = 183.25 b / a = 5701.11
Static Functions
|
Like a variable, a function also can be declared
and/or defined as static. Here is an example:
#ifdef __BORLANDC__ #pragma argsused #endif #include <iostream> // #include "Exercise.h" using namespace std; static int GetNumberOfPages() { int pages = 842; return pages; } int main( int argc, char * argv[] ) { cout << "This book contains " << GetNumberOfPages() << " pages"; return 0; }
No comments:
Post a Comment