Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C#
This is the forum to discuss the Wrox book MCSD Certification Toolkit (Exam 70-483): Programming in C# by Tiberiu Covaci, Rod Stephens, Vincent Varallo, Gerry O'Brien; ISBN: 978-1-118-61209-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 18th, 2016, 10:51 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 5: pg 164 Calling Parent Class Constructors - incorrect!?

Hi,

Isn't this section incorrect? It implies that a parent / base class's constructor is only called if the derived class has a constructor (and then goes further to imply you have to use the :base (params) notation to specify which one.

Though it's true you can specify a particular overloaded constructor in the base if you do nothing (e.g. either don't implement a constructor or do but don't add the base) the base *default* constructor is ALWAYS then called. In fact it's very hard to work a solution not to do this.

Actually on pg 163 it says "Although a clhild class inherits most of the code in its parent classs, it doesn't inherit the class's constructor" - again.. wrong (I think - this should say it doesn't automatically inherit the class's constructors that contain parameters!).

Am I wrong here?

Kind regards

Steve
Reply With Quote
  #2 (permalink)  
Old November 19th, 2016, 11:29 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,153, Level: 23
Points: 3,153, Level: 23 Points: 3,153, Level: 23 Points: 3,153, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 643
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi Steve,

Quote:
Isn't this section incorrect? It implies that a parent / base class's constructor is only called if the derived class has a constructor (and then goes further to imply you have to use the :base (params) notation to specify which one.


I don't think that's what the author of this chapter intended. I think he was trying to say that you need to invoke a parent constructor if you want to determine which one is called.

Quote:
Though it's true you can specify a particular overloaded constructor in the base if you do nothing (e.g. either don't implement a constructor or do but don't add the base) the base *default* constructor is ALWAYS then called. In fact it's very hard to work a solution not to do this.
I make the distinction between the default constructor and a parameterless constructor. A parameterless constructor has no parameters (duh). The default constructor is a parameterless constructor that Visual Studio creates for you if you don't define any other constructors. That's a bit different from a parameterless constructor that you create.

If you make any constructors parameterless or with parameters, then Visual Studio does not create the default constructor. For example, if you only give a class a constructor that takes one parameter, then it has no parameterless constructors.

The program always tries to invoke some base class constructor. If you don't give the base class any constructors, then it invokes the default constructor.

If the child class explicitly invokes a base class constructor, then you know which one it will use.

The weird case occurs if you give the base class a parameterized constructor and no parameterless constructor, but then don't invoke a constructor explicitly in the child class constructor. In that case Visual Studio tries to invoke a parameterless base class constructor but there isn't one. That's when you get the "no constructor with 0 arguments" error.

Quote:
Actually on pg 163 it says "Although a clhild class inherits most of the code in its parent classs, it doesn't inherit the class's constructor" - again.. wrong (I think - this should say it doesn't automatically inherit the class's constructors that contain parameters!).
This may be a semantic issue. I don't think the child class actually inherits the constructors, but its constructors invoke them. For example, suppose Person has a constructor that takes 2 parameters, first and last name. Then if Employee inherited that, it should have a constructor that takes 2 parameters. But it doesn't. If you want Employee to have a 2-parameter constructor, you need to define one (probably invoking the Person class's constructor).

But the child class has some access to the base class's constructors so they are still there even if they are not inherited in the same way that other methods are inherited.

Overall, however, I think your intuition is more or less right and this section of the book is somewhat confusing.

I hope that helps. If it's still unclear, please post a reply.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructors, pg. 39 vs 42 janetmweiss BOOK: Professional C++, 2nd Edition 1 December 8th, 2012 06:09 AM
pg 81 why is there 2 constructors with paramaters flipped? mikezx10 BOOK: Professional ASP.NET Design Patterns 4 February 25th, 2012 04:49 AM
calling a parent class method from a child class (Zend Cert) crmpicco Beginning PHP 2 July 30th, 2010 04:22 AM
errata chapter 5 pgs 164 & 165 wentz1 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 August 25th, 2005 04:00 AM
Calling SubClass attributes from parent class saravananedu Classic ASP Components 1 June 21st, 2004 01:59 AM



All times are GMT -4. The time now is 09:38 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.