








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lecture notes from cpsc 223, a course on algorithms and data abstract structures, covering the use of const keywords, operator overloading, and file organization in c++. It includes examples of header file organization using #ifndef, #define, and #endif, and the use of const keywords in methods to indicate that they do not modify the object. It also covers the concept of operator overloading and its implementation using operator functions.
Typology: Slides
1 / 14
This page cannot be seen from the preview
Don't miss anything!









CPSC 223, Fall 2010 3
#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!
CPSC 223, Fall 2010 7
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
const Stock s1("GOOG", 1000); s1.setShares(900); // sell shares
const Stock s1("GOOG", 1000); cout << s1.getShares() << endl; // check shares
Stock s = s1.worthMore(s2); //‘=’ still copying
Stock& s = s1.worthMore(s2); s.setShares(s.getShares() – 100); // modify s
const Stock& worthMore(const Stock& s) const;
Stock s = s1.worthMore(s2);
Stock& s = s1.worthMore(s2);
const Stock& s = s1.worthMore(s2); CPSC 223, Fall 2010 15
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
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)!
CPSC 223, Fall 2010 21
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
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!
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