Presentation is loading. Please wait.

Presentation is loading. Please wait.

Zhigang Zhu, 2002-2015 1 CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.

Similar presentations


Presentation on theme: "Zhigang Zhu, 2002-2015 1 CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City."— Presentation transcript:

1 Zhigang Zhu, 2002-2015 1 CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City College of New York

2 Zhigang Zhu, 2002-2015 2 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

3 Zhigang Zhu, 2002-2015 3 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

4 Zhigang Zhu, 2002-2015 4 C++ Classes 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 p Class as an ADT that programmers can use without knowing how the member functions are implemented - i.e. with information hiding

5 Zhigang Zhu, 2002-2015 5 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

6 Zhigang Zhu, 2002-2015 6 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)

7 Zhigang Zhu, 2002-2015 7 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

8 Zhigang Zhu, 2002-2015 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 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

9 Zhigang Zhu, 2002-2015 9 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

10 Zhigang Zhu, 2002-2015 10 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

11 Zhigang Zhu, 2002-2015 11 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

12 Zhigang Zhu, 2002-2015 12 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

13 Zhigang Zhu, 2002-2015 13 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

14 Zhigang Zhu, 2002-2015 14 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

15 Zhigang Zhu, 2002-2015 15 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

16 Zhigang Zhu, 2002-2015 16 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

17 Zhigang Zhu, 2002-2015 17 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. 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

18 Zhigang Zhu, 2002-2015 18 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

19 Zhigang Zhu, 2002-2015 19 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

20 Zhigang Zhu, 2002-2015 20 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

21 Zhigang Zhu, 2002-2015 21 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

22 Zhigang Zhu, 2002-2015 22 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

23 Zhigang Zhu, 2002-2015 23 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

24 Zhigang Zhu, 2002-2015 24 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

25 Zhigang Zhu, 2002-2015 25 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

26 Zhigang Zhu, 2002-2015 26 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

27 Zhigang Zhu, 2002-2015 27 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) 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

28 Zhigang Zhu, 2002-2015 28 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

29 Zhigang Zhu, 2002-2015 29 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

30 Zhigang Zhu, 2002-2015 30 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

31 Zhigang Zhu, 2002-2015 31 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;...

32 Zhigang Zhu, 2002-2015 32 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.

33 Zhigang Zhu, 2002-2015 33 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

34 Zhigang Zhu, 2002-2015 34 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; };

35 Zhigang Zhu, 2002-2015 35 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; };

36 Zhigang Zhu, 2002-2015 36 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

37 Zhigang Zhu, 2002-2015 37 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; }

38 Zhigang Zhu, 2002-2015 38 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; }

39 Zhigang Zhu, 2002-2015 39 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

40 Zhigang Zhu, 2002-2015 40 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

41 Zhigang Zhu, 2002-2015 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. x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp If we activate p2.initialize: p2.x p2.y

42 Zhigang Zhu, 2002-2015 42 point Implementation double point::get_x() const { 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

43 Zhigang Zhu, 2002-2015 43 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() const { return x; }

44 Zhigang Zhu, 2002-2015 44 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; }

45 Zhigang Zhu, 2002-2015 45 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

46 Zhigang Zhu, 2002-2015 46 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

47 Zhigang Zhu, 2002-2015 47 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

48 Zhigang Zhu, 2002-2015 48 Assignments p Reading: p Chapter 2.3-2.5 p Programming assignment 1 - Due Wed, Sep. 16 p Need all of chapter 2 to finish, but you can start doing it now p Requirements and guidelines have been posted on the course web site p C++ Installation Guide online p Linux Users: See the assignment #1 guidelines p Mac/Win Users: Check the course bulletin (by Wai Khoo)

49 Zhigang Zhu, 2002-2015 49 Break

50 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 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 Zhigang Zhu, 2002-2015 72 Assignments p Reading: p Chapter 2.3-2.5 p Programming assignment 1 - Due Sep. 16 p Need all of chapter 2 to finish, but you can start doing it now p Requirements and guidelines have been posted on the course web site

73 Zhigang Zhu, 2002-2015 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 "Zhigang Zhu, 2002-2015 1 CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City."

Similar presentations


Ads by Google