473,839 Members | 1,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance and Explicit Calls to Base Class Constructors

Hi,
Maybe someone can help me with the following:
"The first task by any derived class constructor is to call it’s direct or
indirect base class constructor implicitly or explicitly", reads the
statement.
To test this, I added messageboxes in all constructors indicating which
constructor is called.
There are four constructors which could be called:
1. Base class empty constructor
2. Base class 2-Par. constructor
3. Derived class empty constructor
4. Derived class 3-Par. constructor
I expected that constructors # 2 and 4 would be called but not #1 or #3 (no
instantiation of zero parameter objects).
Why does this happen? (see attachment)

Thanks for your help,

Regards,

John

*********
' Fig. 9.9: Point2.vb
' CPoint2 class contains an x-y coordinate pair as Protected data.

Imports System.Windows. Forms

Public Class CPoint2
' implicitly Inherits Object

' point coordinate
Protected mX, mY As Integer

' default constructor
Public Sub New()

' implicit call to Object constructor occurs here
MessageBox.Show ("Implicit call was made!", "CPoint2 empty Constuctor")
X = 0
Y = 0
End Sub ' New

' constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer)

' implicit call to Object constructor occurs here
MessageBox.Show ("Implicit call was made!", "CPoint2 2-Par Constuctor")
X = xValue
Y = yValue
End Sub ' New

' property X
Public Property X() As Integer

Get
Return mX
End Get

Set(ByVal xValue As Integer)
mX = xValue ' no need for validation
End Set

End Property ' X

' property Y
Public Property Y() As Integer

Get
Return mY
End Get

Set(ByVal yValue As Integer)
mY = yValue ' no need for validation
End Set

End Property ' Y

' return String representation of CPoint2
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function ' ToString

End Class ' CPoint2
*******
Imports System.Windows. Forms

Public Class CCircle3
Inherits CPoint2 ' CCircle3 Inherits from class CPoint2

Private mRadius As Double ' CCircle3's radius

' default constructor
Public Sub New()

' implicit call to CPoint constructor occurs here
MessageBox.Show ("Implicit call was made!", "CCircle3 empty Constuctor")
Radius = 0
End Sub ' New

' constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer, ByVal radiusValue As Double)

' implicit call to CPoint2 constructor occurs here
MessageBox.Show ("Implicit call was made!", "CCircle3 3-Par Constuctor")
mX = xValue
mY = yValue
Radius = radiusValue
End Sub ' New

' property Radius
Public Property Radius() As Double

Get
Return mRadius
End Get

Set(ByVal radiusValue As Double)

If radiusValue > 0 Then
mRadius = radiusValue
End If

End Set

End Property ' Radius

' calculate CCircle3 diameter
Public Function Diameter() As Double
Return mRadius * 2
End Function ' Diameter

' calculate CCircle3 circumference
Public Function Circumference() As Double
Return Math.PI * Diameter()
End Function ' Circumference

' calculate CCircle3 area
Public Overridable Function Area() As Double
Return Math.PI * mRadius ^ 2
End Function ' Area

' return String representation of CCircle3
Public Overrides Function ToString() As String
Return "Center = " & "[" & mX & ", " & mY & "]" & _
"; Radius = " & mRadius
End Function ' ToString

End Class ' CCircle3
********
' Fig. 9.11: CircleTest3.vb
' Testing class CCircle3.

Imports System.Windows. Forms

Module modCircleTest3

Sub Main()
Dim circle As CCircle3
Dim output As String

circle = New CCircle3(37, 43, 2.5) ' instantiate CCircle3

' get CCircle3's initial x-y coordinates and radius
output = "X coordinate is " & circle.X & vbCrLf & _
"Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _
circle.Radius

' set CCircle3's x-y coordinates and radius to new values
circle.X = 2
circle.Y = 2
circle.Radius = 4.25

' display CCircle3's String representation
output &= vbCrLf & vbCrLf & _
"The new location and radius of circle are " & _
vbCrLf & circle.ToString () & vbCrLf

