Regarding Constructor Calling in C++
-
class A { public: int a; }; class B:public class A { public: int b; } class C: public class B { public: B obj; } void main() { C obj1(10,20); } How do you write constructor for all three class so that the main will get called/?
-
class A { public: int a; }; class B:public class A { public: int b; } class C: public class B { public: B obj; } void main() { C obj1(10,20); } How do you write constructor for all three class so that the main will get called/?
Member 15229174 wrote:
...so that the main will get called/?
main()
is called by the OS/framework, not you. If, instead, you wanted to know how to call the base class constructor, how about something like:class C : public B
{
public:
C(int x, int y) : B(x, y)
{
}// B obj;
};
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Member 15229174 wrote:
...so that the main will get called/?
main()
is called by the OS/framework, not you. If, instead, you wanted to know how to call the base class constructor, how about something like:class C : public B
{
public:
C(int x, int y) : B(x, y)
{
}// B obj;
};
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Actually an interviewer asked me this question and told that class C contains the object of B class and through main C obj1(10,20) should be passed. Now you have to write constructor in all three class so that in main() function, it will not give an error.
-
class A { public: int a; }; class B:public class A { public: int b; } class C: public class B { public: B obj; } void main() { C obj1(10,20); } How do you write constructor for all three class so that the main will get called/?
you C class does not have a constructor with 2 parameters.
CI/CD = Continuous Impediment/Continuous Despair
-
Actually an interviewer asked me this question and told that class C contains the object of B class and through main C obj1(10,20) should be passed. Now you have to write constructor in all three class so that in main() function, it will not give an error.
-
class A { public: int a; }; class B:public class A { public: int b; } class C: public class B { public: B obj; } void main() { C obj1(10,20); } How do you write constructor for all three class so that the main will get called/?
Actually
class C
constructor should have had four (Yes,4
, since it contains four variables) parameters. Anyway... Try#include using namespace std;
class A
{
public:
int a;
A(int a):a(a){}
};class B: public A
{
public:
int b;
B(int a, int b): A(a), b(b){}
};class C: public B
{
public:
B obj;
C(int a, int b): B(a, b), obj(b, a){} // swapped the arguments of 'obj' just to make it a little different
friend ostream & operator << (ostream & os, const C & c); // overload the insertion operator just to show this object content
};ostream & operator << (ostream & os, const C & c)
{
os << "a = " << c.a << ", b = " << c.b << ", obj.a = " << c.obj.a << ", obj.b = " << c.obj.b;
return os;
}int main()
{
C obj1(10,20);
cout << obj1 << endl;
}"In testa che avete, Signor di Ceprano?" -- Rigoletto