Answer to Question #203735 in C++ for python

Question #203735

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.


1
Expert's answer
2021-06-08T02:13:27-0400
// 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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog