Function Specifiers
Specifiers in C++ - Part 5
Forward: In this part of the series, I explain function specifiers in C++.
By: Chrysanthus Date Published: 25 Aug 2012
Introduction
This is part 5 of my series, Specifiers in C++. In this part of the series, I explain function specifiers in C++. I assume you have read the previous part of the series, because this is a continuation. A function specifier is normally typed in front of the function declaration (interface).
The inline Specifier
You can precede the declaration of a function with the specifier, inline. The inline specifier indicates to the compiler that inline substitution of the function body (definition) at the point of call is to be preferred. The following program illustrates this:
#include <iostream>
using namespace std;
inline void fn();
int main()
{
fn();
return 0;
}
void fn()
{
cout << "Yes";
}
The virtual Specifier
The virtual specifier can be used with class member functions (see below).
An Abstract Base Class
An abstract base class is a class with what is known as a pure virtual function. A pure virtual function is a function (method) that does not have an implementation (that is, it does not have a definition; in other words it does not have a function body in curly braces). Well, you need to know how to produce this virtual function. The typing has a declaration that is preceded by the keyword, virtual; to the declaration is assigned the value zero. The following code illustrates the description of an abstract class that has a pure virtual function.
#include <iostream>
using namespace std;
class MyClass
{
public:
virtual int mthd() = 0;
};
int main()
{
return 0;
}
The class has just one function; it is the virtual function. Note the way it has been typed. It begins with the word, virtual; a null address has been assigned to the declaration within the class description. Any class with a pure virtual function is called an abstract base class. In future, you can inherit other classes from this class and in the inherited (derived) classes, you will give the method its implementation.
In the following code the base class is an abstract base class.
#include <iostream>
using namespace std;
class Calculator
{
public:
int num1;
int num2;
int add()
{
int sum = num1 + num2;
return sum;
}
virtual int mthd() = 0;
};
class ChildCalculator: public Calculator
{
public:
int fixedVal;
int square(int answer)
{
int finalVal = answer * answer + fixedVal;
return finalVal;
}
int mthd()
{
cout<<"I have been implemented in the derived class.";
}
};
int main()
{
ChildCalculator myChildObj;
myChildObj.mthd();
return 0;
}
Note: you cannot instantiate a class from a base abstract class, because it has one or more functions that is (are) virtual. You can instantiate an object from the corresponding derived class, where the virtual functions (methods) have been implemented.
For an inline function, the implementation is given at the point of call; the inline function is not necessarily a class member. For a virtual function, the implementation is given in an inherited class, and the virtual function has to be a class member of a base class.
The explicit Specifier
See later!
Well, I have explained the meaning of some function specifiers. Let us stop here and continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT