1/30 Chapter 8: Dynamic Binding And Abstract classes.

Презентация:



Advertisements
Похожие презентации
1/27 Chapter 9: Template Functions And Template Classes.
Advertisements

Unit II Constructor Cont… Destructor Default constructor.
1/13 Chapter 06- Implementing Operators in a class.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
1 © Luxoft Training 2012 Inner and anonymous classes.
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
© Luxoft Training 2013 Using Reflection API in Java.
© 2006 Cisco Systems, Inc. All rights reserved. MPLS v Complex MPLS VPNs Introducing Central Services VPNs.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
1 When And How To Make Interfaces Work For You Advanced OO Techniques Alex Herbstritt Principal Software Engineer, Progress Software Session 102.
S4-1 PAT328, Section 4, September 2004 Copyright 2004 MSC.Software Corporation SECTION 4 FIELD IMPORT AND EXPORT.
Object-Oriented Programming Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
What to expect? How to prepare? What to do? How to win and find a good job? BUSINESS ENGLISH COURSE NOVA KAKHOVKA GUMNASUIM 2012.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
Транксрипт:

1/30 Chapter 8: Dynamic Binding And Abstract classes

2/30 Objectives What is static binding? What is Dynamic binding? Restriction of overriding methods Virtual method Virtual function table Type conformity Abstract classes A demonstration

3/ Static binding 5 Circle Constructor class Circle { int x,y,r; public: Circle (int xx, int yy, int rr) { x=xx; y=yy; r=rr; } void print() { cout <<x<<y<<r; } }; void main() { Circle c(3,4,5); c.print(); } 4 3 Circle print() main() [180,65520] [120,65520] c Data is associated with corresponding code at compile-time

4/ Dynamic binding pc Circle Constructor class Circle { int x,y,r; public: Circle (int xx, int yy, int rr) { x=xx; y=yy; r=rr; } void print() { cout <<x<<y<<r; } }; void main() { Circle *pc; pc= new Circle(3,4,5); pc->print(); } Circle print() main() [180,[65540]] [120,[65540]] Data is associated with corresponding code at run-time 5 Circle Constructor 4 3 Circle print() main() [180,1200] [120,1200] 1200

5/ Restriction of overriding Functions Perhaps, you want to see Son on the screen.

6/ Virtual Functions- Polymorphism Polymorphism ability occurs only when you use a pointer to an object and used-methods of classes are virtual methods virtual ReturnType or ReturnType virtual are accepted

7/ Virtual Function Table Nephew::print() Son::print() Father::print() [print, 600] […, …] VFT- Nephew VFT- Son VFT- Father [print, 500] […, …] [print, 400] […, …] main() [600, dataX] [500,dataX] [400,dataX] 800

8/ Type Conformity Pointer of base class can point to an subclass object Down Type casting OK

9/30 Type conformity…. Error: Can not convert Point2* to Pont3* Opposite Type casting NO OK

10/30 Type conformity…. Explicit Type casting OK

11/30 Type conformity… Two classes have no relation. Explicit type casting OK

12/ Abstract class Result of so-high generation class Circle int x,y,r; void print() double area() double perimeter() class Rectangle int x1,y1,x2,y2; void print() double area() double perimeter() class Triangle int x1,y1,x2,y2,x3,y3; void print() double area() double perimeter() class Shape void print() double area() double perimeter() How will we implement these methods? Pure virtual methods

13/30 Abstract class… Abstract class must have at least one pure virtual method Pure virtual method is a method with no body. Syntax of pure virtual method: virtual DataType Method (…) = 0; You cannot create an object of abstract class but you can declare a pointer of abstract class. This pointer must be point to an object of a concrete class.

14/30 Abstract class….

15/30 Abstract subclass Error: Cannot create instance of abstract class B A is an abstract class B is public subclass of A In B, the inherited method MA() is not overriden yet B is abstract class

16/30 Abstract subclass… Subclass of a concrete class may be an abstract class. Error: Cannot create instance of abstract class B

17/ A Demonstration The following program depicts using abstract class. People generate all concrete classes as Circle, Rectangle,… into Shape class. User will input some shape details Program will print out details of all shape Values of area and perimeter of each shape will be printed also.

18/30 Class Shape and Circle

19/30 Class Rectangle

20/30 Class ShapeList

21/30 Class ShapeList…, main(), Result

22/ Class Object elements and Class Vector for arbitrary elements

23/ …

24/ Class Student:public Object

25/ Class Circle:public Object

26/ Main

27/30 Summary Virtual Method is a way to make polymorphism. Syntax for virtual method: virtual ReturnType Method (parameters) ReturnType virtual Method (parameters) Compiler will determine the right method will be called using a virtual function table for every class which contains virtual methods. Pure virtual method is a virtual method but it has no code. Syntax for pure virtual method: virtual ReturnType Method (parameters)=0;

28/30 Summary Abstract class is a result of so-high generation. Abstract class must have at least one pure virtual method. You can not create an object of abstract class but you can declare a pointer to it then, it points to an object of a concrete subclass.

29/30 Exercises Using the class Object, implement classes: Father, Mother, Son, Daughter. Write a program will –Input a list of members in a family. Store them into a Vector object. –Print out members of the family.

30/30 Thanks