Download Const Keywords, Operator Overloading, and File Organization in C++ and more Slides Data Structures and Algorithms in PDF only on Docsity!
CPSC 223
Algorithms & Data Abstract Structures
Lecture 3:
Organizing your files, const,
and operator overloading
Today …
• Quiz 2
• Homework 1 is due on Thursday
• Topics
- Using const
- Operator overloading CPSC 223, Fall 2010 2
Organizing your files …
CPSC 223, Fall 2010 3
Organizing Header Files
Within a header file myclass.h …
#ifndef MYCLASS_H_ #define MYCLASS_H_ ... class MyClass { ... }; #endif CPSC 223, Fall 2010 4 These commands ensure your header files are only read once! Also often used to turn on/off debug statements!
Using the const keyword …
CPSC 223, Fall 2010 7
The various uses of const
A simple class for managing stock shares …
class Stock { public: Stock(); Stock(string aCompany, int theShares); int getShares(); string getCompany(); void setShares(int newShares); Stock worthMore(Stock s); private: string company; int shares; }; CPSC 223, Fall 2010 8
The various uses of const
Lets say we want to do the following
const Stock s1("GOOG", 1000); s1.setShares(900); // sell shares
- What will happen when we compile? Compile Error! "error: passing 'const Stock' as 'this' argument of 'void Stock::setShares(int)' discards qualifiers"
- Why is the compiler complaining? … Because s1 is a constant, and we are modifying it! CPSC 223, Fall 2010 9 The compiler is our friend!! It is saving us from introducing bugs later!
The various uses of const
Lets say now we want to do the following
const Stock s1("GOOG", 1000); cout << s1.getShares() << endl; // check shares
- What will happen when we compile? Compile Error! "error: passing 'const Stock' as 'this' argument of 'void Stock::getShares()' discards qualifiers"
- Why is the compiler complaining now? … The compiler can’t tell we aren’t modifying s CPSC 223, Fall 2010 10 The compiler is our friend!! But we have to help it…!
The various uses of const
Now we return the Stock by reference
Stock s = s1.worthMore(s2); //‘=’ still copying
- What will happen when we compile? Compile Error! "error: invalid initialization of reference of type 'Stock&' from expression of type 'const Stock'"
- Why is the compiler complaining now? CPSC 223, Fall 2010 13
The various uses of const
The compiler is complaining because we could do this
Stock& s = s1.worthMore(s2); s.setShares(s.getShares() – 100); // modify s
- But worthMore (i.e., s1) and its argument (s2) are const!
- Luckily the compiler caught our mistake!
- Are we stuck?
- Nope … we can add more const keywords CPSC 223, Fall 2010 14
The various uses of const
We can define worthMore to return a const reference
const Stock& worthMore(const Stock& s) const;
The compiler is happy again:
Stock s = s1.worthMore(s2);
But what about now:
Stock& s = s1.worthMore(s2);
What is the difference? How can we fix it?
const Stock& s = s1.worthMore(s2); CPSC 223, Fall 2010 15
The various uses of const
When should we not return by reference?
- Never return a temporary variable ( compiler error ) const string& badIdea1(string s1, string s2) { string tmp = s1 + s2; return tmp; // same if: return s1+s }
- Returning object state … const string& badIdea2() const { return name; // data member } CPSC 223, Fall 2010 16 What happens to tmp after the function exits?! What happens to name if the object is deallocated?!
Exercise #1: Using const
Add const keywords and references to these functions:
class Department { public: Department(); Department(string name, Company company); void setDeptName(string name); string getDeptName(); Company getCompany(); void addEmployee(Employee emp); Employee getEmployee(string n); void removeEmployee(string n); private: ... }; CPSC 223, Fall 2010 19
Exercise #1: Using const
Add const keywords and references to these functions: class Department { public: Department(); Department(const string& name, const Company& cmp); void setDeptName(const string& name); string getDeptName() const; Company getCompany() const; void addEmployee(const Employee& emp); Employee getEmployee(const string& n) const; void removeEmployee(const string& n); private: ... }; CPSC 223, Fall 2010 20 Really depends on the behavior of these functions (but here we can guess since getters/setters)!
Operator overloading (first pass) …
CPSC 223, Fall 2010 21
Basic operator overloading
- Who remembers what function overloading is?
- Same function name, different argument types
- C++ supports overloading of functions & operators!
- We can define, e.g., +, - , ==, <=, … for our objects CPSC 223, Fall 2010 22 int x, y; ... if(x < y) cout << “x < y\n”; else cout << “x >= y\n”; ... Stock s1, s2; ... if(s1 < s2) cout << “s1 < s2\n”; else cout << “s1 >= s2\n”; ...
Operator Overloading for Classes
- Defines subtraction operator (-) on two objects of
the same class, returning an object of that class
- Note the use of const …
- Is this a unary or binary operator? CPSC 223, 2009 25 return type func+on name ClassName operator‐(const ClassName& arg1, const ClassName& arg2); formal arguments
Stock example again …
Lets add a “<” operator to our Stock class …
class Stock { public: int getShares() const; double getShareValue() const; ... }; // in Stock.cpp: bool operator<(const Stock& s1, const Stock& s2) { ... what should go here??? } CPSC 223, Fall 2010 26
Stock example again …
Lets add a “<” operator to our Stock class … class Stock { public: int getShares() const; double getShareValue() const; ... }; // in Stock.cpp: bool operator<(const Stock& s1, const Stock& s2) { double v1 = s1.getShares() * s1.getShareValue(); double v2 = s2.getShares() * s2.getShareValues(); return v1 < v2; } CPSC 223, Fall 2010 27 Note similar to “worthMore” function!
Operator Overloading for Classes
We can also overload operators as member functions
class ClassName { public: ClassName operator-(const ClassName& arg2); ... }; CPSC 223, 2009 28 return type func+on name ClassName operator‐(const ClassName& arg1, const ClassName& arg2); formal arguments