Consider the definition of the following class:
class Sample
{
private:
int x;
double y;
public :
Sample(); //Constructor 1
Sample(int); //Constructor 2
Sample(int, int); //Constructor 3
Sample(int, double); //Constructor 4
};
i. Write the definition of the constructor 1 so that the private member variables are initialized to
0.
ii. Write the definition of the constructor 2 so that the private member variable x is initialized
according to the value of the parameter, and the private member variable y is initialized to 0.
iii. Write the definition of the constructors 3 and 4 so that the private
member variables are initialized according to the values of the parameters.
// i.
Sample :: Sample()
{
this->x = 0;
this->y = 0;
}
// ii.
Sample :: Sample(int a)
{
this->x = a;
this->y = 0;
}
// iii.
Sample :: Sample(int a, int b)
{
this->x = a;
this->y = b;
}
Sample :: Sample(int a, double b)
{
this->x = a;
this->y = b;
}
Comments
Leave a comment