|
Calling base class constructors
In all object oriented languages, you have a facility called inheritance where you can define one type of thing ("class of object") based on another, and the newly defined class ("subclass" or "extended class") takes the initial ("base") class as it starting point.
In your code for your base class, you'll have some logic which sets up new objects (a "constructor" method), and you'll have code in your extended classes through which you set up objects of that extended type. Almost inevitably, your extended objects will be pretty similar to your basic objects but they'll have a few extras, and so the writers of Object Oriented languages provide you with a way of calling the base class constructor within (or before) your extended class constructor. The base class is sometimes called the parent class. Let me translate that into an example in plainer English.
"A Train journey is a specialised type of public transport journey. If you're setting up a train journey, you'll want to set up a regular public transport journey within it first, with attributes like where it goes from and to, and at what time and who runs it. Then you will add some train extras such as how many carriages long it is"
How does this work in C++?
Train::Train(int nvh, int vhc, int xtrwa ) : PubT(nvh, vhc) {
tronly = xtrwa;
}
A Train is a PubT. When you create a train (with three parameters), you create first a PubT passing the first two parameters in to it, and them you perform the extra actions in the code block - which is saving the third parameter.
Course - C++ Programming
How does this work in Java?
public Train (int nvh, int vhc, int xtrwa) {
super (nvh,vhc);
this.tronly = xtrwa;
}
How does this work in Python?
def __init__(self,nvh,vhc,xtrwa):
pubt.__init__(self,nvh,vhc)
self.tronly = xtrwa
Course - Python Programming
This is old style classes; in new style classes, you'll call parent on the current class to avoid having to state the name of the parent class within the extended class definition.
How does this work in Perl?
In Perl, you "roll you own" ... it's so flexible and there are so many options it's almost untrue! Here's an example:
sub new {
my ($self,$nvh,$vhc,$xtrwa) = @_;
my $cc = new pubt($nvh,$vhc);
$cc->{"tronly"} = $xtrwa;
bless \%{$cc};
}
Course - Perl for Larger Projects
How does this work in [incr-Tcl]?
constructor {nvh vhc xtrwa} {
pubt::constructor $nvh $vhc } {
set tronly $xtrwa
}
}
Course - Tcl Basics and please let us know when you book that you would like us to add in strong coverage of Incr-Tcl
How does this work in PHP?
public function __construct($nvh, $vhc, $xtrwa) {
parent::__construct($nvh, $vhc);
$this->tronly = $xtrwa;
}
This example is for PHP 5. If you're still using PHP version 4, you'll call your constructor the same name as the name of the class, and call the base class constructor via the class name.
Course - Object Oriented PHP
def initialize(nvh, vhc, xtrwa)
super(nvh, vhc)
@tronly = xtrwa
end
How does this work in Lua?
Lua is a small language which provides you with facilities through which you can write OO like code but it is not strictly objects. You can set the metatable of a table [object] to the metatable of the table which you wish to be the parent, then modify it - thus emulating the facility that I'm talking about in this article.
Course - Lua Programming (written 2008-10-03 07:13:33)
Associated topics are indexed under C233 - C and C based languages - OO in C++ - beyond the basicsC234 - C and C based languages - Further C++ Object Oriented featuresH108 - Objects in PHPJ710 - Java - Extending Classes and MoreP218 - Perl - More ObjectsT245 - Tcl/Tk - [incr-Tcl]U107 - Object Orientation - the Lua wayY112 - Python - Objects - Intermediate
Some other Articles
FSB - an update.Claverton PumpAutumnSorting objects in PHPCalling base class constructorsIcelandic BadgeMarc Schneider is still having email troubleHolt on holtHotel Guest SurveysJavascript/HTML example, dynamic server monitor
|
2259 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|