473,670 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class constructor inheritance

I found an odd situation that probably isn't doing what is intended.

class A {
function A() {
// normal constructor for class1
}

function B() {
// a general purpose method
}
}

class B extends A {
// this class will inherit A::B as a constructor
}
Jul 17 '05 #1
11 4674
tc*****@savageR esearch.com (Tim Cadell) wrote in message news:<db******* *************** ****@posting.go ogle.com>...
I found an odd situation that probably isn't doing what is intended.

class A {
function A() {
// normal constructor for class1
}

function B() {
// a general purpose method
}
}

class B extends A {
// this class will inherit A::B as a constructor
}


I'm not sure I understand what you are saying. If I understood you,
you are saying class B picks up class A's constructor. Why would that
be odd? That is how it works in Java, and, I would guess, most OOP
languages. The constructors each execute in the order of the class
hierarchy, starting at the top and working down to the subclass.

Or did I misunderstand you?
Jul 17 '05 #2
Hi Tim,
I found an odd situation that probably isn't doing what is intended.

class A {
function A() {
// normal constructor for class1
}

function B() {
// a general purpose method
}
}

class B extends A {
// this class will inherit A::B as a constructor
}

The inheritance of the construct will result, cause
your class B has not defined a Constructor.
If you like to change this you have to override the
inherited construcotr with your own version....
That like in C++ ? What is odd on that?

Kind Regards.
Karl Heinz
--
Dipl.Ing.(FH) Karl Heinz Marbaise | www.marbaise.org
Jabba Dabba Dooh ;-) | ICQ# 135949029

Jul 17 '05 #3
Karl Heinz Marbaise <kh********@gmx .de> wrote in message news:<c4******* ******@ID-68093.news.uni-berlin.de>...
Hi Tim,
I found an odd situation that probably isn't doing what is intended.

class A {
function A() {
// normal constructor for class1
}

function B() {
// a general purpose method
}
}

class B extends A {
// this class will inherit A::B as a constructor
}

The inheritance of the construct will result, cause
your class B has not defined a Constructor.
If you like to change this you have to override the
inherited construcotr with your own version....
That like in C++ ? What is odd on that?

But even if he has a constructor in class B, the constructor in class
A still executes as part of the construction of class B, yes?
Jul 17 '05 #4
> But even if he has a constructor in class B, the constructor in class
A still executes as part of the construction of class B, yes?


None of this is what he is observing.

First of all, the answer to the above question is no. If class B has a
constructor, and extends class A (which also has a constructor), then class
A's constructor *will* *not* be called when you create a B object, unless
you either

1) There is no constructor for class B
2) You explicitly call the constructor for A in the constructor for B

Now, what Tim was saying is this:

class Shape {
function Shape () {
echo "I am a shape!";
}

function Square() {
echo "I have sharp corners!";
}
}

class Square extends Shape{
// No constructor defined
}

Now, the class Square does not have constructor, so it inherits the parent
constructor, right? Wrong. It has a constructor, Square::Square( ). Doing

$S = new Square();

would result in "I have sharp corners!" being output, but not "I am a
shape!"

NB: I have not actually tested this, I am just explaining what Tim was
saying. And it makes sense, based on what I know of PHP internals, that it
would be this way. In a hard core language, like C++, I doubt it would
behave like this. That is what he is observing.
Jul 17 '05 #5
"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
Now, what Tim was saying is this:

class Shape {
function Shape () {
echo "I am a shape!";
}

function Square() {
echo "I have sharp corners!";
}
}

class Square extends Shape{
// No constructor defined
}

Now, the class Square does not have constructor, so it inherits the parent
constructor, right? Wrong. It has a constructor, Square::Square( ). Doing

$S = new Square();

would result in "I have sharp corners!" being output, but not "I am a
shape!"

That's the kind of bug that would leave me dazed for days. Like, the
other day I wrote two class methods with the same name, but different
signatures, and I assumed PHP would tell the difference based on the
signature. Turns out I was wrong, but it took me awhile to realize
what the problem was.
Jul 17 '05 #6
"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
news:Ut******** ******@nwrddc03 .gnilink.net...
NB: I have not actually tested this, I am just explaining what Tim was
saying. And it makes sense, based on what I know of PHP internals, that it would be this way. In a hard core language, like C++, I doubt it would
behave like this. That is what he is observing.


I reproduced this behavior on PHP 4.3.4. Pretty weird. I know this was a
problem in PHP 3 but that was supposed to have been fixed a long time ago.
According to the manual, "A constructor is a function of the same name as
the class it is being defined in."

Jul 17 '05 #7
> That's the kind of bug that would leave me dazed for days. Like, the
other day I wrote two class methods with the same name, but different
signatures, and I assumed PHP would tell the difference based on the
signature. Turns out I was wrong, but it took me awhile to realize
what the problem was.


I thought PHP would give an error about redefining a function. What does it
actually do? Just use the first one?
Jul 17 '05 #8
"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message news:<EA******* *********@nwrdd c03.gnilink.net >...
That's the kind of bug that would leave me dazed for days. Like, the
other day I wrote two class methods with the same name, but different
signatures, and I assumed PHP would tell the difference based on the
signature. Turns out I was wrong, but it took me awhile to realize
what the problem was.


I thought PHP would give an error about redefining a function. What does it
actually do? Just use the first one?


It just used the last one, which wasn't the one I wanted.
Jul 17 '05 #9
> It just used the last one, which wasn't the one I wanted.

I think the only way to do this would be to check to see what variables were
passed (NB: if they get passed NULL, it will be the same as if they were not
passed at all - using isset() won't help :-/ ), then either do and if/else,
if/elseif/else, or switch() setup.
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
6343
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
7
13212
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
8
1573
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the class of "obj"?
9
4989
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
3
1589
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client app with; D m_D = new D(tkn); public class A : MarshalByRefObject public A () <--------------------- public class B : A public B () <----------------------
4
2653
by: Francisco Amaro | last post by:
Hi all, Have question about inheriting a class that has parameters in the constructor such as : Public MustInherit Class MyParentClass Public mystring As String Public Sub New(ByVal paramstring As String)
3
2238
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
5
2654
by: petethebloke | last post by:
I'm not sure how to ask for what I want, so I can't find it on Google. I'll explain..... I have a class 'hanging' with a constructor: class hanging{ function hanging(){ }
11
18331
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by any mechanism as super is not supported by c++, atleast by MS vc++ 6.0.
6
3206
by: Xu, Qian | last post by:
Hello All, is there any handy tool to generate class diagrams for javascript? I have tried JS/UML, but it generates always an empty diagram. -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8813
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7412
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6212
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.