classes

That's my third lesson of classes and I am having trouble with my HW,
I've to create an object with 2 properties and the object has to return addition, subtraction, division and multiplication.

Any recommendation or reference will be recognized !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class operation
{
	int x;
	int y;
	
	public:
		
		void setXm(){cout<<"Enter x= "; cin>>this->x; }
		void setXa(int x){ this->x=x;}
		
		void setYm(){cout<<"Enter y= "; cin>>this->y; }
		void setYa(int y){ this->y=y;}
		
		int const getX() const { return this->x;}
		int const getY() const { return this->y;}
		
		explicit operation(int x=NULL){this->x=x; this->y=NULL;}
		
		
		void addition(){this->x+=this->y;}
		
		void subtraction(){this->x-=this->y;}
		
		void division(){this->x/=this->y;}
		
		void multiplication(){this->x*=this->y;} 
		
		~operation(){ cout<<" Destructor !! "<<endl;}
};

int main()
{
	operation A;
	
	A.setXm();
	A.setYm();
	
	cout<<"A.x= "<<A.getX()<<endl;
	cout<<"A.y= "<<A.getY()<<endl;
	cout<<endl;
	
	A.addition(); cout<<A.getX()<<endl;
	A.subtraction(); cout<<A.getY()<<endl;
	A.division(); cout<<A.getX()<<endl;
	A.multiplication(); cout<<A.getY()<<endl;
	
	
}
Hello MaxGreen,

Initial observations:

"this->" is not needed in this context as the public member functions already have access to "x" and "y". It does have its use on occasion, but if you feel it is needed here it should not make any difference.

The set functions generally are written as:
1
2
3
4
void setXm(int value)
{
    x = value;
}

Then you would use A.setXm(10);.

My first thoughts until I have a cchance to go over it more.

Andy
> and the object has to return addition, subtraction, division and multiplication.
I guess the keyword being 'return'.

As in
 
int addition(){ return this->x + this->y;}



So you can do
 
  cout << A.addition() << endl;

the combined assignment operators modify the values.
x*=y changes x, in other words... so after you do the first one, x and y have changed and your results will not be what you expected.
this ties in with the above post, but may explain what is going on if you are seeing odd results.

line 17 is odd. Yes, null is really just zero, but it is only really supposed to be used with pointers, and you have no pointers (and c++ has moved to nullptr, leaving null for C code).

on the 'this' thing:

void setYa(int y){ this->y=y;} //this is necessary here, because you need to say which y you need in which place.

void setYa(int input){ y= input;} //if you use any other name than y, you don't need the this clutter.
I recommend in all code, whether in classes or not, to avoid having the same or even very similar variable names (with a few exceptions like for loop scope variables). The more code you have in a project, the more difficult it becomes to spot errors about which copy of the same name you meant to be using or which variable you wanted when there are several with similar names. Again use common sense on exceptions to this ... we had constants with names like OOOI OOIO OOII set of 'binary' codes in an embedded project once :P
Last edited on
Hello MaxGreen,

Consider this as a possibility:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <iomanip>

class operation
{
    int m_x;
    int m_y;

    public:
        void setXm() { std::cout << "Enter m_x= "; std::cin >> m_x; }  // <--- Maybe rename to "getInput"
        void setXa(int value) { m_x = value; }

        void setYm() { std::cout << "Enter m_y= "; std::cin >> m_y; }
        void setYa(int value) { m_y = value; }

        int const getX() const { return m_x; }
        int const getY() const { return m_y; }

        explicit operation(int x = 0) { m_x = x; m_y = 0; }


        int addition() { return m_x + m_y; }

        int subtraction() { return m_x - m_y; }

        double division()
        {
            if (!m_y)
            {
                return (std::cerr << "\n     Division by (0) not allowed! "), 0.0;
            }

            return static_cast<double>(m_x) / m_y;
        }

        int multiplication() { return m_x * m_y; }

        ~operation() { std::cout << " Destructor !! " << '\n'; }
};

int main()
{
    operation A;

    // <--- Get input values here or maybe rename the function to  "getInput".
    A.setXa(10);
    A.setYa(20);

    std::cout << std::fixed << std::setprecision(3);

    std::cout << "A.m_x = " << A.getX() << '\n';
    std::cout << "A.m_y = " << A.getY() << '\n';
    std::cout << '\n';

    std::cout << " Addition is: " << A.addition() << '\n';
    std::cout << " Subtraction is: " << A.subtraction() << '\n';
    std::cout << A.division() << '\n';
    std::cout << A.multiplication() << '\n';

    return 0;
}

Both "setXa"and setYa" could be combined into 1 function.

By changing the variable names in the class it should eliminate the need "this->" for what you are doing.


Something to think about.

Andy
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>

class operation
{
	int m_x {};
	int m_y {};

public:
	explicit operation(int x = 0) : m_x(x) {}
	operation(int x, int y) : m_x(x), m_y(y) {}

	void setXm() { std::cout << "Enter m_x= "; std::cin >> m_x; }
	void setXa(int value) { m_x = value; }

	void setYm() { std::cout << "Enter m_y= "; std::cin >> m_y; }
	void setYa(int value) { m_y = value; }

	int const getX() const { return m_x; }
	int const getY() const { return m_y; }

	int addition() const { return m_x + m_y; }

	int subtraction() const { return m_x - m_y; }

	double division() const {
		return !m_y ? (std::cerr << "\nDivision by 0 not allowed!\n"), NAN : (m_x + 0.0) / m_y;
	}

	int multiplication() const { return m_x * m_y; }
};

int main()
{
	const operation A(10, 20);

	std::cout << std::fixed << std::setprecision(3);
	std::cout << "A.m_x = " << A.getX() << '\n';
	std::cout << "A.m_y = " << A.getY() << "\n\n";
	std::cout << "Add: " << A.addition() << '\n';
	std::cout << "Sub: " << A.subtraction() << '\n';
	std::cout << "Div: " << A.division() << '\n';
	std::cout << "Mul: " << A.multiplication() << '\n';
}

Thank you very much indeed,
Great help from great people!
Topic archived. No new replies allowed.