Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Regarding Constructor Calling in C++

Regarding Constructor Calling in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15229174
    wrote on last edited by
    #1

    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/?

    D M CPalliniC 3 Replies Last reply
    0
    • M Member_15229174

      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/?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • D David Crow

        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

        M Offline
        M Offline
        Member_15229174
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • M Member_15229174

          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/?

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          you C class does not have a constructor with 2 parameters.

          CI/CD = Continuous Impediment/Continuous Despair

          1 Reply Last reply
          0
          • M Member_15229174

            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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Since all the member variables are public you only need a constructor in C. And it would still work without that.

            1 Reply Last reply
            0
            • M Member_15229174

              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/?

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              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

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups