Results 1 to 14 of 14

Thread: help plz constructor design

  1. #1
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default help plz constructor design

    I have a general question abt design. So far I have been able to split my code between xxxx.cpp (implementation) and xxxx.h (declarations) adequately, but by using default constructors. I now want to build my own constructors with variable and member lists.
    Do I have to use scope operators? Where do I put the constructors? How do I do this? I've looked all over the internet but can't find what I need. I am building my "tool chest".../
    All Your Base Are Belong To Us

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: help plz constructor design

    I am not sure what you mean.

    The constructor is not much different to a normal method other than being used to initialize the object.
    It can as easily be declared in the header and implemented in the source file.

    Just consider them methods without return type.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: help plz constructor design

    It can as easily be declared in the header and implemented in the source file.
    That is my problem exactly. Thank you for narrowing the solution. How would I declare it in the .h and implement it in the source file. If I try to do that my code goes wild with "not a member errors". I'll post the code. Could you give me an example of how you would write the .h to include the constructor?

    Here is a working, clean copy. I tried installing a constructor, not shown. I think my problem was trying to initialize it from the .h file.

    At public: I try to install Class( + member variables) and it tanks. I can upload the modified program as well.
    Qt Code:
    1. .cpp:
    2. #include <iostream>
    3. #include"constructor_add_in.h"
    4.  
    5. using namespace std;
    6.  
    7. int main()
    8. {
    9. // two methods are here:solving on the .h side and om the .cpp side
    10. Ships ships;
    11. cout<<ships.x<<endl; //
    12. cout<<"------------------------"<<endl;
    13. cout<<ships.destroyer_1 * ships.submarine_1<<endl; // in line
    14. cout<<"------------------------"<<endl;
    15. ships.mult(ships.destroyer_1, ships.submarine_1); // .h fx
    16. cout<<"----------------------------------"<<endl;
    17. cout<<ships.solution<<endl;
    18. cout<<"----------------------------------"<<endl;
    19. ships.add(ships.barge_1, ships.aircraft_carrier_1);
    20. cout<<ships.solution<<endl;
    21. cout<<ships.barge_1 + ships.aircraft_carrier_1<<endl;
    22. cout<<"-------------------------------------------"<<endl;
    23. ships.mult(6,5);
    24. cout<<ships.solution<<endl;
    25. cout<<"<---------------------------------------------->"<<endl;
    26. ships.add (4,5);
    27. cout<<ships.solution;
    28.  
    29. return 0;
    30.  
    31. }#ifndef CONSTRUCTOR_ADD_IN_H_
    32. #define CONSTRUCTOR_ADD_IN_H_
    33.  
    34. class Ships
    35. {
    36. //
    37. private:
    38. int destroyer = 303; int aircraft_carrier = 25; int submarine = 456; int barge = 57;
    39. public:
    40. int x = barge; int solution = 0;
    41. int destroyer_1 = destroyer; int submarine_1 = submarine;
    42. int aircraft_carrier_1 = aircraft_carrier;
    43. mult (int a, int b){solution = a * b;};
    44. add (int c, int d){solution = c + d; };
    45. int barge_1 = barge;
    46. int product_s = 0;
    47.  
    48. };
    49.  
    50. #endif /* CONSTRUCTOR_ADD_IN_H_ */
    To copy to clipboard, switch view to plain text mode 
    All Your Base Are Belong To Us

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: help plz constructor design

    Quote Originally Posted by T1001 View Post
    try to do that my code goes wild with "not a member errors". I'll post the code.
    Your code does not have any constructor.

    And your methods are all inline in the header.

    Cheers,
    _

  5. #5
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: help plz constructor design

    Ok, I'll address the inline code. Thanks for your input.The main topic above was abt not having a constructor "design" which I need help with. I made a little more progress: this is the bottom of my main.cpp - (1) how do I initialize values with this constructor and isit fine with its current location?
    bottom of .cpp file
    ...
    Qt Code:
    1. ships.add (4,5);
    2. cout<<ships.solution;
    3. return 0;
    4.  
    5. }
    6.  
    7. Ships::Ships() {
    8.  
    9.  
    10.  
    11. // TODO Auto-generated constructor stub
    12.  
    13. }
    14.  
    15. Ships::~Ships() {
    16. // TODO Auto-generated destructor stub
    17. }
    To copy to clipboard, switch view to plain text mode 
    All Your Base Are Belong To Us

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: help plz constructor design

    If you put all code in one file what sense does it all make? How that relates to what you said earlier?

    So far I have been able to split my code between xxxx.cpp (implementation) and xxxx.h (declarations) adequately
    By the way, I moved the thread to "general programming". Please post on the right forum next time.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: help plz constructor design

    Quote Originally Posted by T1001 View Post
    how do I initialize values with this constructor and isit fine with its current location?
    bottom of .cpp file
    Well, you will want the code for Ship in a source file for ship, just like you have a header for the declaration of Ship.

    Now, which values do you want to initialize? You seem to initialize all members already.

    Cheers,
    _

  8. #8
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: help plz constructor design

    sorry abt that


    Added after 1 33 minutes:


    That's the thing -- how do I know they are being initialized at all(in other words how is it being done correctly I can't glean it from my own code). Where is my constructer?
    Last edited by T1001; 8th July 2015 at 23:19.
    All Your Base Are Belong To Us

  9. #9
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: help plz constructor design

    Okay, so I've successfully made a constructor in the main.cpp and seems to be working fine except for two of the inline functions:
    // by the fxs
    Qt Code:
    1. Ships::Ships() {
    2.  
    3.  
    4. int x = barge; int solution = 1200;
    5. int destroyer_1 = destroyer; int submarine_1 = submarine;
    6. int aircraft_carrier_1 = aircraft_carrier;
    7. //mult (int a, int b){solution = a * b;};
    8. //add (int c, int d){solution = c + d; };
    9. int barge_1 = barge;
    10. int product_s = 0;
    11.  
    12. // TODO Auto-generated constructor stub
    13.  
    14. }
    15.  
    16. error: primary
    17.  
    18. Ships::~Ships() {
    19. // TODO Auto-generated destructor stub
    20. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. src\main.cpp:42:15: error: expected primary-expression before 'int'
    2. mult (int a, int b){solution = a * b;};]
    To copy to clipboard, switch view to plain text mode 
    All Your Base Are Belong To Us

  10. #10
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: help plz constructor design

    Let me start by stating that you need to review some C++ tutorials or else you're going to drive yourself (and others) crazy asking such basic questions on these forums. That said, there are two common ways of initializing class member variables, using 1) The constructor initializer list or; 2) by assigning values in your constructor.

    Consider this Example.h class definition:

    Qt Code:
    1. // Example.h
    2. class Example
    3. {
    4. public:
    5. Example();
    6. ~Example();
    7. private:
    8. int a;
    9. int b;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Then if you wanted to initialize the class member variables via the constructor initializer list, you would:

    Qt Code:
    1. // Example.cpp
    2.  
    3. Example::Example()
    4. : a(0)
    5. , b(0)
    6. {
    7. }
    8.  
    9. Example::~Example()
    10. {
    11. }
    To copy to clipboard, switch view to plain text mode 

    If you wanted to initialze the same member variables via assignment in the construtor, you would do:

    Qt Code:
    1. // Example.cpp
    2.  
    3. Example::Example()
    4. {
    5. a = 0;
    6. b = 0;
    7. }
    8.  
    9. Example::~Example()
    10. {
    11. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by T1001 View Post
    Okay, so I've successfully made a constructor in the main.cpp and seems to be working fine except for two of the inline functions:
    // by the fxs
    Qt Code:
    1. Ships::Ships() {
    2.  
    3.  
    4. int x = barge; int solution = 1200;
    5. int destroyer_1 = destroyer; int submarine_1 = submarine;
    6. int aircraft_carrier_1 = aircraft_carrier;
    7. //mult (int a, int b){solution = a * b;};
    8. //add (int c, int d){solution = c + d; };
    9. int barge_1 = barge;
    10. int product_s = 0;
    11.  
    12. // TODO Auto-generated constructor stub
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    Those variables you define and initialize in lines 4-10 of your constructor are not class member variables, they're local variables created on the stack that disappear when your constructor has been executed. Class member variables must be defined as such and are defined in the class definition. See above for the Example class definition in Example.h. The two variables a and b are integer variables that are declared as private member variables.


    Added after 9 minutes:


    Quote Originally Posted by T1001 View Post
    I've looked all over the internet but can't find what I need.
    Of course that's a crazy statement. Google "c++ class construction" or "c++ class design". Pretty sure there are a few million hits at least.
    Last edited by jefftee; 9th July 2015 at 02:34.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: help plz constructor design

    Quote Originally Posted by jefftee View Post
    That said, there are two common ways of initializing class member variables, using 1) The constructor initializer list or; 2) by assigning values in your constructor.
    There is also a third one which the OP has actually used -- by initializing at the time the variable is declared:

    Qt Code:
    1. // Example.h
    2. class Example
    3. {
    4. public:
    5. Example();
    6. ~Example();
    7. private:
    8. int a = 1; // <==
    9. int b = 2; // <==
    10. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: help plz constructor design

    Quote Originally Posted by wysota View Post
    There is also a third one which the OP has actually used -- by initializing at the time the variable is declared
    I saw that in the OP's earlier post, but in his last post he showed all of the variable declarations in the constructor for some reason. My point was he's not declaring/initializing class member variables if they are declared in the constructor. I have no clue what may still exist in his header file or not.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: help plz constructor design

    Quote Originally Posted by T1001 View Post
    how do I know they are being initialized at all(in other words how is it being done correctly
    You had the initialized using C++11 inline syntax at their declaration the header.

    jefftee's comment contains two other options.

    Quote Originally Posted by T1001 View Post
    Where is my constructer?
    In your original code the default constructor got generated by the compiler.
    In your attempt with the IDE generated constructor stubs you had an explicit constructor that was basically equivalent to the what the compiler would have generated.

    In either case you had initialized members due to using the C++11 inline syntax.

    Cheers,
    _

  14. #14
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: help plz constructor design

    Hey guys thank you for your input. I am doing a c++ tutorial but hit a rough patch with this. I think I'fve got it from here .
    All Your Base Are Belong To Us

Similar Threads

  1. No appropriate default constructor
    By legolizard in forum Newbie
    Replies: 7
    Last Post: 19th June 2012, 23:58
  2. what does the constructor do here?
    By LB4229 in forum Qt Programming
    Replies: 7
    Last Post: 22nd June 2011, 03:19
  3. class constructor
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2008, 18:25
  4. Replies: 3
    Last Post: 5th October 2008, 23:41
  5. QImage constructor
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 12:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.