lecture2b - Lecture2:AbstractDataTypesPartii. ECE2574

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 2 : Abstract Data Types Part ii. ECE 2574 Chao Huang Virginia Tech Spring 2010
Background image of page 1

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

View Full DocumentRight Arrow Icon
 C. Huang, Virginia Tech 2 Overloaded Operators Fundamental operators Non-fundamental operators int a,b,c; a = b + c; ... . .. class listClass { public: ... . .. private: listItemType Items[MAX_LIST]; int Size; }; ... . .. cout << list1 + list2 << endl; ... . ..
Background image of page 2
 C. Huang, Virginia Tech 3 Overloaded Operators:  “+” Suppose we want to concatenate two lists in a program A very natural syntax would be: listClass list1, list2, list3; list3 = list1 + list2; An overloaded operator, e.g.  +  in this example, has more than  one meaning » Built-in: add two numbers » Overloaded: concatenate two lists » Compiler uses the data type to de-code the meaning In general, we can overload common operators for classes to  enable a particular operator to work on instances of a particular  class » Specify what happens when the operator is applied to  instances of a particular class list3 = list1.operator+( list2 );
Background image of page 3

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

View Full DocumentRight Arrow Icon
 C. Huang, Virginia Tech 4 ListA_overload.h class listClass { public: listClass(); // default constructor bool ListIsEmpty() const; int ListLength () const; void ListInsert (int Newposition, listItemType NewItem, bool& Success); void ListDelete (int Position, bool& Success); void ListRetrieve(int Position, listItemType& DataItem,bool& Success) const; int Index(int Position) const; listClass operator+(const listClass&); private: listItemType Items[MAX_LIST]; //list items int Size; //number of items };
Background image of page 4
 C. Huang, Virginia Tech 5 ListA_overload.cpp // overloaded + (concatenate two lists) { listClass temp; cout << "DEBUG: executing operator + " << endl; temp.Size = Size + a.Size; int i; for (i=0; i<Size; i++) { temp.Items[i] = Items[i]; } for (i=0; i<a.Size; i++) { temp.Items[Size+i] = a.Items[i]; } return temp; } // end operator+ mylist3=mylist1+mylist2;
Background image of page 5

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

View Full DocumentRight Arrow Icon
 C. Huang, Virginia Tech 6 Assignment Operators A class assignment operator “=” assigns values to a target  class instance mylist3 = mylist1 + mylist2; The compiler synthesizes an assignment operator automatically  if the class does not define its own. » Each member of the right-hand object is assigned to the 
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/28/2010 for the course ECE 2574 taught by Professor Chuang during the Spring '08 term at Virginia Tech.

Page1 / 32

lecture2b - Lecture2:AbstractDataTypesPartii. ECE2574

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online