Presentation is loading. Please wait.

Presentation is loading. Please wait.

Xiaoyan Li 2007 1 CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Similar presentations


Presentation on theme: "Xiaoyan Li 2007 1 CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College."— Presentation transcript:

1 Xiaoyan Li 2007 1 CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

2 Xiaoyan Li 2007 2 Review of Lecture 1 p Course Objectives and Schedule p WHAT (Topics) p WHY (Importance) p WHERE (Goals) p HOW (Information and Schedule) p The Phase of Software Development p Basic design strategy p Pre-conditions and post-conditions p Running time analysis

3 Xiaoyan Li 2007 3 The quiz from last class: p Printout all items in an integer array of size N p for (i = 0; i < N-1; i++) p for (i=0; i <= N; i++) p for (i=0; i > N; i++) p if (array[n-1]>0) print to screen array[i] print to screen array[i] else else exit program exit program

4 Xiaoyan Li 2007 4 Outline A Review of C++ Classes (Lecture 2) A Review of C++ Classes (Lecture 2) p OOP, ADTs and Classes p Class Definition, Implementation and Use p Constructors and Value Semantics More on Classes (Lecture 3) p Namespace and Documentation p Classes and Parameters p Operator Overloading

5 Xiaoyan Li 2007 5 p Chapter 2 introduces Object Oriented Programming. p OOP is the typical approach to programming which supports the creation of new data types and operations to manipulate those types. p This lecture gives a review of C++ Classes and introduces ADTs. Object Oriented Programming

6 Xiaoyan Li 2007 6 C++ Classes, Objects and ADTs p Class p Mechanism to create objects and member functions p Support information hiding p Abstract Date Types (ADTs) p mathematical data type (point, stack, queue, etc.) p A specification of a set of data and the set of operations that can be performed on the data. p In OOP, An ADT is a class, an instance of an ADT or class is an object

7 Xiaoyan Li 2007 7 A point ADT p A data type to store and manipulate a single point on a plane p Manipulations p Initialize p Retrieval p Shift x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0 - 2 yp1

8 Xiaoyan Li 2007 8 A point ADT p A data type to store and manipulate a single point on a plane p Manipulations p Initialize p Retrieval coordinates p Shift x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0 - 2 yp1 (-1, 0.8)

9 Xiaoyan Li 2007 9 A point ADT p A data type to store and manipulate a single point on a plane p Manipulations p Initialize p Retrieval coordinates p Shift x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0 - 2 y0.8 p1

10 Xiaoyan Li 2007 10 A point ADT p A data type to store and manipulate a single point on a plane p Manipulations p Initialize p Retrieval coordinates p Shift by x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0 - 2 y (0.3, -0.6) (1.3, -1.4) (1.3, -1.4) p2 p1

11 Xiaoyan Li 2007 11 Outline A Review of C++ Classes (Lecture 2) A Review of C++ Classes (Lecture 2) p OOP, ADTs and Classes p Class Definition, Implementation and Use p Constructors and Value Semantics More on Classes (Lecture 3) p Namespace and Documentation p Classes and Parameters p Operator Overloading

12 Xiaoyan Li 2007 12 point Definition  We can implement the point object using a data type called a class. class point {... }; Don’t forget the semicolon at the end x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

13 Xiaoyan Li 2007 13 point Definition p The class will have two components called x and y. These components are the x and y coordinates of this point. p Using a class permits two new features... class point {... double x; double y; }; x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

14 Xiaoyan Li 2007 14 point Definition ÊThe two components will be private member variables. This ensures that nobody can directly access this information. The only access is through functions that we provide for the class. class point {... private: double x; double y; }; x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

15 Xiaoyan Li 2007 15 point Definition ËIn a class, the functions which manipulate the class are also listed. class point { public:... private: double x; double y; }; Prototypes for the point functions go here, after the word public: x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

16 Xiaoyan Li 2007 16 point Definition ËIn a class, the functions which manipulate the class are also listed. class point { public:... private: double x; double y; }; Prototypes for the point member functions go here x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

17 Xiaoyan Li 2007 17 point Definition class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x() const; double get_y( ) const; private: double x; double y; }; Our point has at least four member functions: Function bodies will be elsewhere. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

18 Xiaoyan Li 2007 18 point Definition class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x( ) const; double get_y( ) const; private: double x; double y; }; The keyword const appears after two prototypes: This means that these functions will not change the data stored in a point ADT. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

19 Xiaoyan Li 2007 19 Files for the point ADT  The point class definition, which we have just seen, is placed with documentation in a file called point.h, outlined here. p The implementations of the four member functions will be placed in a separate file called point.cxx, which we will examine in a few minutes. (.cc,.cpp) Documentation: (Preconditions and Postconditions) Class definition: point class definition which we have already seen x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

20 Xiaoyan Li 2007 20 Outline A Review of C++ Classes (Lecture 2) A Review of C++ Classes (Lecture 2) p OOP, ADTs and Classes p Class Definition, Implementation and Use p Constructors and Value Semantics More on Classes (Lecture 3) p Namespace and Documentation p Classes and Parameters p Operator Overloading

21 Xiaoyan Li 2007 21 Using the point ADT  A program that wants to use the point ADT must include the point.h header file (along with its other header inclusions). p File pointmain1.cxx #include #include “point.h"... x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

22 Xiaoyan Li 2007 22 Using the point ADT  Just for illustration, the example program will declare two point variables named p1 and p2. #include #include “point.h" int main( ) { point p1; point p2; x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

23 Xiaoyan Li 2007 23 Using the point ADT  Just for illustration, the example program will declare two point objects named p1 and p2. #include #include “point.h" int main( ) { point p1; point p2; x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

24 Xiaoyan Li 2007 24 Using the point ADT p The program starts by calling the initialize member function for p1. #include #include “point.h" int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

25 Xiaoyan Li 2007 25 Using the point ADT p The program starts by activating the initialize member function for p1. #include #include “point.h" int main( ) { point p1: point p2; p1. initialize(-1.0, 0.8); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

26 Xiaoyan Li 2007 26 Using the point ADT ÊThe member function activation consists of four parts, starting with the object name. int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); Name of the object x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

27 Xiaoyan Li 2007 27 Using the point ADT ËThe instance (object) name is followed by a period. int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); A Period x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

28 Xiaoyan Li 2007 28 Using the point ADT ÌAfter the period is the name of the member function that you are activating. int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); Name of the Function x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

29 Xiaoyan Li 2007 29 Using the point ADT ÍFinally, the arguments for the member function. In this example the first argument (x coordinate) and the second argument (y coordinate) #include "thinker.h" int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); Arguments x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

30 Xiaoyan Li 2007 30 A Quiz How would you activate p1's get_x member function ? What would be the output of p1's get_x member function at this point in the program ? int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

31 Xiaoyan Li 2007 31 A Quiz Notice that the get_x member function has no arguments. At this point, activating p1.get_x will return a double value -1.0. int main( ) { point p1; point p2; p1. initialize(-1.0, 0.8); cout << p1. get_x( ) <<endl; x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

32 Xiaoyan Li 2007 32 A Quiz Trace through this program, and tell me the complete output. int main( ) { point p1; point p2; p1.initialize(-1.0, 0.8); cout << p1.get_x( ) << p1.get_y() << endl; p2.initialize(p1.get_x(), p1.get_y()); cout << p2.get_x( ) << p2.get_y() << endl; p2.shift(1.3, -1.4); cout << p2.get_x( ) << p2.get_y() << endl;... x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

33 Xiaoyan Li 2007 33 A Quiz -1.0 0.8 0.3 -0.6 0.3 -0.6 x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp int main( ) { point p1; point p2; p1.initialize(-1.0, 0.8); cout << p1.get_x( ) << p1.get_y() << endl; p2.initialize(p1.get_x(), p1.get_y()); cout << p2.get_x( ) << p2.get_y() << endl; p2.shift(1.3, -1.4); cout << p2.get_x( ) << p2.get_y() << endl;...

34 Xiaoyan Li 2007 34 What you know about Objects 4 Class = Data + Member Functions. 4 You know how to define a new class type, and place the definition in a header file. 4 You know how to use the header file in a program which declares instances of the class type. 4 You know how to activate member functions. 6 But you still need to learn how to write the bodies of a class’s member functions.

35 Xiaoyan Li 2007 35 Outline A Review of C++ Classes (Lecture 2) A Review of C++ Classes (Lecture 2) p OOP, ADTs and Classes p Class Definition, Implementation and Use p Constructors and Value Semantics More on Classes (Lecture 3) p Namespace and Documentation p Classes and Parameters p Operator Overloading

36 Xiaoyan Li 2007 36 point Implementation Remember that the member function’s bodies generally appear in a separate point.cxx file. Function bodies will be in.cxx file. Function bodies will be in.cxx file. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x( ) const; double get_y( ) const; private: double x; double y; };

37 Xiaoyan Li 2007 37 point Implementation We will look at the body of intialize, which must assign its two arguments to the two private member variables. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x( ) const; double get_y( ) const; private: double x; double y; };

38 Xiaoyan Li 2007 38 point Implementation void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; } For the most part, the function’s body is no different than any other function body. But there are two special features about a member function’s body... x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

