问题
I have a question:
Say I have originally these classes which I can\'t change (let\'s say because they\'re taken from a library which I\'m using):
class Animal_
{
public:
Animal_();
int getIdA()
{
return idA;
};
string getNameA()
{
return nameA;
}
private:
string nameA;
int idA;
}
class Farm
{
public :
Farm()
{
sizeF=0;
}
Animal_* getAnimal_(int i)
{
return animals_[i];
}
void addAnimal_(Animal_* newAnimal)
{
animals_[sizeF]=newAnimal;
sizeF++;
}
private:
int sizeF;
Animal_* animals_[max];
}
But then I needed a class where I just add couple of fields so I did this:
class PetStore : public Farm
{
public :
PetStore()
{
idF=0;
};
private:
int idF;
string nameF;
}
But I can\'t initialize my derived class, I mean I did this Inheritance so I can add animals to my PetStore but now since sizeF is private how can I do that? I\'m thinking maybe in the PetStore default constructor I can call Farm()... so any idea?
回答1:
The constructor of PetStore
will call a constructor of Farm
; there's
no way you can prevent it. If you do nothing (as you've done), it will
call the default constructor (Farm()
); if you need to pass arguments,
you'll have to specify the base class in the initializer list:
PetStore::PetStore()
: Farm( neededArgument )
, idF( 0 )
{
}
(Similarly, the constructor of PetStore
will call the constructor of
nameF
. The constructor of a class always calls the constructors of
all of its base classes and all of its members.)
回答2:
First off, a PetStore
is not a farm.
Let's get past this though. You actually don't need access to the private members, you have everything you need in the public interface:
Animal_* getAnimal_(int i);
void addAnimal_(Animal_* newAnimal);
These are the methods you're given access to and these are the ones you should use.
I mean I did this Inheritance so I can add animals to my PetStore but now since sizeF is private how can I do that ??
Simple, you call addAnimal
. It's public
and it also increments sizeF
.
Also, note that
PetStore()
{
idF=0;
};
is equivalent to
PetStore() : Farm()
{
idF=0;
};
i.e. the base constructor is called, base members are initialized.
回答3:
The base-class constructor is already automatically called by your derived-class constructor. In C++, if the base class has a default constructor (takes no arguments, can be auto-generated by the compiler!), and the derived-class constructor does not invoke another base-class constructor in its initialisation list, the default constructor will be called. I.e. your code is equivalent to:
class PetStore: public Farm
{
public :
PetStore()
: Farm() // <---- Call base-class constructor in initialision list
{
idF=0;
};
private:
int idF;
string nameF;
}
回答4:
but I can't initialize my derived class, I mean I did this Inheritance so I can add animals to my PetStore but now since sizeF is private how can I do that ?? so I'm thinking maybe in the PetStore default constructor I can call Farm()... so any Idea ???
Don't panic.
Farm constructor will be called in the constructor of PetStore, automatically.
See the base class inheritance calling rules: What are the rules for calling the superclass constructor?
回答5:
Calling Base constructor in the Derived Constructor
Note:If we don't use super() keyword then it will call default constructor of Base Class which in this case is illegal as here Base Class Constructor takes three arguments.
So,it becomes neccessary to use super( ) keyword with desired arguments.
Remember:
1).From Outside the class,a constructor is always called with new operator.
2). From inside the class,it may be called through this( ) keyword OR super( ) keyword.
3).this( ) keyword can be used to call another Constructor of the same class(See Inner Class Concept).
4).Unlike other methods( i.e functions() ),Constructors are not inherited.
5).If you name Method Name Of Base Class Same as of Derived Class Method Name then
base class will not be called.
6).Whenever you create object of Derived Class then constructor of Base Class runs first(See program below).
class demo
{
public static void main(String args[])
{
derived1 d1=new derived1("Tom","Dad",21,"Programming","Cooking");
derived2 d2=new derived2("Tom","Dad",21,500);
d1.baseDisplay();//Calling Base class's baseDisplay() via derived class object
d1.display();
d2.baseDisplay();//Calling Base class's baseDisplay() via derived class object
d2.display();
}
}
class base
{
private
String name;
String fatherName;
int age;
String interest;
String hobby;
int piggyBankAmount;
base(String name,String fatherName,int age)
{
this.name=name;
this.fatherName=fatherName;
this.age=age;
}
public void baseDisplay()
{
System.out.println("Name:"+name+"\nFather's Name:"+fatherName+"\nAge:"+age);
}
}
class derived1 extends base
{ /* String interest; Note we inherited these data members from Base Class
String hobby; */
derived1(String name,String fatherName,int age,String interest,String hobby)
{
super(name,fatherName,age);
this.interest=interest;
this.hobby=hobby;
}
public void display()
{
System.out.println("Hobby:"+hobby+"\nInterest:"+interest);
}
}
class derived2 extends base
{ //int piggyBankAmount; Note we inherited this data member from Base Class
derived2(String name,String fatherName,int age,int piggyBankAmount)
{
super(name,fatherName,age);
this.piggyBankAmount=piggyBankAmount;
}
public void display()
{
System.out.println("piggyBankAmount:"+piggyBankAmount);
}
}
Output:
Name:Tom
Father's Name:Dad
Age:21
Hobby:Cooking
Interest:Programming
Name:Tom
Father's Name:Dad
Age:21
piggyBankAmount:500
Program to show Constructor of Base Class Runs First When Derived Class Object is created
class demo
{
public static void main(String args[])
{
derived1 d1=new derived1("Tom","Dad",21,"Programming","Cooking");
derived2 d2=new derived2("Tom","Dad",21,500);
d1.display();
d2.display();
}
}
class base
{
private
String name;
String fatherName;
int age;
String interest;
String hobby;
int piggyBankAmount;
base(String name,String fatherName,int age)
{
this.name=name;
this.fatherName=fatherName;
this.age=age;
System.out.println("Name:"+name+"\nFather's Name:"+fatherName+"\nAge:"+age);//See Constructor of Base class runs first
}
}
class derived1 extends base
{ /* String interest; Note we inherited these data members from Base Class
String hobby; */
derived1(String name,String fatherName,int age,String interest,String hobby)
{
super(name,fatherName,age);
this.interest=interest;
this.hobby=hobby;
}
public void display()
{
System.out.println("Hobby:"+hobby+"\nInterest:"+interest);
}
}
class derived2 extends base
{ //int piggyBankAmount; Note we inherited this data member from Base Class
derived2(String name,String fatherName,int age,int piggyBankAmount)
{
super(name,fatherName,age);
this.piggyBankAmount=piggyBankAmount;
}
public void display()
{
System.out.println("piggyBankAmount:"+piggyBankAmount);
}
}
Output:
Name:Tom
Father's Name:Dad
Age:21
Name:Tom
Father's Name:Dad
Age:21
Hobby:Cooking
Interest:Programming
piggyBankAmount:500
来源:https://stackoverflow.com/questions/10282787/calling-the-base-class-constructor-from-the-derived-class-constructor