Functions and Namespaces
|
Functions Local Definition
|
Like a variable, a function can be part of a
namespace. To declare a function in a namespace, provide its return type,
followed by a name, followed by the argument(s), if any, inside of
parentheses. Here is an example:
|
namespace InterestAndDiscount { double Principal; double Rate; int Time; double CalculateDiscount(); double CalculateInterest(); double CalculateMaturity(); }
A member function of a namespace can be accessed using
the scope access operator.
There are two main ways you can implement a member function. In the body
of the namespace, which is a local implementation, delimit the body of the
function with an opening curly bracket “{“ and a closing curly bracket
“}”. A function that is a member of a namespace has complete access to
the member variables of the same namespace. Therefore, you do not have to
pass the member variables as arguments to the member functions. Here is an
example:
|
#include <iostream> using namespace std; namespace InterestAndDiscount { double Principal; double Rate; int Time; double GetInterestRate() { return Rate / 100; } double CalculateInterest() { return Principal * GetInterestRate() * Time; } double CalculateMaturity() { return Principal + CalculateInterest(); } } int main() { using namespace InterestAndDiscount; Principal = 12500; // $ Rate = 12.25; // % Time = 4; // Years cout << "Interest Calculation"; cout << "\nPrincipal: $" << Principal << "\nRate: " << Rate << "%" << "\nTime: " << Time << " years" << "\nInterest: $" << CalculateInterest() << "\nMaturity: $" << CalculateMaturity() << "\n\n"; return 0; } This would produce: Interest Calculation Principal: $12500 Rate: 12.25% Time: 4 years Interest: $6125 Maturity: $18625
If a nested namespace has its own functions, you can
also implement them in the body of the nested namespace. Here is an
example:
|
namespace InterestAndDiscount { double Principal; double Rate; int Time; double GetInterestRate() { return Rate / 100; } double CalculateInterest() { return Principal * GetInterestRate() * Time; } double CalculateMaturity() { return Principal + CalculateInterest(); } namespace Discounter { double Maturity; double DiscountRate; double TermOfDiscount; double Discount() { return Maturity * DiscountRate * TermOfDiscount; } } }
After locally implementing the member functions of a
nested namespace, you can access its members and display their value in
the main() function as done above.
|
Functions Global Definitions
|
To implement a member function outside the body of a
namespace, provide the return type, followed by the name of the namespace,
followed by the scope access operator “::”. Here is an example:
|
namespace InterestAndDiscount { double Principal; double Rate; int Time; double GetInterestRate(); double CalculateInterest(); double CalculateMaturity(); } InterestAndDiscount::GetInterestRate() { return Rate / 100; } InterestAndDiscount::CalculateInterest() { return Principal * GetInterestRate() * Time; } InterestAndDiscount::CalculateMaturity() { return Principal + CalculateInterest(); }
To implement the member functions of a nested
namespace outside of the parent namespace, you must qualify each member
function to specify the function (or the variable) you are calling. Here
is an example:
|
namespace InterestAndDiscount { double Principal; double Rate; int Time; double GetInterestRate(); double CalculateInterest(); double CalculateMaturity() namespace Discounter { double Maturity; double DiscountRate; double TermOfDiscount; double Discount(); } } . . . InterestAndDiscount::Discounter::Discount() { return Maturity * DiscountRate * TermOfDiscount; }
Namespaces and External Functions
|
The member variables of a namespace are variables like
any of those we have used so far. They can request their values from an
outside function. Here is an example:
|
#include <iostream> using namespace std; namespace InterestAndDiscount { . . . } . . . int main() { using namespace InterestAndDiscount; double GetThePrincipal(); cout << "Loan Processing\n"; cout << "Enter the following values\n"; Principal = GetThePrincipal(); cout << "Rate (between 0 and 100): "; cin >> Rate; cout << "Time (number of years): "; cin >> Time; cout << "\nInterest on a loan"; cout << "\nPrincipal: $" << Principal; cout << "\nRate: " << Rate << "%"; cout << "\nTime: " << Time << " years"; cout << "\nInterest: $" << CalcInterest(); cout << "\nMaturity Value: $" << CalcMaturityValue(); return 0; } double GetThePrincipal() { double P; cout << "Principal: $"; cin >> P; return P; }
The member variable of a namespace can also be passed
as argument to a function. When passing the argument, if the using
namespace routine has been entered, you can pass the argument like any
other. Otherwise, you should qualify the namespace member with the ::
operator. In the following example, one member of a namespace is passed by
its name only because of the previous using namespace. The other members
are passed by being qualified, which is for demonstration purposes only:
|
#include <iostream> using namespace std; namespace InterestAndDiscount { . . . } . . . int main() { using namespace InterestAndDiscount; void GetThePrincipal(double& p); void RateAndTime(double &r, double &t); cout << "Loan Processing"; cout << "\nEnter the following values\n"; GetThePrincipal(Principal); RateAndTime(InterestAndDiscount::Rate, InterestAndDiscount::Time); cout << setiosflags(ios::fixed) << setprecision(2); cout << "\nInterest on a loan"; cout << "\nPrincipal: $" << Principal; cout << "\nRate: " << Rate << "%"; cout << setiosflags(ios::fixed) << setprecision(0); cout << "\nTime: " << Time << " years"; cout << setiosflags(ios::fixed) << setprecision(2); cout << "\nInterest: $" << CalcInterest(); cout << "\nMaturity Value: $" << CalcMaturityValue(); return 0; } void GetThePrincipal(double& P) { cout << "Principal: $"; cin >> P; while( P < 0 ) { cout << "Enter a positive number: $"; cin >> P; } } void RateAndTime(double &rate, double &time) { do { cout << "Rate (between 0 and 100): "; cin >> rate; } while(rate < 0 || rate > 100); do { cout << "Time (Nbr of Years): "; cin >> time; } while(time <= 0 || time >= 30); }
C++ Built-in Functions
|
Introduction
|
Although as a smart programmer you can create any
function to perform a desired job, the C++ language provides a series of
functions already made so you can just add them to your program without
caring how they work, all you need to know is what these functions do. The
functions that are part of the C++ language are highly valuable, were
tested sufficiently, and are completely reliable. The C++ built-in
functions are made for various assignments ranging from algebra, geometry,
trigonometry, and finance, etc. Besides the functions that are part of the
C++ Standard, each compiler ships with its own set of functions that may
not be available on other compilers. Borland C++ Builder provides an
extremely rich library of functions.
|
Asserting a Value or an Expression
|
Most of the values you use in your program will need
to fit in an interval of your choice. For example, when requesting the age
of a person, you would need such a value to be positive. After all, you do
not expect a person to be –26 years old. C++ provides a function that
can be used to check that a value or expression responds to a criteria of
your choice. This function is called assert() and is defined in the
cassert library of the std namespace. Its syntax is:
|
void assert(int Expression);
The assert() function considers an expression as its
argument and tests it. This function is used in the same context as the
conditional statements we will study in the next lesson of this book. If
the Expression is true, assert() acknowledges that and lets the compiler
continue with the next operation. If the Expression is false, assert() displays a (nasty) message. Although we have not yet learned
conditional statements, the following is an example that requests the age
of a student and checks that the supplied age is valid only if the student
is older than 8:
|
#include <iostream> #include <cassert> using namespace std; int main() { float StudentAge; cout << "Type Student's Age: "; cin >> StudentAge; assert(StudentAge > 8); cout << "Student Age: " << StudentAge << "\n\n"; return 0; }
Mathematic Functions
|
The C++ language also provides a series of functions
to perform various mathematically related operations. The functions are
defined in various libraries and this can depend on the compiler you are
using.
The functions defined in the cmath library are:
The functions defined in the cmath library are:
acos | cos | fmod | modf | tan |
asin | cosh | frexp | pow | tanh |
atan | exp | ldexp | sin | |
atan2 | fabs | log | sinh | |
ceil | floor | log10 | sqrt |
Additional functions are defined in the cstdlib
library and they are:
abs | labs | srand |
div | ldiv | rand |
No comments:
Post a Comment