Re: [C++] Inheritance Question: Constructors for Derived classes?

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 12/28/04


Date: Tue, 28 Dec 2004 11:50:18 +1100


"entropy123" <email_entropy123@yahoo.com> wrote in message
news:1104194263.307460.149630@z14g2000cwz.googlegroups.com...
| Hi All
|
| My question is the following: Is there a way to make a constructor for
| the derived class with additional derived class specific arguments
| while also making use of the derived class constructor:
|
| For example: In the code below the derived class constructor ALSO takes
| an int value in addition to the _str taken by the base class. The
| compiler produces an error when I attempt to implement the derived
| class as in my code snippet. Is there some other way?
|
| Thanks,
| ent
|
| class Base {
| public:
| Base(string in_str);
| ~Base();
| string getStr();
|
| private:
| string _str;
|
| }
|
| class Derived : public Base {
| public:
| Derived ( int in_data, string in_str ); //error
| private:
| int _data;
| }

Use initialisation lists:

class Base
 {
  public:
    Base( string in_str ) : _str( in_str ) {}
  private:
    string _str;
 };

class Derived : public Base
 {
  public:
    Derived( int in_data, string in_str )
             : Base( in_str ) {}
  private:
    int _data;
 };

Cheers.
Chris Val



Relevant Pages

  • Re: "this" and "base"
    ... compiler and the programming language. ... a private inherited field in a base class from a derived class is that the ... > because the member "is a private". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Bind Custom Class to windows form datagrid.
    ... Implementing it in the derived class won't work because ... >> Private mstrFirstName As String ... >> Public Sub New ... >> Public Function AddAs Integer ...
    (microsoft.public.dotnet.framework.windowsforms.databinding)
  • "this" and "base"
    ... Why can't you access a private inherited field from a base ... because the member "is a private". ... class manually in the derived class as shown in figure 1: ... because I inherit a member with the exact same signature ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Custom Attributes, Shared methods, Derived classes and reflection
    ... Private Sub Button1_Click(ByVal sender As System.Object, ... Dim MyDerivedClass As New MyDerivedClass_Instance ... Public PropertyForString1 As String ... I can require that a derived class implement a specific friendly ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Events with accessors
    ... Personally, I wouldn't - I'd create a protected method called OnMyEventwhich raises the event, and keep the detail private. ... The corresponding handler, the corresponding delegate, the corresponding event arguments--and now, yet another entity so that I can define an Event in an abstract base class for each derived class to raise when appropriate. ... I really don't see why events declared in the usual way couldn't have been set up for raising by derived classes, despite their encapsulation of the private delegate. ... A derived class can use the event in exactly the same way as a different class can - but it can't get at the private implementation, just as a public or protected property prevents you from getting at the implementation. ...
    (microsoft.public.dotnet.general)