🗊Презентация Nheritance, polymorphism, and virtual functions

Нажмите для полного просмотра!
Nheritance, polymorphism, and virtual functions, слайд №1Nheritance, polymorphism, and virtual functions, слайд №2Nheritance, polymorphism, and virtual functions, слайд №3Nheritance, polymorphism, and virtual functions, слайд №4Nheritance, polymorphism, and virtual functions, слайд №5Nheritance, polymorphism, and virtual functions, слайд №6Nheritance, polymorphism, and virtual functions, слайд №7Nheritance, polymorphism, and virtual functions, слайд №8Nheritance, polymorphism, and virtual functions, слайд №9Nheritance, polymorphism, and virtual functions, слайд №10Nheritance, polymorphism, and virtual functions, слайд №11Nheritance, polymorphism, and virtual functions, слайд №12Nheritance, polymorphism, and virtual functions, слайд №13Nheritance, polymorphism, and virtual functions, слайд №14Nheritance, polymorphism, and virtual functions, слайд №15Nheritance, polymorphism, and virtual functions, слайд №16Nheritance, polymorphism, and virtual functions, слайд №17Nheritance, polymorphism, and virtual functions, слайд №18Nheritance, polymorphism, and virtual functions, слайд №19Nheritance, polymorphism, and virtual functions, слайд №20Nheritance, polymorphism, and virtual functions, слайд №21Nheritance, polymorphism, and virtual functions, слайд №22Nheritance, polymorphism, and virtual functions, слайд №23Nheritance, polymorphism, and virtual functions, слайд №24Nheritance, polymorphism, and virtual functions, слайд №25Nheritance, polymorphism, and virtual functions, слайд №26Nheritance, polymorphism, and virtual functions, слайд №27Nheritance, polymorphism, and virtual functions, слайд №28Nheritance, polymorphism, and virtual functions, слайд №29Nheritance, polymorphism, and virtual functions, слайд №30Nheritance, polymorphism, and virtual functions, слайд №31Nheritance, polymorphism, and virtual functions, слайд №32Nheritance, polymorphism, and virtual functions, слайд №33Nheritance, polymorphism, and virtual functions, слайд №34Nheritance, polymorphism, and virtual functions, слайд №35Nheritance, polymorphism, and virtual functions, слайд №36Nheritance, polymorphism, and virtual functions, слайд №37Nheritance, polymorphism, and virtual functions, слайд №38Nheritance, polymorphism, and virtual functions, слайд №39Nheritance, polymorphism, and virtual functions, слайд №40Nheritance, polymorphism, and virtual functions, слайд №41Nheritance, polymorphism, and virtual functions, слайд №42Nheritance, polymorphism, and virtual functions, слайд №43Nheritance, polymorphism, and virtual functions, слайд №44Nheritance, polymorphism, and virtual functions, слайд №45Nheritance, polymorphism, and virtual functions, слайд №46Nheritance, polymorphism, and virtual functions, слайд №47Nheritance, polymorphism, and virtual functions, слайд №48

Содержание

Вы можете ознакомиться и скачать презентацию на тему Nheritance, polymorphism, and virtual functions. Доклад-сообщение содержит 48 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





Lesson 15
Inheritance, Polymorphism, and Virtual Functions
Описание слайда:
Lesson 15 Inheritance, Polymorphism, and Virtual Functions

Слайд 2





What Is Inheritance?
Provides a way to create a new class from an existing class
The new class is a specialized version of the existing class
Описание слайда:
What Is Inheritance? Provides a way to create a new class from an existing class The new class is a specialized version of the existing class

Слайд 3





Example: Insect Taxonomy
Описание слайда:
Example: Insect Taxonomy

Слайд 4





The "is a" Relationship
Inheritance establishes an "is a" relationship between classes.
A poodle is a dog
A car is a vehicle
A flower is a plant
A football player is an athlete
Описание слайда:
The "is a" Relationship Inheritance establishes an "is a" relationship between classes. A poodle is a dog A car is a vehicle A flower is a plant A football player is an athlete

