Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For example::
#include<iostream.h>
class ex
{
public:
ex()
{
a=0;
b=0;
}
ex(
..
...
}
};
void main()
{
ex e1(3,5), e2(5,7),e3;
e3=e1.add(e2);
}

The answer should be 8 q2..
The x of e1 should be added to x of e2....and y of e1 should be added to y of e2...
Thanks ... a s a p..

What I have tried:

I have tried but I didn't got it
Posted
Updated 14-Jun-18 21:43pm

1 solution

C++
e3=e1.add(e2);
Seeing this statement and knowing that all applies to the class ex, the function prototype must be
C++
ex add(const ex& e1) const;
and the corresponding implementation is
C++
ex ex::add(const ex& e1) const
{
    ex result;
    result.a = this->a + e1.a;
    result.b = this->b + e1.b;
    return result;
}
or shorter using the constructor (and to be placed in the class definition)
C++
ex add(const ex& e1) const
{
    return ex(this->a + e1.a, this->b + e1.b);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900