09/04/131 VIT - SCSE
1. The derived class need not have a constructor as long as
the base class has a no-argument constructor.
2. However, if the base class has constructors with
arguments (one or more), then it is mandatory for the
derived class to have a constructor and pass the
arguments to the base class constructor.
3. When an object of a derived class is created, the
constructor of the base class is executed first and later the
constructor of the derived class.
Constructors in Derived Classes
09/04/132 VIT - SCSE
1. No constructors in the base class and derived class
2. Constructor only in the base class
class A
{
public:
A()
{
cout<<”No-argument constructor of the base class B is executed”;
}
};
class B:public A
{
public:
};
void main()
{
B ob; //accesses base constructor
}
Constructors in Derived Classes
09/04/133 VIT - SCSE
3. Constructor only in the derived class
class A
{
public:
};
class B:public A
{
public:
B()
{
cout<<”No-argument constructor of the base class B is executed”;
}
};
void main()
{
B ob; //accesses derived constructor
}
09/04/134 VIT - SCSE
4. Constructors in both base and derived classes
class A
{
public:
A()
{
cout<<”No-argument constructor of the base class A executed first”;
}
};
class B:public A
{
public:
B()
{
cout<<”No-argument constructor of the base class B is executed next”;
}
};
void main()
{
B ob; //accesses both constructor
}
09/04/135 VIT - SCSE
class A
{
public:
A()
{
cout<<”No argument constructor of
base class A”;
}
A(int a)
{
cout<<”One argument constructor of
the base class A”;
}
};
class B:public A
{
public:
B(int a)
{
cout<<”One argument
constructor of the
derived class B”;
}
};
void main()
{
B ob(3);
}
5. Multiple constructors in base class and a single
constructor in derived class
09/04/136 VIT - SCSE
Output:
No argument constructor of base class A
One argument constructor of the derived class B
09/04/137 VIT - SCSE
class A
{
public:
A(int a)
{
cout<<”One argument constructor of the base
class A”;
} };
class B:public A
{
public:
B(int a)
{
cout<<”One argument constructor of the
derived class B”;
} };
void main()
{
B ob(3);
}
Output:
Error
6. Constructor in base and derived classes without default
constructor
09/04/138 VIT - SCSE
class A
{
public:
A(int a)
{
cout<<”One argument constructor of
the base class A”;
}};
class B:public A
{
public:
B(int a):A(a)
{
cout<<”One argument constructor of
the derived class B”;
}};
void main()
{
B ob(3);
}
Output:
One argument
constructor of the
base class A
One argument
constructor of the
derived class B
7. Explicit invocation in the absence of default constructor
09/04/139 VIT - SCSE
class A1
{
public:
A1()
{
cout<<”No-argument constructor of the base
class A1”;
}};
class A2
{
public:
A2()
{
cout<<”No argument constructor of the base
class A2”;
}};
class B:public
A2,public A1
{
public:
B()
{
cout<<”No argument
constructor of the
derived class B”;
}};
void main()
{
B ob;
}
8. Constructor in a multiple inherited class with default
invocation
09/04/1310 VIT - SCSE
Output:
No argument constructor of the base class A2
No-argument constructor of the base class A1
No argument constructor of the derived class B
09/04/1311 VIT - SCSE
class A1
{
public:
A1()
{
cout<<”No-argument constructor of the base
class A1”;
}};
class A2
{
public:
A2()
{
cout<<”No argument constructor of the base
class A2”;
}};
class B:public
A2,public A1
{
public:
B():A2(),A1()
{
cout<<”No argument
constructor of the
derived class B”;
}};
void main()
{
B ob;
}
9. Constructor in a multiple inherited class with explicit
invocation
09/04/1312 VIT - SCSE
Output:
No argument constructor of the base class A2
No-argument constructor of the base class A1
No argument constructor of the derived class B
09/04/1313 VIT - SCSE
class A
{
public:
A1()
{
cout<<”No-argument constructor of the base
class A”;
}
};
class B:public A
{
public:
B()
{
cout<<”No argument constructor of the base
class B”;
}
};
class C:public B
{
public:
C()
{
cout<<”No argument
constructor of the
derived class C”;
}
};
void main()
{
C ob;
}
10. Constructor in multilevel inheritance
09/04/1314 VIT - SCSE
Output:
No argument constructor of the base class A
No-argument constructor of the base class B
No argument constructor of the derived class C
09/04/1315 VIT - SCSE
Destructors in derived classes
1.Unlike constructors, destructors in the class hierarchy
(parent and child class) are invoked in the reverse order of
the constructor invocation.
2.The destructor of that class whose constructor was
executed last, while building object of the derived class, will
be executed first whenever the object goes out of scope.
09/04/1316 VIT - SCSE
class B1
{
public:
B1()
{
cout<<”No argument constructor of the base
class B1”;
}
~B1()
{
cout<<”Destructor in the base class B1”;
}};
class B2
{
public:
B2()
{
cout<<”No argument constructor of the base
class B2”;
}
~B2()
{
cout<<”Destructor in the
base class B2”;
}
};
class D:public B1,public
B2
{
public:
D()
{
cout<<”No argument
constructor of the
derived class D”;
}
09/04/1317 VIT - SCSE
~D()
{
cout<<”Destructor in the
derived class D”;
}
};
void main()
{
D ob;
}
Output:
No argument constructor of the base
class B1
No argument constructor of the base
class B2
No argument constructor of the derived
class D
Destructor in the derived class D
Destructor in the base class B2
Destructor in the base class B1