39 Xiaoyan Li 2007 39 point Implementation ÊIn the heading, the function's name is preceded by the class name and :: - otherwise C++ won't realize this is a class’s member function. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; }

40 Xiaoyan Li 2007 40 point Implementation ËWithin the body of the function, the class’s member variables and other member functions may all be accessed. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; }

41 Xiaoyan Li 2007 41 void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; } point Implementation ËWithin the body of the function, the class’s member variables and other member functions may all be accessed. But, whose member variables are these? Are they p1.x p1.y p2.x p2.y ? x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

42 Xiaoyan Li 2007 42 void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; } point Implementation ËWithin the body of the function, the class’s member variables and other member functions may all be accessed. If we activate p1.initialize: p1.x p1.y x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

43 Xiaoyan Li 2007 43 void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; } point Implementation ËWithin the body of the function, the class’s member variables and other member functions may all be accessed. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp If we activate p2.initialize: p2.x p2.y

44 Xiaoyan Li 2007 44 point Implementation double point::get_x() { return x; } Here is the implementation of the get_x member function, which return the x coordinate: x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

45 Xiaoyan Li 2007 45 point Implementation Here is the implementation of the get_x member function, which return the x coordinate: Notice how this member function implementation uses the member variable x of the point object. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp double point::get_x() { return x; }

46 Xiaoyan Li 2007 46 point Implementation Member functions may activate other member functions Notice this member function implementation still directly assign the member variables x and y. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp void point::origin() { x = 0.0; y = 0.0; }

47 Xiaoyan Li 2007 47 point Implementation Member functions may activate other member functions Notice how this member function implementation uses the member function initialize. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp void point::origin() { initialize(0.0, 0.0); } Without object name

48 Xiaoyan Li 2007 48 class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x( ) const; double get_y( ) const; private: double x; double y; }; A Common Pattern p Often, one or more member functions will place data in the member variables... p...so that other member functions may use that data. Initialize & shift get_x & get_y

49 Xiaoyan Li 2007 49 p Classes have member variables and member functions. An object is a variable where the data type is a class. p You should know how to declare a new class type, how to implement its member functions, how to use the class type. p Frequently, the member functions of an class type place information in the member variables, or use information that's already in the member variables. p Next we will see more features of OOP and classes. Summary of classes

50 Xiaoyan Li 2007 50 Outline A Review of C++ Classes (Lecture 2) A Review of C++ Classes (Lecture 2) p OOP, ADTs and Classes p Class Definition, Implementation and Use p Constructors and Value Semantics More on Classes (Lecture 3) p Namespace and Documentation p Classes and Parameters p Operator Overloading

51 Xiaoyan Li 2007 51 Constructors: point Initialization p The program starts by activating the initialize member function for p1. #include #include “point.h" int main( ) { point p1: point p2; p1. initialize(-1.0, 0.8); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp First improvement: automatic initialization without activating the initialize function

52 Xiaoyan Li 2007 52 Constructors: point Initialization class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x() const; double get_y( ) const; private: double x; double y; }; We can provide a normal member function initialize x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

53 Xiaoyan Li 2007 53 Constructors: point Initialization class point { public: point(double init_x, double init_y); void shift(double dx, double dy); double get_x() const; double get_y( ) const; private: double x; double y; }; Or use a constructor that is automatically called x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp -function name same as class name - no return type, even no “void” !

54 Xiaoyan Li 2007 54 Constructors: Implementation void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; } For the most part, the constructor is no different than any other member functions. We only need to replace initialize with point x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

55 Xiaoyan Li 2007 55 Constructors: Implementation point::point(double init_x, double init_y) { x = init_x; y = init_y; } For the most part, the constructor is no different than any other member functions. But there are three special features about constructor... x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

56 Xiaoyan Li 2007 56 Constructors p Constructor is a member function which p the name must be the same as the class name p automatically called whenever a variable of the class is declared p arguments must be given after the variable name (when declared in user file) p A way to improve the initialize function p by providing an initialization function that is guaranteed to be called

57 Xiaoyan Li 2007 57 Constructors: point Initialization p Automatically called when declared. p Parameters after the object names #include #include “point.h" int main( ) { point p1: point p2; p1. initialize(-1.0, 0.8); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp First improvement: automatic initialization without explicitly activating an initialize function

58 Xiaoyan Li 2007 58 Constructors: point Initialization p Automatically called when declared. p Parameters after the object names #include #include “point.h" int main( ) { point p1(-1.0, 0.8): point p2(0.3, 0.6); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp First improvement: automatic initialization without explicitly activating an initialize function

59 Xiaoyan Li 2007 59 Default Constructors p Automatically called when declared. p Parameters after the object names #include #include “point.h" int main( ) { point p1(-1.0, 0.8): point p2(0.3, 0.6); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp Sometime we want to define an object with no parameters…

60 Xiaoyan Li 2007 60 Default Constructors p Automatically called when declared. p NO parameters after the object name p2 #include #include “point.h" int main( ) { point p1(-1.0, 0.8); point p2; x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp …not even a pair of parentheses

61 Xiaoyan Li 2007 61 Default Constructors class point { public: point(); point(double init_x, double init_y); … private: double x; double y; }; We could provide a second constructor with no parameters x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp Implementation point::point() { x = 0.0; y = 0.0; }

62 Xiaoyan Li 2007 62 Constructors: Function Overloading p You may declare as many constructors as you like – one for each different way of initializing an object p Each constructor must have a distinct parameter list so that the compiler can tell them part p Question: How many default constructor is allowed?

63 Xiaoyan Li 2007 63 Constructors: automatic default constructor p What happens if you write a class without any constructors? p The compiler automatically creates a simple default constructor p which only calls the default constructors for the member variables that are objects of some other classes p Programming Tip :Always provide your own constructors, and better with a default constructor

64 Xiaoyan Li 2007 64 Value Semantics of a Class p Value semantics determines how values are copied from one object to another p Consists of two operations in C++ p The assignment operator p The copy constructor p Document the value semantics p When you implement an ADT, the document should include a comment indicating that the value semantics is safe to use.

65 Xiaoyan Li 2007 65 Value Semantics: assignment operator p Automatic assignment operator p For a new class, C++ normally carries out assignment by simply copying each variable from the object on the right to that on the left p our new class point can use automatic assignment operator p When automatic assignment fails p we will see examples in Lecture 4 (pointers and dynamic arrays) point p1(-1.0, 0.8), p2; p2 = p1; cout << p2.get_x() <<“ “ << p2.get_y();

66 Xiaoyan Li 2007 66 Value Semantics: copy constructor p A copy constructor p is a constructor with exactly one parameter whose data type is the same as the constructor’s class p is to initialize a new object as an exact copy of an existing object p An example point p1(-1.0, 0.8); point p2 (p1); cout << p2.get_x() <<“ “ << p2.get_y();

67 Xiaoyan Li 2007 67 Value Semantics: copy constructor p A copy constructor p is a constructor with exactly one parameter whose data type is the same as the constructor’s class p is to initialize a new object as an exact copy of an existing object p An alternative syntax point p1(-1.0, 0.8); point p2 = p1; cout << p2.get_x() <<“ “<< p2.get_y();

68 Xiaoyan Li 2007 68 Value Semantics: discussion  point p2 = p1; versus p2 = p1;  The assignment p2 = p1; merely copies p1 to the already existing object p2 using the assignment operator.  The syntax point p2 = p1; looks like an assignment statement, but actually a declaration that both declare a new object, and calls the copy constructor to initialize p2 as a copy of p1. p p2 will be the same iff the assignment operator and the copy constructor do the same things

69 Xiaoyan Li 2007 69 Copy Constructor: Implementation p You may write a copy constructor much like any other constructor p Lecture 4 and later p Take advantage of a C++ feature p automatic copy constructor p similar to assignment, the automatic copy constructor initializes a new object by merely copy all the member variables from the existing object. p Automatic versions may fail! Point Demo Point Demo

70 Xiaoyan Li 2007 70 Constructors, etc.– a summary p Constructor is a member function p define your own constructors (including a default) p automatic default constructor p inline member functions ( Ch 2.2) p Place a function definition inside the class definition p for time efficiency p value semantics of a class p assignment operators and copy constructor p automatic assignment op and copy constructor

71 Xiaoyan Li 2007 71 Outline A Review of C++ Classes (Lecture 2) A Review of C++ Classes (Lecture 2) p OOP, ADTs and Classes p Class Definition, Implementation and Use p Constructors and Value Semantics More on Classes (Lecture 3) p Namespace and Documentation p Classes and Parameters p Operator Overloading

72 Xiaoyan Li 2007 72 Assignments p Reading: p Chapter 2.3-2.5 p Programming assignment 1 - Due Sep. 19 p Need all of chapter 2 to finish, but you can start doing it now p Requirements and guidelines will be posted on the course web site tonight. p Read it before next lecture.

73 Xiaoyan Li 2007 73 T HE E ND Presentation copyright 1997, Addison Wesley Longman For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact. The first part (p.3-47) of this lecture was adapted from:


Download ppt "Xiaoyan Li 2007 1 CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College."

Similar presentations


Ads by Google