OOP quick question (constructors..)

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Lattice
{
    private:
        //private members
        Site site[ NSITES ];
        Atom atom[ NSITES ];                               /* ONE IS VACANCY   -CONFUSING-  */
        double lifeTime;

    public:
        //constructor
        Lattice();

        //destructor
        ~Lattice();


Question:
does it make sense that I manually called the contructors / initialise functions for site[ NSITES ] and atom[ NSITES ] ? or are they called automatically? in this case how can I use an overloaded constructor instead? would that be called AFTER the default constructors anyway?

Thanks a lot,
Clodi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Circle
{
public:
	Circle()
	{
		radius = 1; //Default
	}

	Circle(double newRadius)
	{
		radius = newRadius;//Argumented
	}

	double getArea()
	{
		return radius * radius * 3.14159;
	}

private:
	double radius;
};
OK thanks.
This helps.
So basically you SET with default and ADJUST with overloaded. No way around it. Thanks a lot
If you're trying to avoid writing another constructor, you can use a defaulted parameter:

1
2
3
4
5
6
7
class Circle
{  double radius;
public:
    Circle (double r = 1)
    {  radius = r;   // If constructor called with no args, radius is 1
    }
};
Last edited on
nice. Thanks a lot. good to know
Another thing new in C++11 is constructor delegation:

1
2
3
4
5
6
7
8
9
10
class Circle
{
public:
	Circle() : Circle(1) {} //Default value would look better, but this is for sake of example

	Circle(double newRadius) : radius(newRadius) {} //Using member initializer list

private:
	double radius;
};
Last edited on
Topic archived. No new replies allowed.