Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How i use overload for a clas atribute ?

Status
Not open for further replies.

UnityKing

Programmer
Dec 12, 2004
17
CA
May someone help me with overloading, i just started C++ and im programming a rectangle class :

Code:
class clsRectangle { 

public:
	int m_Height;
	int m_Width;
	clsRectangle operator= (clsRectangle);                                                   
	int area (void) {return (m_Height*m_Width);}                          
	int Perrimeter (void) {return ( 2*m_Height + 2*m_Width);}
};


clsRectangle::operator= (clsRectangle) {

}

how can i do something like this :
Rectangle.width = 10

(like u can do in Vb 6.0)
 
I've never used VB, but it sounds like you want to make a property. They don't exist in C++. If you want to access the width, just use m_Width like this:

clsRectangle rect;
rect.m_Width = 10;

Operator overloading is for applying an operator between two data types where the operator would not be defined by default. You probably don't need an = operator here, because there is a default one created that just copies all of the member variables, which will work fine here. A += operator might be useful to add one rectangle's width and height to the current rectangle:

Code:
clsRectangle clsRectangle::operator +=(clsRectangle rect)
{
  m_Width += rect.m_Width;
  m_Height += rect.m_Height;
  return *this;
}

Hope this answers your question.
 
To properly encapsulate your class you should create a Set and Get function for each member variable, and make the member variables private.

Example:
Code:
void SetWidth( int  iWidth )
{
    m_Width = iWidth;
}

int GetWidth( void )
{
    return m_Width;
}
 
I know hot to make a fonction,

but i dont want to put something like : Rect.Width (10)

i want something like that: Rect.Width = 10

i think timay said its cant be done in c++ but im not really sure.
 
OMFG i found it, so mutch easy this way :

Code:
class clsRectangle {

public:
    int Height;
    int Width;
    int Aire (void) {return (m_Height*m_Width);}
    int Perrimeter (void) {return ( 2*m_Height + 2*m_Width);}
};


int main(){
	clsRectangle Rect;

	[u]Rect.m_Height = 100;[/u]
	[u]Rect.m_Width = 10;[/u]


	return (0);
}

thanks for intellisence
 
And thank timmay3141 who mentioned that already above. ;)

Your solution is generally considered to be poor design. Is there a reason you want to use the "Rect.Width = 10;" instead of "Rect.Width(10);"?

As cpjust indicated, encapsulating member variables is usually preferred:
Code:
class clsRectangle {
[red]private:[/red]
    int m_Height;
    int m_Width;
[red]public:
    int Width() { return m_Width; }
    void Width(int w) { m_Width = w; }
    int Height() { return m_Height; }
    void Height(int h) { m_Height = h; }
[/red]
    int Aire (void) {return (m_Height*m_Width);}
    int Perrimeter (void) {return ( 2*m_Height + 2*m_Width);}
};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top