Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Data Type and C++ Classes 1. Object-oriented Programming 2. Abstract Data Types 3. Classes and Objects 4. Examples 5. Member Functions and Member.

Similar presentations


Presentation on theme: "Abstract Data Type and C++ Classes 1. Object-oriented Programming 2. Abstract Data Types 3. Classes and Objects 4. Examples 5. Member Functions and Member."— Presentation transcript:

1 Abstract Data Type and C++ Classes 1. Object-oriented Programming 2. Abstract Data Types 3. Classes and Objects 4. Examples 5. Member Functions and Member Variables 6. Constructors 7. Operator Overloading

2 Object-Oriented Programming (OOP)  Procedure-oriented Programming o Traditional method uses command statements o Imperative method  Object-oriented Programming o Views program as set of objects interacting with each other o Class—template for creating objects o Class encapsulates data and behavior

3 Abstract Data Type (ADT)  ADT—A way of defining a complex data type by specifying operations (without specifying details)  Example 1—ADT List o Create a list o Insert an item into the list o Remove an item from the list o Check if the list is empty o Display list contents

4 Abstract Data Type (ADT)  Example 2—ADT Fraction o Create a fraction o Add another fraction and return the sum o Subtract another fraction and return the difference o Return the numerator of the fraction o Display the fraction

5 Classes and Objects  Class o Implements ADT’s in a programming language (C++) o Specifies actions (behavior) o Encapsulates the data which describes an object (attributes) o Is a template for creating (instantiating) objects of the class

6 Example--Circle  Circle o What operations (behavior) are appropriate for a circle? o Create a circle with a particular radius o Calculate its circumference o Calculate its area

7 Class Circle (class interface) class Circle { public: Circle(double r); double getRadius(); double circumference(); double area(); private: double radius; }; Save as circle.h

8 Class Circle (class implementation) #include "circle.h" Circle::Circle(double r){ radius = r; } double Circle::getRadius(){ return radius; } double Circle::circumference(){ return 2 * 3.14159 * radius; } double Circle::area(){ return 2 * 3.14159 * radius; } Save as circle.cpp

9 Class Circle (used in client program) #include #include "circle2.h" using namespace std; int main(){ double r1 = 1.0; double r2 = 3.0; Circle c1(r1); Circle2 c2(r2); cout << "Circle with radius " << r1 << " has area of " << c1.area() << endl; cout << "Circle with radius " << r2 << " has volume of " << c2.volume() << endl; return 0; } Save as circleTest.cpp

10 Your Turn 1. In the main(), instantiate a third circle whose radius is the same as that of c2. 2. Print the area and the volume of the third circle.

11 Example--Fraction  Fraction o What operations (behavior) are appropriate for a fraction? o Create a fraction o Add another fraction and return the sum o Subtract another fraction and reutrn the difference o Display the fraction

12 Class Fraction (class interface) class Fraction{ public: Fraction(int n, int d); Fraction add(Fraction f); Fraction subtract(Fraction f); void display(); private: int numer; int denom; }; Save as fraction.h

13 Class Fraction (class implementation) #include #include "fraction" using namespace std; Fraction:: Fraction(int n, int d){ numer = h; denom = d; } Fraction Fraction ::add(Fraction f){... }... Void Fraction::display(){... } Save as fraction.cpp

14 Class Fraction(used in client program) #include #include "fraction.h" using namespace std; int main(){ Fraction f1(3, 4); Fraction f2(1, 2); f1.display(); cout << endl; f2.display(); cout << endl; Fraction sum = f1.add(f2); Fraction diff = f1.subtract(f2); cout << "Sum "; sum.display(); cout << endl; cout << "Diff: "; diff.display(); cout << endl; return 0; } Save as fractionTest.cpp

15 Class List (used in client program) #include #include "fraction.h" using namespace std; int main(){ Fraction f1(3, 4); Fraction f2(1, 2); f1.display(); cout << endl; f2.display(); cout << endl; Fraction sum = f1.add(f2); Fraction diff = f1.subtract(f2); cout << "Sum "; sum.display(); cout << endl; cout << "Diff: "; diff.display(); cout << endl; return 0; } Save as listTest.cpp

16 Example--List  List o What operations (behavior) are appropriate for a list? o Create a list o Insert an item at the list end o Check if the list is empty o Display list contents

17 Class List(class interface) #define MAX_SIZE 100; class List{ public: List(); void append(int item); void removeAt(int pos); bool isEmpty(); void print(); private: int data[MAX_SIZE]; int count; }; Save as list.h

18 Class List (class implementation) #include #include "list.h" using namespace std; List::List(){ count = 0; } void List::append(int item){... }... void List::print(){... } Save as list.cpp

19 Class List (used in client program) #include #include "list.h" using namespace std; int main(){ int mylist[] = {2, 4, 6, 8, 10}; int listCount = 5; List list; for (int i = 0; i < listCount; i++){ list.append(mylist[i]); } cout << "Original List\n"; list.print(); cout << endl;... } Save as listTest.cpp

20 What is this Object ?  The plan is to describe a thinking cap by telling you what actions can be done to it.

21 Using the Object’s Slots  You may put a piece of paper in each of the two slots (green and red), with a sentence written on each.  You may push the green button and the thinking cap will speak the sentence from the green slot’s paper.  And same for the red button.

22 Example

23 That test was a breeze !

24 Example I should study harder !

25 Thinking Cap Implementation  We can implement the thinking cap using a data type called a class. class thinking_cap {... };

26 Thinking Cap Implementation  The class will have two components called green_string and red_string. These compnents are strings which hold the information that is placed in the two slots.  Using a class permits two new features... class thinking_cap {... char green_string[50]; char red_string[50]; };

