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

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.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Lecture 9 Concepts of Programming Languages
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Abstract Data Types and Encapsulation Concepts
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
OOP Languages: Java vs C++
Chapter 11 Abstract Data Types and Encapsulation Concepts.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Programming Languages and Paradigms Object-Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 13.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
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.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Object Oriented Programming in C++ Chapter 6 Inheritance.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
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.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Oriented Programming Chapter Chapter
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
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.
1 CS Programming Languages Class 22 November 14, 2000.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Overview of C++ Polymorphism
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Introduction to Object Oriented Programming Chapter 10.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
ECE 264 Object-Oriented Software Development
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.
Chapter 2 Objects and Classes
Abstract Data Types and Encapsulation Concepts
Overloading Operator MySting Example
Inheritance and Polymorphism
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
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
Overview of C++ Polymorphism
Java Programming Language
Object-Oriented Programming (Part 2)
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

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

The this Pointer To invoke a class member function, use the ''dot'' operator (structure field selection):. 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 Q ualifier (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 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; }; delete tmp1; } 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 : public { } All members of are also members of. The 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: queue() { } ~queue() { } 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:

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, public manager { /*... */ };

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: 1.Only use pointers to classes that have no derived classes. Sure... 2.Use a variable to determine the type of the object. Very easy to forget to check the type variable. 3.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)

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 > We wish to use > for user-defined objects, the same way they are (normally) used for "built-in" objects (e.g. int, char, etc.).

Overloading Operators > 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()

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