Constructed class in constructed class

Jan 8, 2012 at 4:00am
Quick question. Suppose I have a class:
1
2
3
4
5
class data {
public:
	double x, y, z, blah;
	data ();
};

with the constructor:
1
2
3
4
5
6
data::data () {
	x = 0;
	y = 0;
	z = 0;
	blah = 1;
}

In another class (and constructor), the data class is used:
1
2
3
4
5
6
7
8
class otherclass {
public:
	// other variables
	data moardata;
otherclass () {
	//constructing class
}
};


would I need to construct moardata.x, moardata.y, etc if I wanted them to be zero? Or would the constructor for data affect each member declared in otherclass?

Thanks!
Last edited on Jan 8, 2012 at 4:00am
Jan 8, 2012 at 4:04am
Yes. Your data constructor would set them all to zero for each otherclass.
Last edited on Jan 8, 2012 at 4:05am
Jan 8, 2012 at 4:07am

would I need to construct moardata.x, moardata.y, etc if I wanted them to be zero?


No. On creating an object of otherclass, the default constructor of the class data will be called which already does what you want to.
Topic archived. No new replies allowed.