473,513 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need of Explicit Constructor Call in C++

sbvishal
6 New Member
Expand|Select|Wrap|Line Numbers
  1. class Base
  2. { public:
  3.     Base(){}
  4.     /*Base(int x)
  5.     {
  6.       int a=x;
  7.       //int b=y;
  8.       cout<<"b="<<a<<endl;
  9.       //cout<<"y="<<b<<endl;
  10.     }*/
  11. };
  12.  
  13. class der: public Base
  14. {  public:
  15.     der(int x)//:Base(x)
  16.     {
  17.         int a=x;cout<<"x="<<a<<endl;
  18.     }
  19. };
  20.  
  21. void main()
  22. {
  23.     der d(1);
  24.     getch();
  25.  
  26. }
  27.  
In above program if we dont use the default constructor then we have to use explicit calling of constructor.
Why it is required......
Sep 25 '07 #1
2 2231
dmjpro
2,476 Top Contributor
Expand|Select|Wrap|Line Numbers
  1. class Base
  2. { public:
  3.     Base(){}
  4.     /*Base(int x)
  5.     {
  6.       int a=x;
  7.       //int b=y;
  8.       cout<<"b="<<a<<endl;
  9.       //cout<<"y="<<b<<endl;
  10.     }*/
  11. };
  12.  
  13. class der: public Base
  14. {  public:
  15.     der(int x)//:Base(x)
  16.     {
  17.         int a=x;cout<<"x="<<a<<endl;
  18.     }
  19. };
  20.  
  21. void main()
  22. {
  23.     der d(1);
  24.     getch();
  25.  
  26. }
  27.  
In above program if we dont use the default constructor then we have to use explicit calling of constructor.
Why it is required......
Because base class must be initialized before derived one initialized.

Kind regards,
Dmjpro.
Sep 25 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Adding to this, the derived class object is really a compound object. That is, there is a base object inside the derived object. As a derived class, there is no knowledge as to how to initialize that inner base object, yet it must be initialized. Therefore, the derived class must call the constructor of it's immediate base class.

You will find that as the hierarchy deepens, each derived class constructor must have enough arguments to call the immediate base class constructor.
Sep 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
15320
by: Dario | last post by:
Trying to compile the following code-fragment with g++ 2.96: class Entity { private: void * data; public: explicit Entity(int); explicit Entity(Entity &); virtual ~Entity(); void...
1
5638
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
6
7500
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
12
4729
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
9
2353
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
9
1538
by: D.J. Stachniak | last post by:
Hi, I have a question about explicit constructors and the C++ standard. Forgive me for my laziness in not pouring over the spec myself, but I thought I'd ask first and read later. I have a...
2
3921
by: Fred Zwarts | last post by:
Consider the following code: // Start of code class MyException_t { public: // Default constructor.
12
8962
by: Ole Nielsby | last post by:
Why is this? I've stumbled on this restriction more than once, and I'd like to know the philosophy behind it if there is one. I figure I'd be less prone to make this error if I knew the reason....
1
434
by: denis | last post by:
Hello, I noticed the following in the "C++ Coding Standards" book. Why should the code on line 30 be in error? Transmogrify accepts a (B obj), and we are passing it a B obj. I understand why...
12
7173
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
7260
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,...
0
7161
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...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7101
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...
0
4746
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.