Constructor in C++

A

Allerdyce.John

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.
 
A

Alf P. Steinbach

* (e-mail address removed):
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?

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

Allerdyce.John

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.
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
V

Victor Bazarov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,416
Latest member
MyraTrotte
Top