

























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Virtual Functions Virtual means existing in appearance but not in reality. When virtual functions are used, a program that appears to be calling a function of one class may be called a part of a different course. Why are virtual functions needed?
Typology: Slides
1 / 65
This page cannot be seen from the preview
Don't miss anything!


























































1
Normal Member Functions Accessed with Pointers (1/2) // normal functions accessed from pointer #include
Normal Member Functions Accessed with Pointers (1/2) int main() { Derv1 dv1; // object of derived class 1 Derv2 dv2; // object of derived class 2 Base* ptr; // pointer to base class ptr = &dv1; // put address of dv1 in pointer // The rule is that pointers to objects of a derived // class are type compatible with pointers to objects of // the base class. ptr->show(); // execute show() OR (*ptr).show(); // the function in the base class is always executed. ptr = &dv2; // put address of dv2 in pointer ptr->show(); // execute show() return 0; } 5
Virtual Member Functions Accessed with Pointers (1/2) // virtual functions accessed from pointer #include
Virtual Member Functions Accessed with Pointers (2/2) int main() { Derv1 dv1; // object of derived class 1 Derv2 dv2; // object of derived class 2 Base* ptr; // pointer to base class ptr = &dv1; // put address of dv1 in pointer ptr->show(); // execute show() of Derv // The rule is that the compiler selects the function // based on the contents of the pointer ptr, not on the // type of the pointer ptr = &dv2; // put address of dv2 in pointer ptr->show(); // execute show() of Derv return 0; }
11 Abstract Classes and Pure Virtual Functions (^) Think of the shape class in the multshap program in Chapter 9. We’ll never make an object of the shape class; we’ll only make specific shapes such as circles and triangles. When we will never want to instantiate objects of a base class, we call it an abstract class_. Such a class exists_ only to act as a parent of derived classes that will be used to instantiate objects. (^) If we don’t want anyone to instantiate objects of the base class, we’ll write our classes so that such instantiation is impossible. How can we can do that? By placing at least one pure virtual function in the base class. A pure virtual function is one with the expression = added to the declaration. This is shown in the next
Abstract Classes and Pure Virtual Functions (1/2) int main() { // Base bad; // can’t make object from abstract class Base* arr[2]; // array of pointers to base class Derv1 dv1; // object of derived class 1 Derv2 dv2; // object of derived class 2 arr[0] = &dv1; // put address of dv1 in array arr[1] = &dv2; // put address of dv2 in array arr[0]->show(); // execute show() in both objects arr[1]->show(); return 0; }
Abstract Classes and Pure Virtual Functions (^) Once you’ve placed a pure virtual function in the base class, you must override it in all the derived classes from which you want to instantiate objects. If a class doesn’t override the pure virtual function, it becomes an abstract class itself, and you can’t instantiate objects from it (although you might from classes derived from it). For consistency, you may want to make all the virtual functions in the base class pure.
Virtual Functions and the person Class (2/3) } bool isOutstanding(){ return (gpa > 3.5)? true : false; } }; class professor : public person { // professor class private: int numPubs; // number of papers published public: void getData() { //get professor data from user person::getName(); cout << " Enter number of professor’s publications: "; cin >> numPubs; } bool isOutstanding() { return (numPubs > 100)? true : false; } };
Virtual Functions and the person Class (1/3) int main(){ person* persPtr[100]; // array of pointers to persons int n = 0; char choice; // number of persons on list do { cout << "Enter student or professor (s/p): "; cin >> choice; if(choice=='s') persPtr[n] = new student; else persPtr[n] = new professor; persPtr[n++]->getData(); //get data for person cout << " Enter another (y/n)? "; cin >> choice; } while( choice=='y' ); //cycle until not ‘y’ for(int j=0; j<n; j++){ //print names of all persPtr[j]->putName(); // say if outstanding if( persPtr[j]->isOutstanding() ) cout << " This person is outstanding\n"; } return 0; } //end main()
Virtual Base Classes #include