Слайд 5





Inheritance – Terminology and Notation in C++
Base class (or parent) – inherited from
Derived class (or child) – inherits from the base class
Notation:
	class Student 	      // base class
	{
		. . .
	};
	class UnderGrad : public student 
	{					// derived class
		. . .
	};
Описание слайда:
Inheritance – Terminology and Notation in C++ Base class (or parent) – inherited from Derived class (or child) – inherits from the base class Notation: class Student // base class { . . . }; class UnderGrad : public student { // derived class . . . };

Слайд 6





Back to the ‘is a’ Relationship
An object of a derived class 'is a(n)' object of the base class
Example: 
an UnderGrad is a Student
a Mammal is an Animal
A derived object has all of the characteristics of the base class
Описание слайда:
Back to the ‘is a’ Relationship An object of a derived class 'is a(n)' object of the base class Example: an UnderGrad is a Student a Mammal is an Animal A derived object has all of the characteristics of the base class

Слайд 7





What Does a Child Have?
An object of the derived class has:
all members defined in child class
all members declared in parent class

An object of the derived class can use:
all public members defined in child class
all public members defined in parent class
Описание слайда:
What Does a Child Have? An object of the derived class has: all members defined in child class all members declared in parent class An object of the derived class can use: all public members defined in child class all public members defined in parent class

Слайд 8





Protected Members and                   Class Access
protected member access specification: like private, but accessible by objects of derived class
Class access specification: determines how private, protected, and public members of base class are inherited by the derived class
Описание слайда:
Protected Members and Class Access protected member access specification: like private, but accessible by objects of derived class Class access specification: determines how private, protected, and public members of base class are inherited by the derived class

Слайд 9





Class Access Specifiers
public – object of derived class can be treated as object of base class (not vice-versa)
protected – more restrictive than public, but allows derived classes to know details of parents
private – prevents objects of derived class from being treated as objects of base class.
Описание слайда:
Class Access Specifiers public – object of derived class can be treated as object of base class (not vice-versa) protected – more restrictive than public, but allows derived classes to know details of parents private – prevents objects of derived class from being treated as objects of base class.

Слайд 10





Inheritance vs. Access
Описание слайда:
Inheritance vs. Access

Слайд 11





Inheritance vs. Access
Описание слайда:
Inheritance vs. Access

Слайд 12





Inheritance vs. Access
Описание слайда:
Inheritance vs. Access

Слайд 13





Inheritance vs. Access
Описание слайда:
Inheritance vs. Access

Слайд 14





Constructors and Destructors in Base and Derived Classes
Derived classes can have their own constructors and destructors
When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor
When an object of a derived class is destroyed, its destructor is called first, then that of the base class
Описание слайда:
Constructors and Destructors in Base and Derived Classes Derived classes can have their own constructors and destructors When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor When an object of a derived class is destroyed, its destructor is called first, then that of the base class

Слайд 15





Constructors and Destructors in Base and Derived Classes
Описание слайда:
Constructors and Destructors in Base and Derived Classes

Слайд 16





Constructors and Destructors in Base and Derived Classes
Описание слайда:
Constructors and Destructors in Base and Derived Classes

Слайд 17





Constructors and Destructors in Base and Derived Classes
Описание слайда:
Constructors and Destructors in Base and Derived Classes

Слайд 18





Passing Arguments to 
Base Class Constructor
Allows selection between multiple base class constructors
Specify arguments to base constructor on derived constructor heading:
	Square::Square(int side) : 					Rectangle(side, side) 
Can also be done with inline constructors
Must be done if base class has no default constructor
Описание слайда:
Passing Arguments to Base Class Constructor Allows selection between multiple base class constructors Specify arguments to base constructor on derived constructor heading: Square::Square(int side) : Rectangle(side, side) Can also be done with inline constructors Must be done if base class has no default constructor