11 constructors in derived classes

  • 1.
    09/04/131 VIT -SCSE 1. The derived class need not have a constructor as long as the base class has a no-argument constructor. 2. However, if the base class has constructors with arguments (one or more), then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor. 3. When an object of a derived class is created, the constructor of the base class is executed first and later the constructor of the derived class. Constructors in Derived Classes
  • 2.
    09/04/132 VIT -SCSE 1. No constructors in the base class and derived class 2. Constructor only in the base class class A { public: A() { cout<<”No-argument constructor of the base class B is executed”; } }; class B:public A { public: }; void main() { B ob; //accesses base constructor } Constructors in Derived Classes
  • 3.
    09/04/133 VIT -SCSE 3. Constructor only in the derived class class A { public: }; class B:public A { public: B() { cout<<”No-argument constructor of the base class B is executed”; } }; void main() { B ob; //accesses derived constructor }
  • 4.
    09/04/134 VIT -SCSE 4. Constructors in both base and derived classes class A { public: A() { cout<<”No-argument constructor of the base class A executed first”; } }; class B:public A { public: B() { cout<<”No-argument constructor of the base class B is executed next”; } }; void main() { B ob; //accesses both constructor }
  • 5.
    09/04/135 VIT -SCSE class A { public: A() { cout<<”No argument constructor of base class A”; } A(int a) { cout<<”One argument constructor of the base class A”; } }; class B:public A { public: B(int a) { cout<<”One argument constructor of the derived class B”; } }; void main() { B ob(3); } 5. Multiple constructors in base class and a single constructor in derived class
  • 6.
    09/04/136 VIT -SCSE Output: No argument constructor of base class A One argument constructor of the derived class B
  • 7.
    09/04/137 VIT -SCSE class A { public: A(int a) { cout<<”One argument constructor of the base class A”; } }; class B:public A { public: B(int a) { cout<<”One argument constructor of the derived class B”; } }; void main() { B ob(3); } Output: Error 6. Constructor in base and derived classes without default constructor
  • 8.
    09/04/138 VIT -SCSE class A { public: A(int a) { cout<<”One argument constructor of the base class A”; }}; class B:public A { public: B(int a):A(a) { cout<<”One argument constructor of the derived class B”; }}; void main() { B ob(3); } Output: One argument constructor of the base class A One argument constructor of the derived class B 7. Explicit invocation in the absence of default constructor
  • 9.
    09/04/139 VIT -SCSE class A1 { public: A1() { cout<<”No-argument constructor of the base class A1”; }}; class A2 { public: A2() { cout<<”No argument constructor of the base class A2”; }}; class B:public A2,public A1 { public: B() { cout<<”No argument constructor of the derived class B”; }}; void main() { B ob; } 8. Constructor in a multiple inherited class with default invocation
  • 10.
    09/04/1310 VIT -SCSE Output: No argument constructor of the base class A2 No-argument constructor of the base class A1 No argument constructor of the derived class B
  • 11.
    09/04/1311 VIT -SCSE class A1 { public: A1() { cout<<”No-argument constructor of the base class A1”; }}; class A2 { public: A2() { cout<<”No argument constructor of the base class A2”; }}; class B:public A2,public A1 { public: B():A2(),A1() { cout<<”No argument constructor of the derived class B”; }}; void main() { B ob; } 9. Constructor in a multiple inherited class with explicit invocation
  • 12.
    09/04/1312 VIT -SCSE Output: No argument constructor of the base class A2 No-argument constructor of the base class A1 No argument constructor of the derived class B
  • 13.
    09/04/1313 VIT -SCSE class A { public: A1() { cout<<”No-argument constructor of the base class A”; } }; class B:public A { public: B() { cout<<”No argument constructor of the base class B”; } }; class C:public B { public: C() { cout<<”No argument constructor of the derived class C”; } }; void main() { C ob; } 10. Constructor in multilevel inheritance
  • 14.
    09/04/1314 VIT -SCSE Output: No argument constructor of the base class A No-argument constructor of the base class B No argument constructor of the derived class C
  • 15.
    09/04/1315 VIT -SCSE Destructors in derived classes 1.Unlike constructors, destructors in the class hierarchy (parent and child class) are invoked in the reverse order of the constructor invocation. 2.The destructor of that class whose constructor was executed last, while building object of the derived class, will be executed first whenever the object goes out of scope.
  • 16.
    09/04/1316 VIT -SCSE class B1 { public: B1() { cout<<”No argument constructor of the base class B1”; } ~B1() { cout<<”Destructor in the base class B1”; }}; class B2 { public: B2() { cout<<”No argument constructor of the base class B2”; } ~B2() { cout<<”Destructor in the base class B2”; } }; class D:public B1,public B2 { public: D() { cout<<”No argument constructor of the derived class D”; }
  • 17.
    09/04/1317 VIT -SCSE ~D() { cout<<”Destructor in the derived class D”; } }; void main() { D ob; } Output: No argument constructor of the base class B1 No argument constructor of the base class B2 No argument constructor of the derived class D Destructor in the derived class D Destructor in the base class B2 Destructor in the base class B1