Why in the underlying example keeps the compiler complaining about a redefinition for the B constructor. If i remove the {} it complains they need to be there. How is this done correctly?
I want to implement the constructor of B in the CPP file and not inline.
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;
class A
{
public:
A();
~A();
};
class B : public A
{
public:
B() : A() {};
~B();
};
A::A()
{
cout << "A construct!";
}
B::B()
{
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
cout << "B construct!";
}
int main()
{
return 0;
}
You have already defined B constructor in the class definition:
class B : public A
{
public:
B() : A() {};
~B();
};
So, when you define it again outside of the class definition:
B::B()
{
cout << "B construct!";
you get the error; if the correct constructor is the last one, replace your class definition with this one:
class B : public A
{
public:
B();
~B();
};
If you want to specify the base class constructor in B constructor definition, you can do:
B::B()
: A()
{
cout << "B construct!";
Remove : A() {}, that's the body of your constructor, not just {}. Add the initializer list to the body B::B() you have later on.
Related
while I'm writing some code regarding base class/derived class type variables, I encountered this somewhat weird situation. It goes like this :
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A Con" << endl; }
virtual ~A() { cout << "A Dec" << endl; }
};
class B : A {
public:
A a;
B(A _a) : a(_a) {
cout << "B Con" << endl;
}
virtual ~B() { cout << "B Dec" << endl; }
};
int main()
{
A a;
B b(a);
}
I declared base class(A) type member variable in derived class(B), and then above code gives me this result :
OK, so first constructor and last constructor would be for 'a' in main function.
But what about the others? why destructors are called 5 times while constructors are only called 3?
My Guess is :
A Con -> a in main()
A Con -> b in main() (base class constructor)
B Con -> b in main() (derived class constructor)
A Dec -> temporary value passed in B(A _a) : a(_a), copy constructor is called...
B Dec -> b in main() (derived class destructor)
A Dec -> member variable in B class (A a;)
A Dec -> b in main() (base class destuctor)
A Dec -> a in main()
Am I right or wrong?
This:
B(A _a) : a(_a) {
cout << "B Con" << endl;
}
calls A's copy constructor, which you have not defined and which does not print anything. Add this to A:
A(A const&) { cout << "A Con (Copy)" << endl; }
And while you're at it, you may want A(A&&), A& operator=(A const&) and A& operator=(A&&) as well.
Full disclaimer, this is homework - not graded, just given to students so we can practice.
I'm asking for help, because we won't get an answer and I just want to know how to solve it.
What I can do is define structures B and C. Their interface has to be "like A's interface, with modifications so it works correctly".
I can't add any new methods. I also can't change anything in struct A.
This is the code:
#include <iostream>
struct A;
struct B;
struct C;
struct A {
A() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
int main(){
C c;
}
And the desired output is:
A::A()
A::A()
B::B()
A::A()
A::A()
B::B()
A::A()
C::C()
What I tried to do so far:
First I started like this, just to check things out:
struct B : public A {
B() : A() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
struct C : public B {
C() : B() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
Which gave me:
A::A()
B::B()
C::C()
So I tried to get first C, then A, then B, then A (desired output from the bottom):
struct B : public virtual A {
B() : A() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
struct C : public B, public A {
C() : A(), B() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
But of course this didn't work. (warning: direct base āAā inaccessible in āCā due to ambiguity)
Adding virtual keyword like below gave me again C, B then A, from the bottom:
struct B : public virtual A {
B() : A() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
struct C : public virtual A, public B {
C() : A(), B() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
If I want to get C, then A I have to do the following (but then there will be no B)
struct C : public virtual A {
C() : A() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
struct B : public virtual A, public C {
B() : C(), A() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
I also have no idea how could I get A::A() twice in a row.
I just want to understand how this should work, but if you still feel like you don't want to help me with a solution, then please leave me some tips.
I do not see a restriction in the exercise on use of class members:
struct A
{
A() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
};
struct B : A
{
B() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
};
struct C
: B
, A
{
C() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
B b;
A a;
};
UPDATE
The desired output had changed since my first answer was posted. So previous answer had become wrong. But now you see the point - you can use composition and copy A a; to B definition and remove A inheritance from C definition.
You can do something like:
struct B : A { //inherit from A
B() {
A();
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
struct C : B { //inherit from B
C() {
B(); //anonymous B object
A(); //anonymous A object
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
Running sample
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout << "A ctor" << endl;
}
virtual ~A()
{
cout << "A dtor" << endl;
}
virtual void foo() = 0;
};
class B : public A
{
public:
B()
{
cout << "B ctor" << endl;
}
virtual ~B()
{
cout << "B dtor" << endl;
}
virtual void foo()
{
cout <<"B's foo" << endl;
}
};
class C : public A
{
public:
C() {
cout << "C ctor" << endl;
}
virtual ~C()
{
cout << "C dtor" << endl;
}
virtual void foo() {cout << "C's foo" << endl;
}
};
int main ()
{
C *ptr = new C[1];
B b;
return 0;
}
This gives the following output:
A ctor
C ctor
A ctor
B ctor
B dtor
A dtor
I don't understand why this is happening. For example, I know that a new C object is being created, that's derived from A, so the A ctor runs first. Then the C ctor runs. And then I thought the C dtor runs, but for some reason the A ctor is running again.
C is created, this constructs A (base class) and then C
B is created, this constructs A (base class) and then B
B is destroyed (goes out of scope), this destructs B and then A (base class)
C is never deleted, so it's leaked and the destructors are never called.
I'm playing with construting/destructing object. Here is what I've tried http://coliru.stacked-crooked.com/a/ff17cc5649897430:
#include <iostream>
struct B{
B(){ std::cout << "B()" << std::endl; }
B(int){ std::cout << "B(int)" << std::endl; }
};
struct A : virtual B
{
int B;
A(int a) : B(a) { std::cout << "A(int)" << std::endl; }
} a(10);
int main()
{
}
The program output is
B()
A(int)
Why? I explicitly specify the constructor of the class B to be invoked in the ctor-initializer.
The B(a) is constructing the B member variable. Name your variables better and you'll see what you want to see.
I have a function that gets an object of 'A', it's called: func. I send it an object of B.
Shouldn't it activate the constructor of B? cause I send an object of B although I have to send an object of A. I really don't know why this function prints:
A()
DESTRUCTOR A
and not:
CONSTRUCTOR A
CONSTRUCTOR B
B()
DESTRUCTOR B
DESTRUCTOR A
doesn't it have to multiply the object B because the func gets A a and not A& a?
this is my code:
class A
{
public:
A() { cout << "CONSTRUCTOR A\n"; }
~A() { cout << "DESTRUCTOR A\n"; }
virtual void f() { cout << "A()" << endl; }
};
class B: public A
{
public:
B() { cout << "CONSTRUCTOR B\n"; }
~B() { cout << "DESTRUCTOR B\n"; }
virtual void f() { cout << "B()" << endl; }
};
void func(A a) {
a.f();
}
int main() {
B b;
func(b);
return 0;
}
Because if you send son but the function asks for the father class it treats the child as a father.
class A
{
public:
A() { cout << "CONSTRUCTOR A\n"; }
~A() { cout << "DESTRUCTOR A\n"; }
virtual void f() { cout << "A()" << endl; }
};
class B: public A
{
public:
B() { cout << "CONSTRUCTOR B\n"; }
~B() { cout << "DESTRUCTOR B\n"; }
virtual void f() { cout << "B()" << endl; }
};
void func(B a) {
a.f();
}
int main() {
B b;
func(b);
return 0;
}
if you try this code you see the result you expect.
I think the reason that you are confused is because you are passing by value instead of a pointer. That is why the B object is being treated as an A, since func takes a value of A it treats the object as A by default, which also means that the wrong destructor is being called.Run this code and you will understand better.
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "CONSTRUCTOR A\n"; }
~A() { cout << "DESTRUCTOR A\n"; }
virtual void f() { cout << "A()" << endl; }
};
class B: public A
{
public:
B() { cout << "CONSTRUCTOR B\n"; }
~B() { cout << "DESTRUCTOR B\n"; }
virtual void f() { cout << "B()" << endl; }
};
void func(A * a) {
a->f();
}
int main() {
B * b = new B();;
cout << "\n";
func(b);
cout << "\n";
delete b;
return 0;
}
This is basically the point of virtual functions, when using inheritance you want to able able to pass everything in as a parent class pointer. However the method you want called is the child classes method. When you pass by value the object has to be treated as what it is passed in as.