Object-Oriented Programming (Part 2)

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

C++ Classes & Data Abstraction
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Programming Language Concepts Lecture 19 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming (Part.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Lecture 9 Concepts of Programming Languages
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
1 CSC241: Object Oriented Programming Lecture No 13.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Object Oriented Programming in C++ Chapter 6 Inheritance.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Object-Oriented Programming Chapter Chapter
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Overview of C++ Polymorphism
1 Introduction to Object Oriented Programming Chapter 10.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Chapter 2 Objects and Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Popping Items Off a Stack Using a Function Lesson xx
Abstract Data Types and Encapsulation Concepts
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Inheritance CMSC 202, Version 4/02.
Overloading Operator MySting Example
Abstract Data Types Programmer-created data types that specify
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Programming (OOP) Lecture No. 45
Inheritance and Polymorphism
C++ Plus Data Structures
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Review: Two Programming Paradigms
11.1 The Concept of Abstraction
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 2 Objects and Classes
Pointers and Linked Lists
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Corresponds with Chapter 7
Abstract Data Types and Encapsulation Concepts
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Overview of C++ Polymorphism
Java Programming Language
Lab4 problems More about templates Some STL
Classes C++ representation of an object
Lecture 10 Concepts of Programming Languages
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Object-Oriented Programming (Part 2) Programming Language Principles Lecture 31 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida

The this Pointer To invoke a class member function, use the ''dot'' operator (structure field selection): <object name> . <member function name> The this pointer: an implicit parameter to each class member function, of type (Object *). Its value is the address of the “current” object, through which the member function has been invoked. It can be used inside member functions. Examples coming up.

The const Qualifier Three places where the const qualifier can be used in a member function declaration: class A { ... const X& f(const Y& y) const { // 1. Can't assign to object returned // 2. Can't change parameter y // 3. Can't change this. }

The const Qualifier (cont’d) A a; ... a.f(y) // f is a member of class A. // a is an object of class A. // a.f(y) can't be assigned a value. // a.f(y) cannot change y. // a.f(y) cannot change a.

Constructors cell(int i, cell *n) {info=i; next=n;} int info; class cell { cell(int i, cell *n) {info=i; next=n;} int info; cell *next; }; cell a(1,0); //declare a and call cell(1,0) cell b(2,&a); //declare b and call cell(2,&a) These don’t just declare a and b of type cell. They also call the constructor function cell.

Overloading Constructors class cell { cell (int i) {info=i; next=this;} cell (int i, cell *n) { info=i; next=n; } int info; cell *next; }; cell c(7);

Friend Classes and Information Hiding Class members are private by default. A friend declaration in a class allows functions from another class to access members of the current one. class cell { friend class circlist; cell (int i) {info=i; next=this;} cell (int i, cell *n) {info=i; next=n;} int info; cell *next; }; cell has no public interface. It’s an 'auxiliary' data type, solely for use with circlist.

Friend Classes and Information Hiding class circlist { private: cell *rear; public: circlist() {rear=new cell(0);} ~circlist(); int empty() const {return rear==rear->next;} int top() const {return rear->next->info; } void push (int); int pop(); void enter(int); }; The class circlist has a total of seven member functions, four of which still need to be defined.

Friend Classes and Information Hiding circlist::~circlist(){ cell *tmp1=rear, *tmp2=rear->next; while (tmp2!=rear) { delete tmp1; tmp1=tmp2; tmp2=tmp1->next; }; } void circlist::push(int x) { rear->next = new cell (x, rear->next);

Friend Classes and Information Hiding void circlist::enter(int x) { rear->info = x; rear = rear->next = new cell(0,rear->next); } int circlist::pop() { if ( empty() ) return 0; cell *front = rear->next; rear->next = front->next; int x = front->info; delete front; return x;

Friend Classes and Information Hiding Class circlist hides its internal details. Objects of type circlist are manipulated exclusively through the public interface of circlist. Information hidden inside circlist: circlist based on cell, only rear is stored. Should the implementation of circlist ever change, users of circlist will be COMPLETELY UNAFFECTED. Three principles of Object-Oriented Programming: Abstraction, Encapsulation, Information Hiding.

Derived Classes Humans tend to abstract on two dimensions: A part-of B, (a.k.a. has_a) A kind-of B, (a.k.a. is_a). ADT programming focuses on the has_a relation. OOP also includes the is_a abstraction. Support for the is_a abstraction implies: if A is_a B, and B has_a X, then A has_a X. This is inheritance !!

Derived Classes (cont’d) In general, class <derived-class> : public <base-class> { <member-declarations> } All members of <base-class> are also members of <derived-class>. The <derived-class> may have additional members. public clause: derived members retain their attributes (public, private, or protected). private clause: derived members will be private.

The circlist class, revisited class circlist { public: int empty () const { return rear==rear->next;} int top() const { return rear->next->info; } // 'inspectors' protected: circlist () {rear = new cell(0);}; ~circlist(); void push(int); int pop(); void enter(int); // 'mutators' private: cell *rear; }

The circlist class, revisited Protected members behave as if they were private, except that: they are visible to members (and friends) of derived classes. Some of the circlist functions are now protected so they'll be inherited by the new class queue.

Derived class queue class queue: private circlist { public: void enqueue (int x) { enter(x); } int dequeue () { return pop(); } circlist::empty; circlist::top; };

Complete list of members of queue Public Functions: queue new constructor ~queue new destructor enqueue new dequeue new empty, top inherited, explicitly made public Private Functions: push inherited pop inherited enter inherited Private Variables (accessible to new functions of queue): none Private Variables (accessible only by inherited functions): rear inherited

Another class derived from circlist class stack: private circlist { public: stack () { } ~stack () { } void push (int x) { circlist::push(x); } int pop() { return circlist::pop(); } circlist::empty; circlist::top; };

Sample Use of these Classes main () { stack s; queue q; s.push(1); s.push(2); s.push(3); q.enqueue(7); q.enqueue(8); q.enqueue(9); cout << "Stack Top: " << s.top() << endl; cout << "Queue Top: " << q.top() << endl; cout << s.pop() << " "; cout << s.pop() << endl; cout << q.dequeue() << " "; cout << q.dequeue() << endl; } Results: Stack Top: 3 Queue Top: 7 3 2 1 7 8 9

Class Hierarchies class employee { /* . . . */ }; class manager: public employee { /* . . . */ }; class director: public manager { /* . . . */ }; class temporary { /* . . . */ }; class secretary: public employee { { /* . . . */ }; class tsec: public temporary, //MULTIPLE INHERITANCE !! public secretary { /* . . . */ }; class consultant: public temporary,

Class Hierarchy is a DAG

Type Identification In our example, given a pointer of type employee*, it can point to: employee, secretary, tsec, manager, or director. Three solutions, in general, to this problem: Only use pointers to classes that have no derived classes. Sure ... Use a variable to determine the type of the object. Very easy to forget to check the type variable. Use virtual functions.

Virtual Functions Allow print in both employee and manager, with different definitions. C++ will “do the right thing”, based on the actual object class.

Virtual Functions (cont’d)

Virtual Functions (cont’d)

Virtual Functions (cont’d)

Operator Overloading Allow intrinsic operators in C++ to be applied to objects of new types. Overloading is achieved by defining functions named operatorXXX, where XXX is one of: Can’t overload these: Can’t change precedence, arity or associativity. Can’t add new operators, either.

Operator Overloading (cont’d) Example: the subscript operator, which returns a reference. First, without operator overloading:

Operator Overloading (cont’d) Now, simply replace elem with operator[]:

Overloading Operators << and >> We wish to use << and >> for user-defined objects, the same way they are (normally) used for "built-in" objects (e.g. int, char, etc.).

Overloading Operators << and >> Input is similar. The signature is: To use them:

The Orthodox Canonical Form An idiom (pattern). The OCF is characterized by the presence of: A default constructor: X::X() A copy constructor: X::X(const X&) An assignment operator: X& operator=(const X&) A destructor: X::~X()

Object-Oriented Programming (Part 2) Programming Language Principles Lecture 31 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida