Passing object of derived class to base class - c++

Hi I have a derived class and I want to pass the pointer of the derived class object to the base class.
I am getting segmentation fault while running the code.
#include <iostream>
using namespace std;
class A {
public:
virtual void x() = 0;
A* a;
};
class B: public A {
public:
B(A* x) {
a = x;
}
void x () {}
};
class C: public B {
public:
C() : B(new C) { }
};
int main() {
C c;
return 0;
}
Can someone help me suggest a way to achieve this or help me in fixing the code.

I believe you want to pass a pointer to current object. This is what this is for.
C() : B(this) { }

Related

why am i getting error while inheriting template class [duplicate]

How can I access base class variable from a child method? I'm getting a segmentation fault.
class Base
{
public:
Base();
int a;
};
class Child : public Base
{
public:
void foo();
};
Child::Child() :Base(){
void Child::foo(){
int b = a; //here throws segmentation fault
}
And in another class:
Child *child = new Child();
child->foo();
It's not good practice to make a class variable public. If you want to access a from Child you should have something like this:
class Base {
public:
Base(): a(0) {}
virtual ~Base() {}
protected:
int a;
};
class Child: public Base {
public:
Child(): Base(), b(0) {}
void foo();
private:
int b;
};
void Child::foo() {
b = Base::a; // Access variable 'a' from parent
}
I wouldn't access a directly either. It would be better if you make a public or protected getter method for a.
Solved! the problem was that I was calling Child::foo() (by a signal&slot connection) from a non yet existing object.
Thanks for the aswers.
class Base
{
public:
int a;
};
class Child : public Base
{
int b;
void foo(){
b = a;
}
};
I doubt if your code even compiled!

Error: cannot convert '<unresolved overloaded function type>' to 'function pointer'

#include<iostream>
#include<bitset>
#include<string.h>
#include<string>
#include<vector>
#include<math.h>
#include<stdarg.h>
class b {
public:
b();
void ef(void(*f)());
};
class d : public b{
public:
d();
void print();
};
b::b() {}
void b::ef(void(*f)()) { f(); }
d::d(): b(){}
void d::print() { cout<<"WORKS"<<endl; }
int main() {
d obj;
obj.ef(obj.print);
}
I have a problem with a derived class method, I would execute d::print() as parameter of b::ef(), compiling I got this error:
"error: cannot convert '' to
'void (*)()'"
Can you help me to fix it? Thank you
Here's your code, fixed. But I can see a dozens of things "wrong" with it, in the sense "does what you asked, but not what you (probably) want."
The print method needs to act on a d object. But that means the base class has to know about the derived class. That's awkward. If the base class had a virtual print function, then it could have that passed in, and would call the derived classes override of that virtual function. But that's not what we have here.
#include <iostream>
using std::cout;
namespace {
class d;
class b {
public:
b();
void ef(d&, void(d::*)());
};
class d : public b {
public:
d();
void print();
};
b::b() {}
void b::ef(d& dobj, void(d::*f)()) {
(dobj.*f)();
}
d::d() : b() {}
void d::print() {
cout << "WORKS\n";
}
} // anon
int main() {
d obj;
obj.ef(obj, &d::print);
}

Accessing the variable of the base class from derived class in Hybrid Inheritance

I have written a C++ program.
#include <iostream>
#include <string>
using namespace std;
class A
{
int a;
public:
void read()
{
cin>>a;
}
};
class B:public A
{
public:
B()
{ }
};
class C:public A
{
public:
C()
{}
};
class D:public B,public C
{
public:
void display()
{
cout<<a<<endl;
}
};
void main()
{
A a1;B b1;C c1;
D d1;
d1.display();
}
I get a message saying there is ambiguity. B is inheriting A.. So B class would inherit item 'a' from class A. Also, C class will inherit item 'a' from class A. Now if im trying to inherit both B and C classes in class D, which item 'a' will i be able to access ?? from class B or from class C ?? Is there any way where i can remove the ambiguity .?
You can fix your ambiguity problem by using one of the following options:
Be explicit about the base class
Use B as the explicit base class
void display()
{
cout<< B::a <<endl;
}
or use C as the explicit base class.
void display()
{
cout<< C::a <<endl;
}
Use virtual inheritance
Change B and C to use virtual inheritance.
class B: virtual public A
{
public:
B() {}
};
class C: virtual public A
{
public:
C() {}
};

Why output is showing errors

This is a code fro diamond tree problem of multiple inheritance
and according to me this code is cool but it is showing some error on compilation
..help me to figure the error
#include<iostream>
using namespace std;
class A //A Diamond tree problem
{
int x;
public:
A(int i) { x = i; }
void print() { cout << x; }
};
class B: virtual public A
{
public:
B():A(10) { }
};
class C: virtual public A
{
public:
C():A(20) { }
};
class D: public B, public C{
};
int main()
{
D d;
d.print();
return 0;
}
It would be useful to see the error:
In constructor ‘D::D()’:
error: no matching function for call to ‘A::A()’
When using virtual inheritance, the virtual base class must be initialised by the most derived class. In this case, that is D; so in order to be able to instantiate D, it must initialise A:
class D: public B, public C
{
public:
D():A(42) {}
};
Alternatively, you could provide A with a default constructor. Declaring any constructor will prevent the compiler from implicitly generating one for you.
You need to provide default construct for D and call A in member initialize list:
class D: public B, public C{
public:
D():A(30){}
};
Or you could provide a default A constructor
A():x(0) {}

How to initialize an object of derived class when the derived class and base class both have parameterized constructors?

I am facing problem in initializing objects.
Following is a piece of code,
#include <iostream>
#include <conio.h>
using namespace std;
class Base
{
public:
Base(int a)
{
m_a = a;
}
private:
int m_a;
};
class Derived:public Base
{
public:
Derived(char a)
{
m_a = a;
}
private:
char m_a;
};
void main()
{
_getch();
}
Compiling the above code gives the following error,
error C2512: 'Base' : no appropriate default constructor available
I know that since derived class and base class both have only parametrized constructors i need to initialize the base class object in derived class constructor. But i don't know how to do it.
Can anyone please tell me as to what is wrong in the above code?
public:
Derived(char a):Base(/*int Parameter*/),m_a(a)
{
}
After making trails i one more way to initialize the base class too,
Following is the code,
#include <iostream>
#include <conio.h>
using namespace std;
class Base
{
public:
Base(int a)
{
m_a = a;
}
private:
int m_a;
};
class Derived:public Base
{
public:
Derived(int b, char a):Base(b)
{
m_a = a;
}
private:
char m_a;
};
void main()
{
Derived d(10,'A');
_getch();
}