CPSC 250 Data Structures Introduction & Review Dr. Yingwu Zhu
What to learn? ADT design & implementation using C++ class Algorithm efficiency analysis Big-O ADTs: Binary search trees, AVL trees, Heaps, Priority queues Sorting algorithms Basic sorting algorithms Quicksort, Heapsort, Mergesort
Communication Channels Course website http://fac-staff.seattleu.edu/zhuy Syllabus, tentative schedule, HW, lecture slides, supplemental materials, grading policy, SU emails Check regularly (daily)
Programming Assignments Linux server: cs1.seattleu.edu Use SSHClient or putty to access the server SU email account and password Problem? Contact Renny Philipose (philipr@seattleu.edu) Work from home, NO VPN needed! Submission Electronic submission (announced later!) Ensure your program is compilable!
Study Strategies Class participation Lecture reviews Programming practice Questions/Thinking: What? Why? How?
Other Info See Syllabus for details
C++ classes vs. structs Similarity? Difference?
Structs and Classes: Similarities Essentially the same syntax Both are used to model objects with multiple attributes (characteristics) represented as data members also called fields Thus, both are used to process nonhomogeneous data sets. 9
Differences Structs vs. Classes Members public by default Class members are private by default Can be specified private Can be specified public 10
Advantages in C++ Classes C++ classes model objects which have: Attributes represented as data members Operations represented as functions (or methods) Leads to object oriented programming Objects are self contained "I can do it myself" mentality 11
Class Declaration Syntax class ClassName { public: Declarations of public members private: Declarations of private members }; 1. Data members normally placed in private: section of a class (information hiding) 2. Function members usually in public: section (exported for external use) 12
Class Libraries Class declarations placed in header file Given.h extension Contains data items and prototypes Implementation file Same prefix name as header file Given.cpp extension Programs which use this class library called client/driver programs 13
Translating a Library 14
Example of User-Defined Time Class Now we create a Time class (page 150) Actions done to Time object, done by the object itself Note interface for Time class object, Fig. 4.2 Data members private inaccessible to users of the class Information hiding class Time { private: unsigned myhours, myminutes; char myamorpm; public: }; 15
Constructor Understanding Constructors, p158-159 Initialize data members Optional: allocate memory Note constructor definition in Time.cpp example (p161) ClassName::ClassName (parameter_list) : member_initializer_list { // body of constructor definition }
Constructors Time::Time() { myhours = 0; myminutes = 0; myamorpm = A ; } Data member initialization Time::Time(unsigned h, unsigned m, char c) : myhours(h), myminutes(m), myamorpm(c) { } 17
Overloading Functions Same function name, different signatures! Note existence of multiple functions with the same name Time(); Time(unsigned inithours, unsigned initminutes, char initampm); Known as overloading Compiler compares numbers and types of arguments of overloaded functions Checks the "signature" of the functions 18
Default Arguments Possible to specify default values for constructor arguments Time(unsigned inithours = 12, unsigned initminutes = 0, char initampm = 'A'); Consider Time t1, t2(5), t3(5,30), t4(5,30,'p'); 19
Copy Operations(p166) During initialization Time t = bedtime During Assignment t = midnight; 20
Overloading Operators Same symbol can be used more than one way Operator, function: operator () Two cases If is a function member: a b a.operator (b) Otherwise, a b operator (a,b) Operators: +,-,*,/ Operators: <<, >> 21
Redundant Declarations Note use of #include "Time.h" in Time.cpp Client program Causes "redeclaration" errors at compile time Solution is to use conditional compilation Use #ifndef and #define and #endif compiler directives 22
Pointers to Class Objects Possible to declare pointers to class objects Time * timeptr = &t; Access with timeptr->getmiltime() or (*timeptr).getmiltime() 23
The this Pointer Every class has a keyword, this a pointer whose value is the address of the object Value of *this would be the object itself 24
Exercise Use what we have reviewed to Implement a polynomial class in the form of ax+b, coefficients a and b are integers. The class implementation should have: Constructor Overload operator+ Overload operator<< 25
Example for class ax+b //poly.h: header file #include <iostream> using namespace std; #ifndef _POLY_H // avoid redundant declarations #define _POLY_H class Poly { private: int m_ca; //coefficient a int m_cb; public: Poly(int a, int b) : m_ca(a), m_cb(b) { }; //constructor void display(ostream& out) const; //for operator << overloading Poly operator+(const Poly& po); //overloading + }; //do not forget to put ;!!!! ostream& operator<<(ostream& out, const Poly& po); //overloading << #endif 27
Example: ax+b //poly.cpp: implementation file #include poly.h using namespace std; void Poly::display(ostream& out) const { out << m_ca << x + << m_cb << endl; } Poly Poly::operator+(const Poly& po) { Poly c; c.m_ca = this->m_ca + po.m_ca; //what is this? Do we need it here? c.m_cb = m_cb + po.m_cb; return c; } ostream& operator<<(ostream& out, const Poly& po) { po.display(out); return out; } 28