' display CCircle3's diameter
output &= "Diameter is " & _
String.Format(" {0:F}", circle.Diameter ()) & vbCrLf

' display CCircle3's circumference
output &= "Circumfere nce is " & _
String.Format(" {0:F}", circle.Circumfe rence()) & vbCrLf

' display CCircle3's area
output &= "Area is " & String.Format(" {0:F}", circle.Area())

MessageBox.Show (output, "Demonstrat ing Class CCircle3")
End Sub ' Main

End Module ' modCircleTest3
******

Nov 21 '05 #1
1 2260
John,
As you code states, you only have implicit calls to constructors. Implicitly
called constructors are always the default constructor. If you want
constructors #2 & #4 called, then you need to explicitly call them.

Something like:

| ' constructor
| Public Sub New(ByVal xValue As Integer, _
| ByVal yValue As Integer, ByVal radiusValue As Double)

| ' Explicit call to CPoint2 constructor occurs here
MyBase.New(xVal ue, yValue)

| MessageBox.Show ("Implicit call was made!", "CCircle3 3-Par
Constuctor")
| mX = xValue
| mY = yValue
| Radius = radiusValue
| End Sub ' New
--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"John" <Jo**@discussio ns.microsoft.co m> wrote in message
news:47******** *************** ***********@mic rosoft.com...
| Hi,
| Maybe someone can help me with the following:
| "The first task by any derived class constructor is to call it's direct or
| indirect base class constructor implicitly or explicitly", reads the
| statement.
| To test this, I added messageboxes in all constructors indicating which
| constructor is called.
| There are four constructors which could be called:
| 1. Base class empty constructor
| 2. Base class 2-Par. constructor
| 3. Derived class empty constructor
| 4. Derived class 3-Par. constructor
| I expected that constructors # 2 and 4 would be called but not #1 or #3
(no
| instantiation of zero parameter objects).
| Why does this happen? (see attachment)
|
| Thanks for your help,
|
| Regards,
|
| John
|
| *********
| ' Fig. 9.9: Point2.vb
| ' CPoint2 class contains an x-y coordinate pair as Protected data.
|
| Imports System.Windows. Forms
|
| Public Class CPoint2
| ' implicitly Inherits Object
|
| ' point coordinate
| Protected mX, mY As Integer
|
| ' default constructor
| Public Sub New()
|
| ' implicit call to Object constructor occurs here
| MessageBox.Show ("Implicit call was made!", "CPoint2 empty
Constuctor")
| X = 0
| Y = 0
| End Sub ' New
|
| ' constructor
| Public Sub New(ByVal xValue As Integer, _
| ByVal yValue As Integer)
|
| ' implicit call to Object constructor occurs here
| MessageBox.Show ("Implicit call was made!", "CPoint2 2-Par
Constuctor")
| X = xValue
| Y = yValue
| End Sub ' New
|
| ' property X
| Public Property X() As Integer
|
| Get
| Return mX
| End Get
|
| Set(ByVal xValue As Integer)
| mX = xValue ' no need for validation
| End Set
|
| End Property ' X
|
| ' property Y
| Public Property Y() As Integer
|
| Get
| Return mY
| End Get
|
| Set(ByVal yValue As Integer)
| mY = yValue ' no need for validation
| End Set
|
| End Property ' Y
|
| ' return String representation of CPoint2
| Public Overrides Function ToString() As String
| Return "[" & mX & ", " & mY & "]"
| End Function ' ToString
|
| End Class ' CPoint2
| *******
| Imports System.Windows. Forms
|
| Public Class CCircle3
| Inherits CPoint2 ' CCircle3 Inherits from class CPoint2
|
| Private mRadius As Double ' CCircle3's radius
|
| ' default constructor
| Public Sub New()
|
| ' implicit call to CPoint constructor occurs here
| MessageBox.Show ("Implicit call was made!", "CCircle3 empty
Constuctor")
| Radius = 0
| End Sub ' New
|
| ' constructor
| Public Sub New(ByVal xValue As Integer, _
| ByVal yValue As Integer, ByVal radiusValue As Double)
|
| ' implicit call to CPoint2 constructor occurs here
| MessageBox.Show ("Implicit call was made!", "CCircle3 3-Par
Constuctor")
| mX = xValue
| mY = yValue
| Radius = radiusValue
| End Sub ' New
|
| ' property Radius
| Public Property Radius() As Double
|
| Get
| Return mRadius
| End Get
|
| Set(ByVal radiusValue As Double)
|
| If radiusValue > 0 Then
| mRadius = radiusValue
| End If
|
| End Set
|
| End Property ' Radius
|
| ' calculate CCircle3 diameter
| Public Function Diameter() As Double
| Return mRadius * 2
| End Function ' Diameter
|
| ' calculate CCircle3 circumference
| Public Function Circumference() As Double
| Return Math.PI * Diameter()
| End Function ' Circumference
|
| ' calculate CCircle3 area
| Public Overridable Function Area() As Double
| Return Math.PI * mRadius ^ 2
| End Function ' Area
|
| ' return String representation of CCircle3
| Public Overrides Function ToString() As String
| Return "Center = " & "[" & mX & ", " & mY & "]" & _
| "; Radius = " & mRadius
| End Function ' ToString
|
| End Class ' CCircle3
| ********
| ' Fig. 9.11: CircleTest3.vb
| ' Testing class CCircle3.
|
| Imports System.Windows. Forms
|
| Module modCircleTest3
|
| Sub Main()
| Dim circle As CCircle3
| Dim output As String
|
| circle = New CCircle3(37, 43, 2.5) ' instantiate CCircle3
|
| ' get CCircle3's initial x-y coordinates and radius
| output = "X coordinate is " & circle.X & vbCrLf & _
| "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _
| circle.Radius
|
| ' set CCircle3's x-y coordinates and radius to new values
| circle.X = 2
| circle.Y = 2
| circle.Radius = 4.25
|
| ' display CCircle3's String representation
| output &= vbCrLf & vbCrLf & _
| "The new location and radius of circle are " & _
| vbCrLf & circle.ToString () & vbCrLf
|
| ' display CCircle3's diameter
| output &= "Diameter is " & _
| String.Format(" {0:F}", circle.Diameter ()) & vbCrLf
|
| ' display CCircle3's circumference
| output &= "Circumfere nce is " & _
| String.Format(" {0:F}", circle.Circumfe rence()) & vbCrLf
|
| ' display CCircle3's area
| output &= "Area is " & String.Format(" {0:F}", circle.Area())
|
| MessageBox.Show (output, "Demonstrat ing Class CCircle3")
| End Sub ' Main
|
| End Module ' modCircleTest3
| ******
|
|
|
Nov 21 '05 #2

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

Similar topics

5
2206
by: Jochen Zeischka | last post by:
Hello, I just tried something with multiple inheritance and I have a problem with the construction of my object. There's a base class Base, containing an integer. The base class has 2 derived classes: Derived1 and Derived2. Then, multiple inheritance is used to derive MultipleDer from Derived1 and Derived2. All classes have 1 constructor: ClassName::ClassName(int i = 0);
4
2286
by: Busin | last post by:
When a child class inherits from a base class, will the child class inherits everything of the base class, including all member variables and functions? Or is such inheritance "selective", like not inheriting all constructors, assignment operators, destructor, etc.? Thanks!
4
2904
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
20
23125
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a much finer level. As such, it's very frustrating to be working in VBA which lacks inheritance, one of the more powerful tools for eliminating duplication at the level I'm talking about. I've recently come up with a technique to emulate one...
14
12930
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
45
6375
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
11
1365
by: needin4mation | last post by:
I know this doesn't have much of a context, but, in this (take from Petzold's Programming Windows with C#) snippet: class DatePlus: Date { public DatePlus(){} public DatePlus(int year, int month, int day): base(year, month, day){} .. ..
7
3744
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
3
2556
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
0
9855
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9697
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
10586
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
10648
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
10293
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
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
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.