Слайд 19





Passing Arguments to 
Base Class Constructor
Описание слайда:
Passing Arguments to Base Class Constructor

Слайд 20





Redefining Base Class Functions
Redefining function: function in a derived class that has the same name and parameter list as a function in the base class

Typically used to replace a function in base class with different actions in derived class
Описание слайда:
Redefining Base Class Functions Redefining function: function in a derived class that has the same name and parameter list as a function in the base class Typically used to replace a function in base class with different actions in derived class

Слайд 21





Redefining Base Class Functions
Not the same as overloading – with overloading, parameter lists must be different
Objects of base class use base class version of function; objects of derived class use derived class version of function
Описание слайда:
Redefining Base Class Functions Not the same as overloading – with overloading, parameter lists must be different Objects of base class use base class version of function; objects of derived class use derived class version of function

Слайд 22





Base Class
Описание слайда:
Base Class

Слайд 23





Derived Class
Описание слайда:
Derived Class

Слайд 24





Driver Program
Описание слайда:
Driver Program

Слайд 25





Problem with Redefining
Consider this situation:
Class BaseClass defines functions x() and y().   x() calls y(). 
Class DerivedClass inherits from BaseClass and redefines function y().
An object D of class DerivedClass is created and function x() is called.  
When x() is called, which y() is used, the one defined in BaseClass or the the redefined one in DerivedClass?
Описание слайда:
Problem with Redefining Consider this situation: Class BaseClass defines functions x() and y(). x() calls y(). Class DerivedClass inherits from BaseClass and redefines function y(). An object D of class DerivedClass is created and function x() is called. When x() is called, which y() is used, the one defined in BaseClass or the the redefined one in DerivedClass?

Слайд 26





Problem with Redefining
Описание слайда:
Problem with Redefining

Слайд 27





Class Hierarchies
Описание слайда:
Class Hierarchies

Слайд 28





Class Hierarchies
Описание слайда:
Class Hierarchies

Слайд 29





Polymorphism and Virtual Member Functions
Virtual member function: function in base class that expects to be redefined in derived class
Function defined with key word virtual:
virtual void Y() {...}
Supports dynamic binding: functions bound at run time to function that they call
Without virtual member functions, C++ uses static (compile time) binding
Описание слайда:
Polymorphism and Virtual Member Functions Virtual member function: function in base class that expects to be redefined in derived class Function defined with key word virtual: virtual void Y() {...} Supports dynamic binding: functions bound at run time to function that they call Without virtual member functions, C++ uses static (compile time) binding

Слайд 30





Polymorphism and Virtual Member Functions
Описание слайда:
Polymorphism and Virtual Member Functions

Слайд 31


Nheritance, polymorphism, and virtual functions, слайд №31
Описание слайда:

Слайд 32


Nheritance, polymorphism, and virtual functions, слайд №32
Описание слайда:

Слайд 33





Static Binding
Program 15-10 displays 'C' instead of 'P' because the call to the getLetterGrade function is statically bound (at compile time) with the GradedActivity class's version of the function.
We can remedy this by making the function virtual.
Описание слайда:
Static Binding Program 15-10 displays 'C' instead of 'P' because the call to the getLetterGrade function is statically bound (at compile time) with the GradedActivity class's version of the function. We can remedy this by making the function virtual.

Слайд 34





Virtual Functions
A virtual function is dynamically bound to calls at runtime.
At runtime, C++ determines the type of object making the call, and binds the function to the appropriate version of the function.
Описание слайда:
Virtual Functions A virtual function is dynamically bound to calls at runtime. At runtime, C++ determines the type of object making the call, and binds the function to the appropriate version of the function.

Слайд 35





Virtual Functions
To make a function virtual, place the virtual key word before the return type in the base class's declaration:

  virtual char getLetterGrade() const;
