Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Formulate the Math class so that it is not possible to create or make copies of

ID: 3918471 • Letter: F

Question

Formulate the Math class so that it is not possible to create or make copies of objects of the class Math. According to the instruction, it shouldn't be possible to create or make copies of objects of the Math class.

#include<iostream>
using namespace std;

class Math
{
public:
static const double pi;
static const double root2;
};

const double Math::pi = 3.1416;
const double Math::root2 = 1.4142;

int main()
{
cout << "pi value : " << Math::pi << endl;
cout << "root2 value : " << Math::root2 << endl;


return 0;
}

Explanation / Answer

#include<iostream>
using namespace std;

class Math
{
public:
static const double pi;
static const double root2;
};

const double Math::pi = 3.1416;
const double Math::root2 = 1.4142;

int main()
{
cout << "pi value : " << Math::pi << endl;
cout << "root2 value : " << Math::root2 << endl;


return 0;
}

From the above program :

Math class it is possible to create or make copies of objects because of static declaration.

We can remove static then Math class is not possible to create or make copies of objects of the class Math.

Program:

#include<iostream>

using namespace std;

class Math

{

public:

const double pi;

const double root2;

};

const double Math::pi = 3.1416;

const double Math::root2 = 1.4142;

int main()

{

cout << "pi value : " << Math::pi << endl;

cout << "root2 value : " << Math::root2 << endl;

return 0;

}

The above program is not possible to create or make copies of objects

It raises an error

invalid use of non-static data member 'Math::pi' cout << "pi value : " << Math::pi << endl;