operator *= function
Dec 10, 2012 at 12:16pm UTC
This program Should write by friend operator *= function .unfortunately It only answers triangle area.where are mistakes?plz correct this program and don't give another program.
Question:
Declare a class for Rectangle and Declare a class for Triangle.So compute area of this two and Declare a operator *= function that it multiplies area of triangle and rectangle and place value in triangle object.
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 62 63 64 65
#include <iostream>
#include <string>
using namespace std;
class triangle;
class rect
{
int a,b;
public :
int s1;
void input();
int calc();
friend triangle operator *=(triangle,rect );
};
void rect :: input()
{
cin>>a>>b;
}
int rect:: calc()
{
return s1=a*b;
}
class rect;
class triangle
{
int c,h,s2;
public :
void input();
int calc();
friend triangle operator *=(triangle,rect );
void show();
};
void triangle::input()
{
cin>>c>>h;
}
int triangle ::calc()
{
return s2=(c*h)/2;
}
void triangle::show()
{
cout<<s2;
}
int main()
{
rect o;
triangle p;
o.input();
o.calc();
p.input();
p.calc();
p*=o;
p.show();
cin.get();
cin.get();
}
triangle operator *=(triangle n,rect m)
{
n.s2*=m.s1;
return n;
}
Dec 10, 2012 at 12:29pm UTC
If you intend to modify a parameter, pass it by reference
http://cplusplus.com/doc/tutorial/functions2/
> Declare a operator *= function that it multiplies area of triangle and rectangle and place value in triangle object.
that's simply obfuscated.
You are killing the invariant too (the area depends on the base and height)
Dec 10, 2012 at 12:48pm UTC
This program does the following:
area triangle =s1
area rectangle=s2
s1=s1*s2
Dec 11, 2012 at 4:35pm UTC
In the following code Why Should We use &n (send by ref)
triangle operator *=(triangle& n,rect m)
If we send by value what happen in friend function?
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 62 63 64 65
#include <iostream>
#include <string>
using namespace std;
class triangle;
class rect
{
int a,b;
public :
int s1;
void input();
int calc();
friend triangle operator *=(triangle&,rect );
};
void rect :: input()
{
cin>>a>>b;
}
int rect:: calc()
{
return s1=a*b;
}
class rect;
class triangle
{
int c,h,s2;
public :
void input();
int calc();
friend triangle operator *=(triangle&,rect );
void show();
};
void triangle::input()
{
cin>>c>>h;
}
int triangle ::calc()
{
return s2=(c*h)/2;
}
void triangle::show()
{
cout<<s2;
}
int main()
{
rect o;
triangle p;
o.input();
o.calc();
p.input();
p.calc();
p*=o;
p.show();
cin.get();
cin.get();
}
triangle operator *=(triangle& n,rect m)
{
n.s2*=m.s1;
return n;
}
Dec 11, 2012 at 5:15pm UTC
I want answer this my question for tomorrow.plz help me.
tanx
Dec 11, 2012 at 5:28pm UTC
Topic archived. No new replies allowed.