1 / 30

Derived Classes & virtual

Derived Classes & virtual. EECE 351 Spring 2000 Lecture # 17. Overview. Introduction Virtual Abstract Base Classes What we know Where we are going. Introduction. What is a derived class? How do you derive? What are the benefits? How are pointers affected? Multiple Inheritance

Download Presentation

Derived Classes & virtual

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Derived Classes & virtual EECE 351 Spring 2000 Lecture # 17 EECE 351 – Lecture 17

  2. Overview • Introduction • Virtual • Abstract Base Classes • What we know • Where we are going EECE 351 – Lecture 17

  3. Introduction • What is a derived class? • How do you derive? • What are the benefits? • How are pointers affected? • Multiple Inheritance • 3 Ps revisited EECE 351 – Lecture 17

  4. What is a derived class? • There are two classes in the derivation process--The inheritor (derived) and the inherited (base) class. • A derived class isusually more specific. Sometimes they implement the base class. • The base class is generalized. It consists of a relatively less-specific definition. EECE 351 – Lecture 17

  5. How do you derive? class PrintedDocument {//Member list.}; class Book : public PrintedDocument {//Member list.}; class PaperbackBook : public Book {// Member list.}; PrintedDocument is considered a “direct base” class to Book; it is an “indirect base” class to PaperbackBook. defaults to private if not specified EECE 351 – Lecture 17

  6. How do you derive? This diagram is called a “directed acyclic graph” (or “DAG”). Some of the classes are base classes for more than one derived class. However, the reverse is not true: there is only one direct base class for any given derived class. The graph above depicts a “single inheritance” structure. EECE 351 – Lecture 17

  7. What are the benefits? • The derived class contains the members of the base class plus any new members you add. • As a result, a derived class can refer to members of the base class (unless those members are redefined in the derived class). • The scope-resolution operator (::) can be used to refer to members of direct or indirect baseclasses when those members have been redefined (overridden) in the derived class. EECE 351 – Lecture 17

  8. What are the benefits? (cont.) • Consider this example: class CBook : public CDocument { CBook( char *szName, long lPagect ); void PrintName(); long m_lPagect; }; void CBook::PrintName() { cout << "Name of book: "; CDocument::PrintName(); } The name variable is defined by the base class (CDocument). To call the function that prints the name of CDocument, use the scope-resolution “::” operator. EECE 351 – Lecture 17

  9. How are pointers affected? • Pointers and references to derived classes can be implicitly converted to pointers and references to their base classes if there is an accessible, unambiguous base class. void main() // Start of main func. { CHuman *acHum[10]; // Array of ten humans. for( int i = 0; i < 10; ++i ) //Do for # in array. { //Prompt user. cout << "Type of person: " << "M)ale/F)emale" << endl; char chType; //Valid in for loop only. cin >> chType; //Get input. EECE 351 – Lecture 17

  10. How are pointers affected? (cont.) switch(tolower(chType))//tolower converts “A” to “a” { case 'm': //If user wants a man acHum[i] = new CMale;//Make one and put in array //TEST for new!!!! break; //That’s it for the guys case ’f': //If user wants a woman acHum[i] = new CFemale;//Same thing except class //TEST for new!!!! break;//Done for the ladies default: //Everything else --i;//Give user another chance } } for( i = 0; i < 10; ++i ) //Print each name in array cout << acHum[i]->GetName() << “, ” << flush; } EECE 351 – Lecture 17

  11. How are pointers affected? (cont.) • In the SWITCH statement in the preceding example, “male” and “female” objects are created, depending on which value the user specified for chType. Because these types are all derivedfrom the CHuman class, there is an implicit conversion to CHuman*. • Because the CHuman class has a GetName() function, it can print the name of each person in the array, although it may omit some of the information specific to the type of document (X and Y chromosomes, etc.) EECE 351 – Lecture 17

  12. Multiple Inheritance • Just like a child has two parents, so too can a C++ class. In fact, C++ classes can have any # of bases. • For each additional class, append “, <access level> <BaseName>” //Multiple parent example class CChild: public CMale, public CFemale { string m_szMom, m_szDad; //Private, right? }; EECE 351 – Lecture 17

  13. Multiple Inheritance (problems) • What ifboth base class and the derived class have a function/variable with thesamename? • AMBIGUITY (This is bad--will not compile!!!) • How to get around? • Use the scope resolution operator (::) • EX. CMale::XChromosone or CFemale::XChromosone EECE 351 – Lecture 17

  14. 3 Ps revisited Derive level Base EECE 351 – Lecture 17

  15. Virtual • What does it do? • How do you use it? • Code Demo • More tips • Pure functions EECE 351 – Lecture 17

  16. What does it do? • “Virtual functions” are functions that ensure that the correct function is called for an object, regardless of the expression used to make the function call. • Suppose a base class contains a function declared as virtual, and a derived class defines the same function. The function from the derived class is invoked for objects of the derived class, even if it is called using a pointer or reference to the base class. EECE 351 – Lecture 17

  17. How do you use it? • Place the keyword virtual before the function. • Checkbox on “Add Member Function...” Dialog Box • If a class is declared that does not provide an overriding implementation of the virtual function, the default implementation from the base class is used. • When calling a function using pointers or references, the following rules apply: • A call to a virtual function is resolved according to the underlying type of object for which it is called. • A call to a non-virtual function is resolved according to the type of the pointer or reference. EECE 351 – Lecture 17

  18. Virtual Demo #include <iostream.h> //cout. #ifndef __BASE_H //Only want to load interface once. #define __BASE_H //Keep from loading again. class CBase //No parents. { //Begin CBase Interface. public: //Everyone has access. virtualvoid NameOf(); //Virtual function example. void InvokingClass(); //Nonvirtual function example. }; //End of CBase Interface. #endif //__BASE_H EECE 351 – Lecture 17

  19. Virtual Demo (cont.) // Implement the two functions. void CBase::NameOf() //CBase’s virtual NameOf { //CBase’s implementation cout << ”CBase::NameOf” << flush; } void CBase::InvokingClass()//CBase’s non-virtual func { //CBase’s implementation cout << "Invoked by CBase ” << flush; } EECE 351 – Lecture 17

  20. Virtual Demo (cont.) // Declare a derived class. #ifndef __DRVD_H //Only want to load interface once. #define __DRVD_H //Keep from loading again. class CDrvd : public Base //CDrvd’s daddy is CBase. {//Begin CDrvd Interface. public: //Everyone has access. void NameOf(); //Orig. virtual func. void InvokingClass(); //Orig. Nonvirtual func. };//End CDrvd Interface. #endif //__DRVD_H EECE 351 – Lecture 17

  21. Virtual Demo (cont.) // Implement the two functions. void CDrvd::NameOf() { //CDrvd’s implementation cout << ”CDrvd::NameOf ” << flush; } void CDrvd::InvokingClass() { //CDrvd’s implementation cout << "Invoked by CDrvd” << flush; } EECE 351 – Lecture 17

  22. Virtual Demo (cont.) void main() { // Declare an object of type CDrvd. CDrvd cDrvd; // Declare two pointers, CDrvd* & CBase* //Initialize them to point to cDrvd. CDrvd *pcDrvd = &cDrvd; CBase *pcBase = &cDrvd; // Call the functions. pcBase->NameOf(); // Call virtual function. pcBase->InvokingClass(); // Call nonvirtualfunction. pcDrvd->NameOf(); // Call virtual function. pcDrvd->InvokingClass(); // Call nonvirtual function. } EECE 351 – Lecture 17

  23. Virtual Demo (cont.) The output from this program is: CDrvd::NameOf Invoked by CBase CDrvd::NameOf Invoked by CDrvd EECE 351 – Lecture 17

  24. More tips • You cannot declare global or static functions as virtual. • The virtual keyword can be used when declaring overriding functions in a derived class. • unnecessary • Virtual functions in a base class must be definedunless they are declared using the pure-specifier. (More later.) • The virtual function-call mechanism can be suppressed by explicitly qualifying the function name using “::” EECE 351 – Lecture 17

  25. Another place it appears CHuman CMale CFemale CChild • class CDrvd: virtualpublic CBase • What it does? • Only one sub-object of CBase. • Huh? • Ex: CHuman CHuman CMale CFemale CChild Instead of EECE 351 – Lecture 17

  26. Pure Functions • If some function is virtual and defined to 0, that function is PURE. • Any other number generates • error C2258: illegal pure syntax, must be '= 0' class CPure //Example of Pure class { //Begin of interface. public: //Everyone has access. virtual int Pure() = 0; //=0 makes it pure. CPure();//Constructor virtual ~CPure();//Destructor };//End of Interface. EECE 351 – Lecture 17

  27. Abstract Base Classes • What does pure do? • Abstract Base class • What is an Abstract Base class? • error C2259: 'CPure' : cannot instantiate abstract class • You will get this error for all derived classes unless one of the classes defines this function. EECE 351 – Lecture 17

  28. Abstract Base Classes (cont.) • Why? • Architect uses these (ABC) to build apps, while implementers do the work. (Think abstraction.) • Teachers use these in homework assignments ;) • Abstract classes act as expressions of general concepts from which more specific classes can be derived. You cannot create an object of an abstract class type; however, you can use pointers and references to abstract class types. EECE 351 – Lecture 17

  29. What we know • A lot (whew!!!) • Derived and Base classes • How to do it, What you get, Why • Pointers • Multiple parents (AMBIGUITY) • Virtual • Pointers • Pure • ABCs EECE 351 – Lecture 17

  30. Where we are going • Function overloading • Class Libraries (SEs have a lot of tools) • STL • Others we won’t get to • MFC • COM, DCOM, COM+ • Others trying to survive EECE 351 – Lecture 17

More Related