Chapter 8:Dynamic BindingAndAbstract classes презентация

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

Слайд 1Chapter 8: Dynamic Binding And Abstract classes


Слайд 2Objectives
What is static binding?
What is Dynamic binding?
Restriction of overriding methods
Virtual method
Virtual

function table
Type conformity
Abstract classes
A demonstration

Слайд 37.1- 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 < }
};
void main()
{ Circle c(3,4,5);
c.print();
}

4

3

Circle
print()


main()
[180,65520]
[120,65520]

800

120

180

65520 c

Data is associated with corresponding code at compile-time


Слайд 47.2- 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 < }
};
void main()
{ Circle *pc;
pc= new Circle(3,4,5);
pc->print();
}

Circle
print()

main()
[180,[65540]]
[120,[65540]]

800

120

180

65540

Data is associated with corresponding code at run-time

5

Circle
Constructor

4

3

Circle
print()


main()
[180,1200]
[120,1200]

1200

1200


Слайд 57.3- Restriction of overriding Functions
Perhaps, you want
to see “Son”
on

the screen.

Слайд 67.4- 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


Слайд 77.5- Virtual Function Table


Слайд 87.6- Type Conformity
Pointer of
base class
can point to
an subclass
object
Down
Type

casting
OK

Слайд 9Type conformity….
Error: Can not convert
“Point2*” to “Poỉnt3*”
Opposite
Type casting
NO OK


Слайд 10Type conformity….
Explicit
Type casting
OK


Слайд 11Type conformity…
Two classes
have no relation.
Explicit type casting
➔ OK


Слайд 127.7- 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


Слайд 13Abstract 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.

Слайд 14Abstract class….


Слайд 15Abstract 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

Слайд 16Abstract subclass…


Слайд 177.8-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.

Слайд 18Class Shape and Circle


Слайд 19Class Rectangle


Слайд 20Class ShapeList


Слайд 21Class ShapeList…, main(), Result


Слайд 227.9- Class Object elements and Class Vector for

arbitrary elements

Слайд 237.9- …


Слайд 247.9- Class Student:public Object


Слайд 257.9- Class Circle:public Object


Слайд 267.9- Main


Слайд 27Summary
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;

Слайд 28Summary
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.

Слайд 29Exercises
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.

Обратная связь

Если не удалось найти и скачать презентацию, Вы можете заказать его на нашем сайте. Мы постараемся найти нужный Вам материал и отправим по электронной почте. Не стесняйтесь обращаться к нам, если у вас возникли вопросы или пожелания:

Email: Нажмите что бы посмотреть 

Что такое ThePresentation.ru?

Это сайт презентаций, докладов, проектов, шаблонов в формате PowerPoint. Мы помогаем школьникам, студентам, учителям, преподавателям хранить и обмениваться учебными материалами с другими пользователями.


Для правообладателей

Яндекс.Метрика