lecture5

# lecture5 - Monday,April17 AssignmentOperators Stacks Queues...

This preview shows pages 1–8. Sign up to view the full content.

Monday, April 17 th Assignment Operators Stacks  Queues

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The Assignment Operator We just learned how to  initialize the value of  a  new class variable  to the value  of an  existing variable .   main() { Circ foo(1,2,3); Circ bar(4,5,6); bar = foo ; } Now lets learn how to set the value of an  existing variable  to the value of an  another existing variable . In this example, both  foo  and  bar  have  been constructed. Both have their member variables  initialized. Then we set  bar  equal to  foo .
The Assignment Operator main() { Circ foo(1,2,3); Circ bar(4,5,6); bar = foo ; } In this case, the  copy constructor  is  NOT  used to copy values from  foo  to  bar . Instead, a special member function called an  assignment operator  is used. If you don’t define your own  assignment  operator Then C++ provides a default version that  just copies each of the members. foo    m_x m_y   m_rad 1 2 3 bar    m_x m_y   m_rad 4 5 6 1 2 3 Lets see how to define our own  assignment operator .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
class Circ { public: Circ(float x, float y, float r) { m_x = x; m_y = y; m_rad = r; } float GetArea(void) { return(3.14159*m_rad*m_rad); } private: float m_x, m_y, m_rad; }; void Assign (const Circ &src) { m_x = src.m_x; m_y = src.m_y; m_rad = src.m_rad; } The Assignment Operator The syntax for an assignment operator is a bit confusing. So lets define a simpler version  first… Here’s how we’d use our new  function. main() { Circ foo(1,2,3); Circ bar(4,5,6); bar.Assign(foo) ; } foo    m_x m_y   m_rad 1 2 3 bar    m_x m_y   m_rad 4 5 6 src  1 2 3 // same as  bar = foo;
class Circ { public: Circ(float x, float y, float r) { m_x = x; m_y = y; m_rad = r; } float GetArea(void) { return(3.14159*m_rad*m_rad); } private: float m_x, m_y, m_rad; }; The Assignment Operator void Assign (const Circ &src) { m_x = src.m_x; m_y = src.m_y; m_rad = src.m_rad; } operator= 1. The function name is  operator= Now lets see what a real  assignment operator  looks like. 2.   The function return type is a  reference to the class . Circ & 3.   The function returns  *this   when its done. return(*this);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
class Circ { public: Circ(float x, float y, float r) { m_x = x; m_y = y; m_rad = r; } float GetArea(void) { return(3.14159*m_rad*m_rad); } private: float m_x, m_y, m_rad; }; The Assignment Operator foo    m_x m_y   m_rad 1 2 3 main() { Circ foo(1,2,3); Circ bar(4,5,6); bar = foo ; } bar    m_x m_y   m_rad 4 5 6 void Assign (const Circ &src) { m_x = src.m_x; m_y = src.m_y; m_rad = src.m_rad; } operator= Circ & return(*this); src  1 2 3 // think:  bar.Assign(foo);
class Squares { public: Squares(int n) { m_n = n; m_sq = new int[n]; for (int j=0;j<n;j++) m_sq[j] = (j+1)*(j+1); } ~ Squares(){ delete []m_sq; } void printSquares() { for (int j=0;j<n;j++) cout << m_sq[j] << endl; } private: int *m_sq, m_n; }; The Assignment Operator Remember our updated  Squares  class… Ok – so when would we ever need to  write our own  Assignment Operator ?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 42

lecture5 - Monday,April17 AssignmentOperators Stacks Queues...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online