473,503 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor in C++

Hi,

I have 2 classes:

class A {
public:
A();
A(int i);
};

class B: public A {
pubilc:
B(int i);
};

If I don't have constructor B(), does it call A() automatically?

Can I do this:
B b; // B does not have default constructor but A has.

Mar 1 '06 #1
4 1613
* Al************@gmail.com:
Hi,

I have 2 classes:

class A {
public:
A();
A(int i);
};

class B: public A {
pubilc:
See <url: http://www.google.com/search?q=pubilc>.

B(int i);
};

If I don't have constructor B(), does it call A() automatically?
If you don't have a constructor B() it doesn't exist; C++ only generates
one for you if you don't declare any constructors at all.

Can I do this:
B b; // B does not have default constructor but A has.


Nope.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 1 '06 #2
If I have
class A {
public
A(int i);
}

public B{
public:
virtual void abstractFunciton = 0;
}

public C{
public:
C(int i);
};

how can I call A' constructor A(int i) from C(int i).

I try
C::C(int i): A(i) {
}
it does not compile.

Mar 1 '06 #3
* Al************@gmail.com:
If I have
class A {
public
A(int i);
}

public B{
public:
virtual void abstractFunciton = 0;
}

public C{
public:
C(int i);
};

how can I call A' constructor A(int i) from C(int i).

I try
C::C(int i): A(i) {
}
it does not compile.


C does not derive from A.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 1 '06 #4
Al************@gmail.com wrote:
If I have
class A {
public
A(int i);
} ;
public B{
public:
virtual void abstractFunciton = 0;
virtual void pureFunction() = 0;
} ;
public C{
public:
C(int i);
};

how can I call A' constructor A(int i) from C(int i).

I try
C::C(int i): A(i) {
}
it does not compile.


You need to derive 'C' from 'A' first to do that.

V
--
Please remove capital As from my address when replying by mail
Mar 1 '06 #5

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

Similar topics

3
4532
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
15
21173
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
23
5138
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
12
4728
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() {} };
18
2970
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
9
2356
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
6309
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
8
4288
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
74
15868
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
13
2361
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
0
7202
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
7084
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
7328
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
7458
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...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
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
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
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.