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".../
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,
_
Re: help plz constructor design
Quote:
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.
Code:
.cpp:
#include <iostream>
#include"constructor_add_in.h"
using namespace std;
int main()
{
// two methods are here:solving on the .h side and om the .cpp side
Ships ships;
cout<<ships.x<<endl; //
cout<<"------------------------"<<endl;
cout<<ships.destroyer_1 * ships.submarine_1<<endl; // in line
cout<<"------------------------"<<endl;
ships.mult(ships.destroyer_1, ships.submarine_1); // .h fx
cout<<"----------------------------------"<<endl;
cout<<ships.solution<<endl;
cout<<"----------------------------------"<<endl;
ships.add(ships.barge_1, ships.aircraft_carrier_1);
cout<<ships.solution<<endl;
cout<<ships.barge_1 + ships.aircraft_carrier_1<<endl;
cout<<"-------------------------------------------"<<endl;
ships.mult(6,5);
cout<<ships.solution<<endl;
cout<<"<---------------------------------------------->"<<endl;
ships.add (4,5);
cout<<ships.solution;
return 0;
}#ifndef CONSTRUCTOR_ADD_IN_H_
#define CONSTRUCTOR_ADD_IN_H_
class Ships
{
//
private:
int destroyer = 303; int aircraft_carrier = 25; int submarine = 456; int barge = 57;
public:
int x = barge; int solution = 0;
int destroyer_1 = destroyer; int submarine_1 = submarine;
int aircraft_carrier_1 = aircraft_carrier;
mult (int a, int b){solution = a * b;};
add (int c, int d){solution = c + d; };
int barge_1 = barge;
int product_s = 0;
};
#endif /* CONSTRUCTOR_ADD_IN_H_ */
Re: help plz constructor design
Quote:
Originally Posted by
T1001
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,
_
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
...
Code:
ships.add (4,5);
cout<<ships.solution;
return 0;
}
Ships::Ships() {
// TODO Auto-generated constructor stub
}
Ships::~Ships() {
// TODO Auto-generated destructor stub
}
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?
Quote:
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.
Re: help plz constructor design
Quote:
Originally Posted by
T1001
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,
_
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?
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
Code:
Ships::Ships() {
int x = barge; int solution = 1200;
int destroyer_1 = destroyer; int submarine_1 = submarine;
int aircraft_carrier_1 = aircraft_carrier;
//mult (int a, int b){solution = a * b;};
//add (int c, int d){solution = c + d; };
int barge_1 = barge;
int product_s = 0;
// TODO Auto-generated constructor stub
}
error: primary
Ships::~Ships() {
// TODO Auto-generated destructor stub
}
Code:
src\main.cpp:42:15: error: expected primary-expression before 'int'
mult (int a, int b){solution = a * b;};]
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:
Code:
// Example.h
class Example
{
public:
Example();
~Example();
private:
int a;
int b;
}
Then if you wanted to initialize the class member variables via the constructor initializer list, you would:
Code:
// Example.cpp
Example::Example()
: a(0)
, b(0)
{
}
Example::~Example()
{
}
If you wanted to initialze the same member variables via assignment in the construtor, you would do:
Code:
// Example.cpp
Example::Example()
{
a = 0;
b = 0;
}
Example::~Example()
{
}
Quote:
Originally Posted by
T1001
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
Code:
Ships::Ships() {
int x = barge; int solution = 1200;
int destroyer_1 = destroyer; int submarine_1 = submarine;
int aircraft_carrier_1 = aircraft_carrier;
//mult (int a, int b){solution = a * b;};
//add (int c, int d){solution = c + d; };
int barge_1 = barge;
int product_s = 0;
// TODO Auto-generated constructor stub
}
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
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.
Re: help plz constructor design
Quote:
Originally Posted by
jefftee
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:
Code:
// Example.h
class Example
{
public:
Example();
~Example();
private:
int a = 1; // <==
int b = 2; // <==
}
Re: help plz constructor design
Quote:
Originally Posted by
wysota
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. :)
Re: help plz constructor design
Quote:
Originally Posted by
T1001
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
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,
_
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 .