473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question about constructor

consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}

Why the output is AB not BA ?
And when the A::A() be called ?

Jul 23 '05 #1
4 918
"July" <ju**********@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com
consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}

Why the output is AB not BA ?
And when the A::A() be called ?


The order of construction is:

1. base object constructor.
2. member object constructor.
3. body of own constructor function.
--
John Carson
Jul 23 '05 #2
July wrote:
consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}

Why the output is AB not BA ?
And when the A::A() be called ?

Obviously, the member object of the class is first constructed.

--
Best Regards

Xie Yubo
Email: xi*****@gmail.com Website: http://xieyubo.cn/
Harbin Institute of Technology
Phone: 86-451-86416614 Fax: 86-451-86413309
Jul 23 '05 #3
On 17 Jul 2005 06:22:43 -0700, "July" <ju**********@yahoo.com> did
courageously avow:
consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}

Why the output is AB not BA ?
And when the A::A() be called ?


Because the base class is called first, and then any derived classes,
in order?
Ken Wilson
"Coding, coding, over the bounding main()"
Jul 23 '05 #4

"July" <ju**********@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}

Why the output is AB not BA ?
And when the A::A() be called ?


Because you need to build the engine *before* you can start building the
car. The same goes with any other component in such a "composition".

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
8
by: Simon | last post by:
Hi everyone, Quick question: If I don't use base() in a subclass's construcor, will the base classes constructor be called at any point. It's just, I would have thought that the base class...
1
by: Chad | last post by:
I get the jist of this Inheritance stuff, I think, I'm just looking to understand better... I created a MustInherit base class with a constructor that had arguments. Within the constructor...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
3
by: xllx.relient.xllx | last post by:
I have a question about an example presented in the book "INFORMIT C++ Reference Guide" by Danny Kalev: <code> string getmagicword() ( return string("Supercalifragilisticexpialidocious"); );
19
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
3
by: MathWizard | last post by:
Hi, I have a question about returning an object in a function and calling a copy constructor. As far as I understand, in the following code the copy constructor may or may not be called,...
5
by: Amal P | last post by:
Dears, I have a question. class Data { }; class DisableDerive:virtual protected Data // A base class { private:
5
by: Dom | last post by:
is there a difference between the following: Rectangle r = new Rectangle (10,20,30,40); Rectangle r = Rectangle.FromLTRB(10,20,30,40); Also, I guess I'm not sure why Structs are created with a...
5
by: Vijay | last post by:
Hi All, I am not able to figure out what exactly happening in below code. what is control flow. Can anyone clear my confusion? Code: class A { public: A(){cout<<"In Constructor\n";}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.