Function Pointers and Classes |
Introduction |
In our introduction to function pointers (or pointer to function, however you want to call them), we saw various ways you can use a callback function. The C++ language goes further by involving classes. The idea is to let you use function pointers as regular members of a class. Here is an example of a class and its test: |
Header File: polygon.h |
|
#pragma once public __gc class CPolygon { public: CPolygon(void); virtual ~CPolygon(void); private: double Rad; public: double getRadius(void); void setRadius(double R); }; |
|
Source Code: polygon.cpp |
|
#include "StdAfx.h" #include ".\polygon.h" #using <mscorlib.dll> CPolygon::CPolygon(void) : Rad(0) { } CPolygon::~CPolygon(void) { } double CPolygon::getRadius(void) { // Ain't no returning no negative joint return (Rad <= 0) ? 0 : Rad; } void CPolygon::setRadius(double R) { // Ain't no using no negative stuff Rad = R; } |
Source Code: exercise.cpp |
// This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #include ".\polygon.h" #using <mscorlib.dll> using namespace System; int _tmain() { // TODO: Please replace the sample code below with your own. CPolygon *Triangle = __gc new CPolygon; Triangle->setRadius(25.55); Console::WriteLine(S"Triangular Polygon"); Console::WriteLine(S"Radius: {0}", Triangle->getRadius().ToString("F")); Console::WriteLine(S""); return 0; } |
Assuming that we are considering only a triangular polygon, having only the radius of the circle, we want to calculate the length of the side, the height, the length of a line perpendicular to a side from the center, and the area. To do this, we will need to perform some arithmetic operations. You can find the formulas to perform these calculations. To prepare the necessary calculations for this class, we can create a function pointer to handle a multiplication of two values. As we have seen already, this function can appear as follows (we created it in a new header file named Operations.h): |
#pragma once double (*Multiplication)(double x, double y);
You would need to implement a function that performs the actual operation. This function would appear as follows (just to be fancy, we implemented this function in a new source file named Operations.cpp): |
#include "StdAfx.h" double Times(double Value1, double Value2) { return Value1 * value2; }
Using a Function Pointer in a Class |
A function pointer allows us to declare a regular variable that represents the function but initialize it with the name function of a function that implements the necessary behavior. The goal is to let the (associated) function handle the calculation, which can reduce the amount of code written and subsequently the amount of errors. As done for the regular variables, this technique can be applied to a class. In C++, in order to use a function pointer as a member variable of a class, the function pointer must be type defined. This can be done as follows: |
#pragma once //--------------------------------------------------------------------------- typedef double (*Multiplication)(double x, double y); //--------------------------------------------------------------------------- double Times(double Value1, double Value2); //--------------------------------------------------------------------------- |
After type defining the function pointer, you can declare a member variable of it in the necessary class. Here is an example: |
#pragma once #include "Operations.h" public __gc class CPolygon { public: CPolygon(void); virtual ~CPolygon(void); private: double Rad; Multiplication Multiply; public: double getRadius(void); void setRadius(double R); }; |
To use this member variable, you can first declare a method. Here is an example: |
#pragma once #include ".\Operations.h" public __gc class CPolygon { public: CPolygon(void); virtual ~CPolygon(void); private: double Rad; Multiplication Multiply; public: double getRadius(void); void setRadius(double R); double CalculateSide(void); }; |
This method can be implemented as follows: |
#include "StdAfx.h" #include ".\polygon.h" #using <mscorlib.dll> using namespace System; CPolygon::CPolygon(void) : Rad(0) { Multiply = Times; } CPolygon::~CPolygon(void) { } double CPolygon::getRadius(void) { // Ain't no returning no negative joint return (Rad <= 0) ? 0 : Rad; } void CPolygon::setRadius(double R) { // Ain't no using no negative stuff Rad = R; } double CPolygon::CalculateSide(void) { return Multiply(Rad, Math::Sqrt(3)); } |
This method can then be tested as follows: |
// This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #include ".\polygon.h" #using <mscorlib.dll> using namespace System; int _tmain() { // TODO: Please replace the sample code below with your own. CPolygon *Triangle = __gc new CPolygon; Triangle->setRadius(25.55); Console::WriteLine(S"Triangular Polygon"); Console::WriteLine(S"Radius: {0}", Triangle->getRadius().ToString("F")); Console::WriteLine(S"Side: {0}", Triangle->CalculateSide().ToString("F")); Console::WriteLine(S""); return 0; } |
Here is the complete program: |
Header File: Operations.h |
#pragma once //--------------------------------------------------------------------------- typedef double (*Addition)(double x, double y); typedef double (*Subtraction)(double x, double y); typedef double (*Multiplication)(double x, double y); typedef double (*Multiply3Values)(double x, double y, double z); typedef double (*Division)(double x, double y); //--------------------------------------------------------------------------- double Add(double Value1, double Value2); double Sub(double Value1, double Value2); double Times(double Value1, double Value2); double Time3Values(double Value1, double Value2, double Value3); double Div(double Value1, double Value2); //--------------------------------------------------------------------------- |
Source Code: Operations.cpp |
#include "StdAfx.h" #include ".\Operations.h" double Add(double Value1, double Value2) { return Value1 + Value2; } double Sub(double Value1, double Value2) { return Value1 - Value2; } double Times(double Value1, double Value2) { return Value1 * Value2; } double Div(double Value1, double Value2) { return (Value2 == 0) ? 0.00 : (Value1 / Value2); } double Time3Values(double Value1, double Value2, double Value3) { return Value1 * Value2 * Value3; } |
Header Code: Polygon.h |
#pragma once #include ".\Operations.h" public __gc class CPolygon { public: CPolygon(void); virtual ~CPolygon(void); private: double Rad; Addition Plus; Division Divide; Multiplication Multiply; Multiply3Values Mult3Vals; public: double getRadius(void); void setRadius(double R); double CalculateSide(void); double CalculateSmallRadius(void); double CalculateHeight(void); double Area(void); }; |
Source Code: Polygon.cpp |
#include "StdAfx.h" #include ".\polygon.h" #using <mscorlib.dll> using namespace System; CPolygon::CPolygon(void) : Rad(0) { Plus = Add; Divide = Div; Multiply = Times; Mult3Vals = Time3Values; } CPolygon::~CPolygon(void) { } double CPolygon::getRadius(void) { // Ain't no returning no negative joint return (Rad <= 0) ? 0 : Rad; } void CPolygon::setRadius(double R) { // Ain't no using no negative stuff Rad = R; } double CPolygon::CalculateSide(void) { return Multiply(Rad, Math::Sqrt(3)); } double CPolygon::CalculateSmallRadius(void) { return Multiply(0.50, Rad); } double CPolygon::CalculateHeight(void) { return Plus(this->CalculateSmallRadius(), this->Rad); } double CPolygon::Area(void) { // This calculation assumes that the polygon is triangular (3 times) // Area = (n * S * r) / 2 return Divide(Mult3Vals(3, this->CalculateSide(), this->CalculateSmallRadius()), 0.50); } |
Source Code: Exercise.cpp |
// This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #include ".\polygon.h" #using <mscorlib.dll> using namespace System; int _tmain() { // TODO: Please replace the sample code below with your own. CPolygon *Triangle = __gc new CPolygon; Triangle->setRadius(25.55); Console::WriteLine(S"Triangular Polygon"); Console::WriteLine(S"Radius: {0}", Triangle->getRadius().ToString("F")); Console::WriteLine(S"Side: {0}", Triangle->CalculateSide().ToString("F")); Console::WriteLine(S"Small Radius: {0}", Triangle->CalculateSmallRadius().ToString("F")); Console::WriteLine(S"Height: {0}", Triangle->CalculateHeight().ToString("F")); Console::WriteLine(S"Area: {0}", Triangle->Area().ToString("F")); Console::WriteLine(S""); return 0; } |
|
||
Home | Copyright © 2004-2010 FunctionX, Inc. | |
|