Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
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.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object-Oriented Programming
Object-Oriented Programming CS 3360 Spring 2012 Sec , Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
ISBN Chapter 12 Support for Object- Oriented Programming.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 12: Adding Functionality to Your Classes.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
ISBN Chapter 12 Support for Object-Oriented Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CS 403 – Programming Languages Class 25 November 28, 2000.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 10 Inheritance and Polymorphism
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12 Support for Object-Oriented Programming.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
ISBN Object-Oriented Programming Chapter Chapter
Dynamic Binding Implementation Object-Oriented Programming Spring
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Overview of C++ Polymorphism
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
ISBN Chapter 12 Support for Object-Oriented Programming.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Support for Object-Oriented Programming
Support for Object-Oriented Programming
7. Inheritance and Polymorphism
Polymorphism.
Programming Language Concepts (CIS 635)
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance, Polymorphism, and Interfaces. Oh My
Support for Object-Oriented Programming in Ada 95
Polymorphism CT1513.
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Inheritance: Polymorphism and Virtual Functions
Object-Oriented Programming
Overview of C++ Polymorphism
Lecture 6: Polymorphism
Presentation transcript:

Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public: ……// can be accessed by any }; class B : public A { private: …… protected: …… public: …… }; What does class B : public A mean? Based on an existing class A, we can create a new class B. If so, we have:  B is a derived class or subclass of A.  A is a parent class or superclass of B.

Message protocol class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public: ……// can be accessed by any void f(); int g(); float k(); …… }; Message protocol : the entire collection of methods of an object, i.e. { void A::f(), int A:: g(),float A:: f() … }

Instance variables, class variables etc. class A { private: int a; //instance variable protected: int x, y; //instance variable public: static int b; // class variable A() { // constructor, same name as class name a = b = 0; x = y = 0; } static int g() { return b; } // class method void f(int m, int n) { //instance method a+= m; b+= n; } }; int A::b = 2; // initialize class variable void main() { A a0; A*a1 = new A; a0.a = 2; // ok ? //Line K a0.b = 3; // ok? a1->x = 5; // ok? //Line L a0.f(1, 2); a1->f(3, 5); cout << a0.a << endl; cout << a0.b << endl; cout a << endl; cout b << endl; } Assume Lines K and L are removed. What are the outputs?

Instance variables, class variables etc. Instance variables: each instance of a class has its own set of instance variables. Class variables: all instances of a class share one copy each class variable. Example: A a0, a1, a2; 1.a0, a1, a2 share a single A::b i.e. a0.b, a1.b, a2.b are the same, A::b 2. a0.a, a1.a, a2.a are all different variables. class A { private: int a; //instance variable protected: int x, y; //instance variable public: static int b; // class variable A() { // constructor, same name as class name a = b = 0; x = y = 0; } static int g() { return b; } // class method void f(int m, int n) { //instance method a+= m; b+= n; } };

Object-oriented programming: C++ class A { private: int a; //instance variable static int b; // class variable protected: int x, y; public: A() { // constructor a = b = 0; x = y = 0; } void f() { a++; b++; } }; class B: public A { private: int x; //instance variable public: B() { // constructor c = 0; } void g() { x = 5; // A’s x or B’s x? } }; How to access A’s x in B’s methods?

Dynamic binding: virtual functions class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public: A(); virtual void f() = 0; // pure (abstract) method virtual void g() { cout << “A::g” << endl; } void k() { cout << “A::k” << endl; } }; class B : public A { public: B(); virtual void f() { … } // must define the pure method void g() { cout << “B::g” << endl; } //override A’s g void k() { cout << “B::k” << endl; } //override A’s k }; void main() { B b; A *c = new B; B *d = new B; b.g(); b.k(); c->g(); c->k(); d->g(); d->k(); } What are the outputs?

Dynamic binding class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public: A(); virtual void g() { cout << “A::g” << endl; } void k() { cout << “A::k” << endl; } }; class B : public A { public: B(); void g() { cout << “B::g” << endl; } void k() { cout << “B::k” << endl; } }; void main() { A a; B b; A *c = &b; B *d = new B; a.g(); a.k(); b.g(); b.k(); c->g(); c->k(); d->g(); d->k(); } What are the outputs?

Dynamic binding class A { private: public: A(); virtual void g() { cout << “A::g” << endl; } void k() { cout << “A::k” << endl; } }; class B : public A { public: B(); void g() { cout << “B::g” << endl; } void k() { cout << “B::k” << endl; } }; class C : public A { public: C(); void g() { cout << “C::g” << endl; } void k() { cout << “C::k” << endl; } }; void main() { A a; B b; C c; A *d; d = &a; d->g(); d->k(); d = &b; d->g(); d->k(); d = &c; d->g(); d->k(); } What are the outputs?

class A { private: public: void g(); }; class B { public: void f(); }; class C : public A, B { public: … … }; Multiple inheritance What happens if both A and B define functions with same name? Answer: Compilation error if C does not define them! A B C

class A { private: public: void g(); }; class B { public: void g(); }; class C : public A, B { public: //not define g() }; Error! Multiple inheritance class A { private: public: void g(); }; class B { public: void g(); }; class C : public A, B { public: void g(); }; OK! WHY?

void main() { A *a; B b; C c; a = &b; a->f(); a->g(); a->k(); a = &c; a->f(); a->g(); a->k(); c.f(); c.g(); c.k(); a->a = 5; cout << b.a << endl; cout << c.a << endl; } What are the outputs? class A { public: A() { } static int a; virtual void f() { cout << "A::f" << endl;} void g() {cout << "A::g" << endl;} void k() {cout << "A::k" << endl;} }; int A::a = 0; class B : public A { public: B() { } virtual void f() { cout << "B::f" << endl;} void g() {cout << "B::g" << endl;} void k() {cout << "B::k" << endl;} }; class C : public A, B { public: C() { } virtual void f() { cout << "C::f" << endl;} void g() {cout << "C::g" << endl;} void k() {cout << "C::k" << endl;} }; An exercise

void main() { A *a; B b; C c; a = &b; a->f(); a->g(); a->k(); a = &c; a->f(); a->g(); a->k(); c.f(); c.g(); c.k(); a->a = 5; cout << b.a << endl; cout << c.a << endl; } What are the outputs? class A { public: A() { } static int a; virtual void f() { cout << "A::f" << endl;} void g() {cout << "A::g" << endl;} void k() {cout << "A::k" << endl;} }; int A::a = 0; class B : public A { public: B() { } virtual void f() { cout << "B::f" << endl;} void g() {cout << "B::g" << endl;} void k() {cout << "B::k" << endl;} }; class C : public A, B { public: C() { } virtual void f() { cout << "C::f" << endl;} void g() {cout << "C::g" << endl;} void k() {cout << "C::k" << endl;} }; An exercise: solution B::f A::g A::k C::f A::g A::k C::f C::g C::k 5

Polymorphism If B is a subclass of A, we can do the following: A *a; a = new B; If B is not a subclass of A, we CANNOT write a = new B.

Polymorphism void main() { A a; B b; C c; D d; … a.draw(); b.draw(); c.draw(); d.draw(); … } class A { public: A() { } virtual void draw() { … } }; class B : public A { public: B() { } void draw() { … } }; class C : public A { public: C() { } void draw() { … } }; class D … class E… #define N 100 void main() { A *a[N]; a[0] = new A; a[1] = new B; a[2] = new C; … for (int i = 0; i < N; i++) { a[i]->draw(); }

Copyright © 2009 Addison-Wesley. All rights reserved.1-15 Are Subclasses Subtypes? Does an “is-a” relationship hold between a parent class object and an object of the subclass? – If a derived class is-a parent class, then objects of the derived class must behave the same as the parent class object A derived class is a subtype if it has an is-a relationship with its parent class – Subclass can only add variables and methods and override inherited methods in “compatible” ways

Copyright © 2009 Addison-Wesley. All rights reserved.1-16 Type Checking and Polymorphism Polymorphism may require dynamic type checking of parameters and the return value – Dynamic type checking is costly and delays error detection If overriding methods are restricted to having the same parameter types and return type, the checking can be static

Copyright © 2009 Addison-Wesley. All rights reserved.1-17 Instance Data Storage Class instance records (CIRs) store the state of an object – Static (built at compile time) If a class has a parent, the subclass instance variables are added to the parent CIR Because CIR is static, access to all instance variables is done as it is in records – Efficient