27 Thinking Cap Implementation  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 thinking_cap {... private: char green_string[50]; char red_string[50]; };

28 Thinking Cap Implementation  In a class, the functions which manipulate the class are also listed. class thinking_cap { public:... private: char green_string[50]; char red_string[50]; }; Prototypes for the thinking cap member functions go here

29 Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; Our thinking cap has at least three member functions: Function bodies will be elsewhere.

30 Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; The keyword const appears after two prototypes: This means that these functions will not change the data stored in a thinking_cap.

31 Files for the Thinking Cap  The thinking_cap class definition, which we have just seen, is placed with documentation in a file called thinker.h, outlined here.  The implementations of the three member functions will be placed in a separate file called thinker.cxx, which we will examine in a few minutes. Documentation Class definition: thinking_cap class definition which we have already seen

32 Using the Thinking Cap  A program that wants to use the thinking cap must include the thinker header file (along with its other header inclusions). #include #include "thinker.h"...

33 Using the Thinking Cap  Just for fun, the example program will declare two thinking_cap objects named student and fan. #include #include "thinker.h" int main( ) { thinking_cap student; thinking_cap fan;

34 Using the Thinking Cap  The program starts by calling the slots member function for student. #include #include "thinker.h" int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye");

35 Using the Thinking Cap  The member function activation consists of four parts, starting with the object name. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); Name of the object

36 Using the Thinking Cap  The instance name is followed by a period. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); A Period

37 Using the Thinking Cap  After the period is the name of the member function that you are activating. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); Name of the Function

38 Using the Thinking Cap  Finally, the arguments for the member function. In this example the first argument (new_green) is "Hello" and the second argument (new_red) is "Goodbye". #include "thinker.h" int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); Arguments

39 A Quiz How would you activate student's push_green member function ? What would be the output of student's push_green member function at this point in the program ? int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye");

40 A Quiz Notice that the push_green member function has no arguments. At this point, activating student.push_green will print the string Hello. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); student. push_green( );

41 A Quiz Trace through this program, and tell me the complete output. int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); fan. slots( "Go Cougars!", "Boo!"); student. push_green( ); fan. push_green( ); student. push_red( );...

42 A Quiz Hello Go Cougars! Goodbye int main( ) { thinking_cap student; thinking_cap fan; student. slots( "Hello", "Goodbye"); fan. slots( "Go Cougars!", "Boo!"); student. push_green( ); fan. push_green( ); student. push_red( );...

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

44 Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ); void push_red( ); private: char green_string[50]; char red_string[50]; }; Remember that the member function’s bodies generally appear in a separate.cxx file. Function bodies will be in.cxx file.

45 Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ); void push_red( ); private: char green_string[50]; char red_string[50]; }; We will look at the body of slots, which must copy its two arguments to the two private member variables.

46 Thinking Cap Implementation void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } 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...

47 Thinking Cap 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. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); }

48 Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); }

49 Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } But, whose member variables are these? Are they student.green_string student.red_string fan.green_string fan.red_string ?

50 Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } If we activate student.slots: student.green_string student.red_string

51 Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } If we activate fan.slots: fan.green_string fan.red_string

52 Thinking Cap Implementation void thinking_cap::push_green { cout << green_string << endl; } Here is the implementation of the push_green member function, which prints the green message:

53 Thinking Cap Implementation void thinking_cap::push_green { cout << green_string << endl; } Here is the implementation of the push_green member function, which prints the green message: Notice how this member function implementation uses the green_string member variable of the object.

54 A Common Pattern  Often, one or more member functions will place data in the member variables... class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; ...so that other member functions may use that data. slots push_green & push_red

55 Member Functions class Circle { public: Circle(double r); double getRadius() const; double circumference() const; double area() const; void setRadius(double r); private: double radius; }; Constructors Circle(double r) Accesor Functions getRadius() Calculator Functions circumference() area() Modifier Functions setRadius(double r) Others

56 Member Variables class Circle { public: Circle(double r); double getRadius() const; double circumference() const; double area() const; void setRadius(double r); private: double radius; }; double radius

57 Constructors  Used to instantiate an object of a given class o E.g., in main (): Circle c1; Circle c2(3.0);  Has the same name as the class name o E.g., class Circle( Circle();  Has no return type  Can be overloaded o E.g., Circle(); Circle(double r);

58 Operator Overloading int main(){ Fraction f1(3, 4); Fraction f2(1, 2);... Fraction sum = f1.add(f2); Fraction diff = f1.subtract(f2);... } class Fraction{ public: Fraction(int n, int d); Fraction add(Fraction f);...

59 Operator Overloading int main(){ Fraction f1(3, 4); Fraction f2(1, 2);... Fraction sum = f1 + f2; Fraction diff = f1 – f2;... } class Fraction{ public: Fraction(int n, int d); Fraction +(Fraction f);... It would be nice if we could use the operator symbol “+,” instead of “add”

60 Operator Overloading class Fraction{ public:... Fraction +(Fraction f);... Fraction Fraction::operator +(Fraction f){ Fraction s; s.numer = numer * f.denom + denom * f.numer; s.denom = denom * f.denom; return s; }

61 Using Overloaded Operator class Fraction{ public:... Fraction +(Fraction f);... int main(){ Fraction f1(3, 4); Fraction f2(1, 2);... Fraction sum = f1 + f2;... } Think of

62  Classes have member variables and member functions.  An object is a variable where the data type is a class.  Class is like a template (cookie cutter) for creating objects of that class.  Creating an object of a class is described as “instantiating” the class. E.g., Thinking_cap mycap; Summary


Download ppt "Abstract Data Type and C++ Classes 1. Object-oriented Programming 2. Abstract Data Types 3. Classes and Objects 4. Examples 5. Member Functions and Member."

Similar presentations


Ads by Google