Overloading Classes
In OOP, it is a good practice to sometimes “overload” classes if you need to bring in information from the outside. For instance, if you want to include configuration variables in another class, you can “overload” that class with those variables. In this tutorial, I will explain how to do that in both PHP and C++.
Please note, before continuing, that I assume you are an intermediate to advanced C++/PHP programmer. Therefore, I will not go into great detail about how variables are set, etc. because you should already know how that works. We will jump right into the code now for an example as it’s usually the best way to explain these tasks.
PHP
<?php
/**
* Class overloading tutorial
* by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER JR. All Rights Reserved.
*
*/// Create our simple class
class OverloadMe
{
/**
* We would like a global var for the whole class
*/
var $var1;/**
* Constructor
*/
function __construct($name){
$this->var1 = $name;
}/**
* Name function
*/
function name(){
return $this->var1;
}
}// Define the name variable
$yourname = "Dennis";// We overload the function when we initialize it
$overload = new OverloadMe($yourname);// Now let's get the output
print "Your name is ".$overload->name();
?>
Now, the procedure is very similar in C++, just classes work differently as you already know. I’ll give the C++ code now and explain it all at the end!
C++
src/main.cpp
/**
* Class overloading tutorial
* by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER JR. All Rights Reserved.
*
*/#include <string>
#include "overload.h"int main(int argc,char* argv[])
{
std::string name = "Dennis";
OverloadMe overload(name.c_str());
overload.name();
return 0; // exit
}
src/overload.h
/**
* Class overloading tutorial
* by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER JR. All Rights Reserved.
*
*/#ifndef Overload_H
#define Overload_H// Class definition
class OverloadMe
{
public:
// Constructor
OverloadMe(const char* namevar);// Name function
void name();
private:
// A global var for the class
const char* var1;
};
#endif
src/overload.cpp
/**
* Class overloading tutorial
* by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER JR. All Rights Reserved.
*
*/// Make the class work
#include <iostream>
#include <stdio.h>
#include "overload.h"using namespace std;
OverloadMe::OverloadMe(const char* namevar)
: var1(namevar)
{
}void OverloadMe::name()
{
cout<< "Your name is " << var1 << endl << endl << "Please hit enter to exit" << endl;
getchar();
return;
}
Now, for Windows users using MSVC++, this code will compile with a simple copy/paste. For *nix and BSD users, I’ve included a Makefile within the ZIP package for you to use when compiling. Basically, the compiler must compile each item as an object first, then it must compile the objects into the program file rather than compiling the .exe’s directly.
Well, I hope this is of some use to someone. The examples are fairly simple, but should explain the concept easily. As we can see in the C++ example, we initialize the class to a variable, and simply overload that variable at the time of initialization.
Regards,
Dennis M.