Class that cannot be inherited ... C++
I suppose this was supposed to be the subject line, not the
"From" name... if I read the message right, your name is Rahul Agarwal.
Rahul said:
hi
(with TCLITE)
When u will wanna some friend funtion
u have to declear that function as friend in the class declearation now
ur defination may be at the same point or "YOU WILL DEFINE THAT
FUNCTION IN .CPP FILE WITHOUT RESOLUTION OPERATOR" if u will not so (or
try with resolution) it will give an error at compile time "funtion is
not a part of class.
so its a completely different way to map its calling
and just changing the declearation will never go to work
i have read about virtual memory and vptr theory
if u get some theory for this friend function calling
please let me
thank u
Rahul Agarwal
Ouch. I really really don't want to violate moderation guidelines,
but after six times trying to read that, my eyes hurt. At least some
of the misspellings (wanna,u) seem to be on purpose... please don't
do that!
Obviously you're asking some question about the program you attached.
I'll try to critique it.
This is the program name, yes?
This old-style header will work on some compilers...
better to use
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area (void) {return (width * height);}
friend CRectangle duplicate (CRectangle);
};
I would have used a constructor instead of functin "set_values"
but we'll overlook that for now.
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
Fine implementation of set_values.
CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
If your class had a constructor, you could have written this a LOT
more simply... even without it, I don't quite understand why you
didn't just use set_values.
I would also recommend changing the parameter to a const reference,
if you're advanced enough to have read about references. (If not,
don't worry about it yet.)
The name "duplicate" makes me think that you wanted to make an
exact copy of the CRectangle. (This would be better as a copy
constructor, but that's a topic for another day.) I wonder why
you multiply the width and height by 2?
int main () {
CRectangle rect, rectb;
rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
}
rect is 2 by 3. When you use duplicate, you double both the width
and height by 2... the result is 4 by 6, so the total area is 24.
Still not sure what you were askikng... did I answer it?