The compiler will not bind the function to calls. Instead, the program will bind them at runtime.
Описание слайда:
Virtual Functions To make a function virtual, place the virtual key word before the return type in the base class's declaration: virtual char getLetterGrade() const; The compiler will not bind the function to calls. Instead, the program will bind them at runtime.

Слайд 36





Updated Version of GradedActivity
Описание слайда:
Updated Version of GradedActivity

Слайд 37





Polymorphism
Описание слайда:
Polymorphism

Слайд 38


Nheritance, polymorphism, and virtual functions, слайд №38
Описание слайда:

Слайд 39


Nheritance, polymorphism, and virtual functions, слайд №39
Описание слайда:

Слайд 40





Polymorphism Requires References or Pointers
Polymorphic behavior is only possible when an object is referenced by a reference variable or a pointer, as demonstrated in the displayGrade function.
Описание слайда:
Polymorphism Requires References or Pointers Polymorphic behavior is only possible when an object is referenced by a reference variable or a pointer, as demonstrated in the displayGrade function.

Слайд 41





Base Class Pointers
Описание слайда:
Base Class Pointers

Слайд 42





Base Class Pointers
Base class pointers and references only know about members of the base class
So, you can’t use a base class pointer to call a derived class function

Redefined functions in derived class will be ignored unless base class declares the function virtual
Описание слайда:
Base Class Pointers Base class pointers and references only know about members of the base class So, you can’t use a base class pointer to call a derived class function Redefined functions in derived class will be ignored unless base class declares the function virtual

Слайд 43





Redefining vs. Overriding
In C++, redefined functions are statically bound and overridden functions are dynamically bound.
So, a virtual function is overridden, and a non-virtual function is redefined.
Описание слайда:
Redefining vs. Overriding In C++, redefined functions are statically bound and overridden functions are dynamically bound. So, a virtual function is overridden, and a non-virtual function is redefined.

Слайд 44





Virtual Destructors
It's a good idea to make destructors virtual if the class could ever become a base class.
Otherwise, the compiler will perform static binding on the destructor if the class ever is derived from.
See Program 15-14 for an example
Описание слайда:
Virtual Destructors It's a good idea to make destructors virtual if the class could ever become a base class. Otherwise, the compiler will perform static binding on the destructor if the class ever is derived from. See Program 15-14 for an example

Слайд 45





Abstract Base Classes and Pure Virtual Functions
Pure virtual function: a virtual member function that must be overridden in a derived class that has objects
Abstract base class contains at least one pure virtual function:
	virtual void Y() = 0;
The = 0 indicates a pure virtual function
Must have no function definition in the base class
Описание слайда:
Abstract Base Classes and Pure Virtual Functions Pure virtual function: a virtual member function that must be overridden in a derived class that has objects Abstract base class contains at least one pure virtual function: virtual void Y() = 0; The = 0 indicates a pure virtual function Must have no function definition in the base class

Слайд 46





Abstract Base Classes and Pure Virtual Functions
Abstract base class: class that can have no objects.  Serves as a basis for derived classes that may/will have objects
A class becomes an abstract base class when one or more of its member functions is a pure virtual function
Описание слайда:
Abstract Base Classes and Pure Virtual Functions Abstract base class: class that can have no objects. Serves as a basis for derived classes that may/will have objects A class becomes an abstract base class when one or more of its member functions is a pure virtual function

Слайд 47





Multiple Inheritance
Описание слайда:
Multiple Inheritance

Слайд 48





Multiple Inheritance
Problem:  what if base classes have member variables/functions with the same name?
Solutions:
Derived class redefines the multiply-defined function
Derived class invokes member function in a particular base class using scope resolution operator ::
Compiler errors occur if derived class uses base class function without one of these solutions
Описание слайда:
Multiple Inheritance Problem: what if base classes have member variables/functions with the same name? Solutions: Derived class redefines the multiply-defined function Derived class invokes member function in a particular base class using scope resolution operator :: Compiler errors occur if derived class uses base class function without one of these solutions



Похожие презентации
Mypresentation.ru
Загрузить презентацию