Click here to Skip to main content
13,705,212 members
Rate this:
 
Please Sign up or sign in to vote.
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 14-Jun-18 8:59am
Updated 14-Jun-18 21:43pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

e3=e1.add(e2);
Seeing this statement and knowing that all applies to the class ex, the function prototype must be
ex add(const ex& e1) const;
and the corresponding implementation is
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)
ex add(const ex& e1) const
{
    return ex(this->a + e1.a, this->b + e1.b);
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 15 Jun 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100