Computer Science E50b
Tuesday Section . 7pm . Science Center 111
TF: Alix Marchandise-Franquet
February 16, 1999
Operator overloading
Chapter 8 (Deitel&Deitel)
- C++ lets you create new types but not new operators. However, you
can overload existing operators, i.e. define their behavior of used
with the newly defined types. You must overload operators to use them
on class objects. Only the assignment (=) and address (&) operators can be used with every
class without explicit overloading.
- Some operators are already overloaded in C++. For example: +, - which
work with int, floats etc...
- Use operator overloading when it makes a program clearer, don't
overload operators in a way that makes the program difficult to
understand. For example, it is possible to overload + to substract and
overload / to multiply, but it is extremely bad style. (Note: you
can't change the way C++ operators work with built-in types, for
example the addition of two integers)
- What operators can you overload? Look at figure 8.1 in D&D
for a list. If you try to overload an operator that can't be
overloaded or to create a new operator via overloading, you will get a
syntax error.
- Should the overloaded operator be a member function? a friend?
- member function: if it needs to access the private data
members of the class and the leftmost operand is a class object
Ex: newtype operator+ (newtype, newtype)
- friend function: can't satisfy the condition for a member
function, but still needs to access the private data members
Ex: ostream& operator<< (ostream&, classname&)
istream& operator>> (istream&, classname&)
- none of the above if it does not need to access the private data
members directly (it still can through accessor functions)
Building a class for a student
Suppose that you were to build a class for a school to store
information regarding students. You want to keep track of certain information
regarding each student who is enrolled at the school, and you want to
provide functions that will enable the school to store/retrieve information
regarding a given student.
Data to keep track of:
- name (string)
- date of birth (as a string day/month/year)
- id number (an int)
- gpa (float)
- major (enum)
Functions to build:
Of course, there should be constructors (at least one), amd accessor
methods (functions to get the value of the private data members or to
set
those values). There is a function that computes and returns the
age
of the person, and one that overloads the << operator to print all the
information
regarding the student.
Of course, there are many many many other things you could do... Feel
free to add them!
The following files contain the code:
student.h : contains the class
student.cc : contains the definitions for the public member functions
and friend functions of the class
studentmain.cc : a sample main
To compile this program, just type:
g++ student.cc studentmain.cc -o student
Building a vector class
Build a class to hold 2-dimensional vectors.
What do you need?
- private data members to hold the x and y coordinates of the
vector
- constructors, a destructor
- accessor functions (getcoordinates, setcoordinates)
- overload operators (+, -, <<...)
vector.h
vector.cc
vectormain.cc
To compile this, type
g++ vector.cc vectormain.cc -o vector