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. How to instantiate class with parameters - syntax please ?

How to instantiate class with parameters - syntax please ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++ooptutorialquestionlearning
13 Posts 4 Posters 0 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.
  • V Vaclav_

    C++ has very specific way / syntax to instantiate a class with parameters, so basic that Google can't find it for me . I need this for implementing multiple inheritance. I can instantiate the "top class" in multiple inheritance hierarchy , but it uses system generated default constructors, without parameters of course. My goal is to create an instance of the top class with parameters passed to the hierarchy lower chain classes. Do I make sense? I hope so. Thanks Vaclav

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

    class A
    {
    public :

    A( int p ) : a( p )
    {
    }
    

    protected:
    int a;
    };

    class B
    {
    public:

    B( int q ) : b( q )
    {
    }
    

    protected:
    int b;
    };

    class C : public A, B
    {
    public :

    C( int x, int y, int z ) : A( x ), B( y), c( z )
    {
        a = x; // also possible
    }
    

    private:

    int c;
    

    };

    int _tmain(int argc, _TCHAR* argv[])
    {
    C test( 1, 2, 3 );

    return 0;
    

    }

    Will this help?

    V 1 Reply Last reply
    0
    • L Lost User

      class A
      {
      public :

      A( int p ) : a( p )
      {
      }
      

      protected:
      int a;
      };

      class B
      {
      public:

      B( int q ) : b( q )
      {
      }
      

      protected:
      int b;
      };

      class C : public A, B
      {
      public :

      C( int x, int y, int z ) : A( x ), B( y), c( z )
      {
          a = x; // also possible
      }
      

      private:

      int c;
      

      };

      int _tmain(int argc, _TCHAR* argv[])
      {
      C test( 1, 2, 3 );

      return 0;
      

      }

      Will this help?

      V Offline
      V Offline
      Vaclav_
      wrote on last edited by
      #5

      Well, it does and does not help. I am probably wrong , but I was looking for something like this class TestClass : paramaterX = "Text" { } I am looking into multiple inheritance and was trying to verify that the classes in the scheme were actually constructed. Simple class A : public B,C,D setup. So I implemented default constructor and have it to output some text to the console, sort of. It does work. Now I am trying to find a way to instantiate the scheme with parameters passed to some of the hierarchy classes. something like class A : ( class A parameter list ), public B : ( class b parameter list)... Is that possible ? BTW I cannot figure out the real syntax / usage of the basic syntax class A : ? For example class A : text { }; the compiler wants to see class definition before "text", text is class A variable.

      L L 2 Replies Last reply
      0
      • V Vaclav_

        Well, it does and does not help. I am probably wrong , but I was looking for something like this class TestClass : paramaterX = "Text" { } I am looking into multiple inheritance and was trying to verify that the classes in the scheme were actually constructed. Simple class A : public B,C,D setup. So I implemented default constructor and have it to output some text to the console, sort of. It does work. Now I am trying to find a way to instantiate the scheme with parameters passed to some of the hierarchy classes. something like class A : ( class A parameter list ), public B : ( class b parameter list)... Is that possible ? BTW I cannot figure out the real syntax / usage of the basic syntax class A : ? For example class A : text { }; the compiler wants to see class definition before "text", text is class A variable.

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #6

        You could be trying to do several things with the multiple inheritance you need to be very precise here. You give us "parameterX" which is sort of labelled as text but that could mean a string class, a c string, or a static const array. You also don't tell us whether the place you want to store the passed text is in the object new to this class or part of one of the inherited classes, it may matter .. depends. Do you see how many variations there are on what your simple description says? This is what makes multiple inheritance tricky and you need to be very clear. If you search the words "C++ composite class" is that what you are trying to do? It appears to me you have jumped in at the deep end with C++ classes and trying to do the most complex without first understanding the basics. I would highly recommend you might like to also do a search of "C++ Parameterized Constructor" and review it given the code you have written above is nonsense. I would also recommend you look at Constructors (C++)[^] That covers the basics of construction order and explicit and derived constructors in multiple inheritance. My best guess you are trying to derive the base of a composite class and it would be this

        class TestClass {
        public:
        TestClass(const string& InitText) { text = InitText; };
        string text;
        };

        In vino veritas

        1 Reply Last reply
        0
        • V Vaclav_

          Well, it does and does not help. I am probably wrong , but I was looking for something like this class TestClass : paramaterX = "Text" { } I am looking into multiple inheritance and was trying to verify that the classes in the scheme were actually constructed. Simple class A : public B,C,D setup. So I implemented default constructor and have it to output some text to the console, sort of. It does work. Now I am trying to find a way to instantiate the scheme with parameters passed to some of the hierarchy classes. something like class A : ( class A parameter list ), public B : ( class b parameter list)... Is that possible ? BTW I cannot figure out the real syntax / usage of the basic syntax class A : ? For example class A : text { }; the compiler wants to see class definition before "text", text is class A variable.

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

          Hi,

          Vaclav_Sal wrote:

          something like class A : ( class A parameter list ), public B : ( class b parameter list)...Is that possible ?

          As a direct answer, I would say that, it is not possible and instead, we can use constructors with parameters.

          Vaclav_Sal wrote:

          BTW I cannot figure out the real syntax / usage of the basic syntax class A : ? For example class A : text{};

          Please have a look at Richard's link too.

          L 1 Reply Last reply
          0
          • L Lost User

            Hi,

            Vaclav_Sal wrote:

            something like class A : ( class A parameter list ), public B : ( class b parameter list)...Is that possible ?

            As a direct answer, I would say that, it is not possible and instead, we can use constructors with parameters.

            Vaclav_Sal wrote:

            BTW I cannot figure out the real syntax / usage of the basic syntax class A : ? For example class A : text{};

            Please have a look at Richard's link too.

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #8

            I just twigged what he is trying to do via you answer, he is trying to make a static composite class isn't he. Like when beginners to C, try to create a struct with constants within the struct definition.

            In vino veritas

            1 Reply Last reply
            0
            • V Vaclav_

              C++ has very specific way / syntax to instantiate a class with parameters, so basic that Google can't find it for me . I need this for implementing multiple inheritance. I can instantiate the "top class" in multiple inheritance hierarchy , but it uses system generated default constructors, without parameters of course. My goal is to create an instance of the top class with parameters passed to the hierarchy lower chain classes. Do I make sense? I hope so. Thanks Vaclav

              V Offline
              V Offline
              Vaclav_
              wrote on last edited by
              #9

              Let me try it again. I have an inheritance hierarchy class A : public B, C When I instantiate class A - A a; - all classes get constructed via default constructor provided by the language if I do not specify them. Now this is not much over my head , I don't see need to further educate myself in this basic. This inheritance scheme at that point works just fine. Next step - class B needs to be constructed using real constructor. Again I know how to pass parameters to class constructor. I just do not know how to do it , or can it be done during the construction of the class A. As far as taking on more than I can chew, that's my business. If you do not want to participate, that is your choice. No "google it" remarks are worthy of your and my time. I am sorry about the above remarks , but I am getting tired of folks with " my grandma can beat up yours " attitude. I just hate to see it popping up on this forum too. All I actually need to know is "composite class", and I can take it from there. Thanks

              L L 2 Replies Last reply
              0
              • V Vaclav_

                Let me try it again. I have an inheritance hierarchy class A : public B, C When I instantiate class A - A a; - all classes get constructed via default constructor provided by the language if I do not specify them. Now this is not much over my head , I don't see need to further educate myself in this basic. This inheritance scheme at that point works just fine. Next step - class B needs to be constructed using real constructor. Again I know how to pass parameters to class constructor. I just do not know how to do it , or can it be done during the construction of the class A. As far as taking on more than I can chew, that's my business. If you do not want to participate, that is your choice. No "google it" remarks are worthy of your and my time. I am sorry about the above remarks , but I am getting tired of folks with " my grandma can beat up yours " attitude. I just hate to see it popping up on this forum too. All I actually need to know is "composite class", and I can take it from there. Thanks

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

                Vaclav_Sal wrote:

                I am getting tired of folks with " my grandma can beat up yours " attitude.

                Much as we feel about you.

                1 Reply Last reply
                0
                • V Vaclav_

                  Let me try it again. I have an inheritance hierarchy class A : public B, C When I instantiate class A - A a; - all classes get constructed via default constructor provided by the language if I do not specify them. Now this is not much over my head , I don't see need to further educate myself in this basic. This inheritance scheme at that point works just fine. Next step - class B needs to be constructed using real constructor. Again I know how to pass parameters to class constructor. I just do not know how to do it , or can it be done during the construction of the class A. As far as taking on more than I can chew, that's my business. If you do not want to participate, that is your choice. No "google it" remarks are worthy of your and my time. I am sorry about the above remarks , but I am getting tired of folks with " my grandma can beat up yours " attitude. I just hate to see it popping up on this forum too. All I actually need to know is "composite class", and I can take it from there. Thanks

                  L Offline
                  L Offline
                  leon de boer
                  wrote on last edited by
                  #11

                  Sorry my mind reading was a bit off, I am however glad you worked out you were trying to build a composite class. The forum can help with specific small problems but it's not something that can teach complex techniques in C++.

                  In vino veritas

                  V 1 Reply Last reply
                  0
                  • L leon de boer

                    Sorry my mind reading was a bit off, I am however glad you worked out you were trying to build a composite class. The forum can help with specific small problems but it's not something that can teach complex techniques in C++.

                    In vino veritas

                    V Offline
                    V Offline
                    Vaclav_
                    wrote on last edited by
                    #12

                    Yes, this time using correct terminology was the ticket. I am not sure why the OP was judged as too complex to ask here about. I am still puzzled why I did not get a clear example of syntax passing list of parameters to class directly, not via constructor. The link posted was not that clear how to name / use the list. It really does matter to me how I get the parameters to correct class in the hierarchy. The example of composite class suggest I have to follow the hierarchy and pass the parameters thru each constructor to the next one. I suppose when I get the code to play I could post it here for everybody can see it is really not that complex. Cheers

                    L 1 Reply Last reply
                    0
                    • V Vaclav_

                      Yes, this time using correct terminology was the ticket. I am not sure why the OP was judged as too complex to ask here about. I am still puzzled why I did not get a clear example of syntax passing list of parameters to class directly, not via constructor. The link posted was not that clear how to name / use the list. It really does matter to me how I get the parameters to correct class in the hierarchy. The example of composite class suggest I have to follow the hierarchy and pass the parameters thru each constructor to the next one. I suppose when I get the code to play I could post it here for everybody can see it is really not that complex. Cheers

                      L Offline
                      L Offline
                      leon de boer
                      wrote on last edited by
                      #13

                      Correct The problem as you will find is hierarchy order as you will find. Sometimes the order will not be the way you want in different functions and you will need to control it and that gets down to some very nitty details. The question we will still always pose back to you is do you really need the hierarchy at all. I can't stress enough to read one article from wikipedia it can save you alot of heartache https://en.wikipedia.org/wiki/Composition_over_inheritance[^]

                      In vino veritas

                      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