Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.

Similar presentations


Presentation on theme: "Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment."— Presentation transcript:

1 Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment and copy initialization, the this pointer. Dynamic type information.

2 Pointers to Derived Classes Pointers to objects of a base class are type compatible with pointers to objects of a derived class. Example - If B is a base class and D is a derived class from B then a pointer declared as a pointer to B can also be a pointer to D. B *bptr; B b; D d; bptr = &b; We can make bptr to point to D also - bptr = &d; This s a perfectly valid argument in C++, as d is an object derived from the class B

3 #include using namespace std; class BaseClass { int x; public: void setx(int i) { x = i; } int getx() { return x; } }; class DerivedClass : public BaseClass { int y; public: void sety(int i) { y = i; } int gety() { return y; } }; int main() { BaseClass *p; BaseClass baseObject; DerivedClass derivedObject; p = &baseObject; p->setx(10); cout getx(); p = &derivedObject; derivedObject.sety(88); cout << derivedObject.gety(); p->setx(99); cout getx(); return 0; }

4

5 Virtual Functions A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs. Base class pointer can point to derived class object. In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked. But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual functions support runtime polymorphism.

6 #include using namespace std; class B { public: virtual void display() { cout<<"Content of base class"; } }; class D1 : public B { public: void display() { cout<<"Content of first derived class"; } } ; class D2 : public B { public: void display() { cout<<"Content of second derived class"; } }; int main() { B *b; D1 d1; D2 d2; b = &d1; b->display(); b = &d2; b->display(); return 0; }

7 Runtime Polymorphism C++ allows binding to be delayed till run time. When you have a function with same name, equal number of arguments and same data type in same sequence in base class as well derived class and a function call of form: base_class_type_ptr->member_function(args); will always call base class member function. The keyword virtual on a member function in base class indicates to the compiler to delay the binding till run time. Every class with atleast one virtual function has a vtable that helps in binding at run time.vtable Looking at the content of base class type pointer it will correctly call the member function of one of possible derived / base class member function.

8 Rules For Virtual Functions 1.The virtual functions must be members of some class. 2.They cannot be static member. 3.They are accessed by using object pointers. 4.A virtual function can be a friend of another class. 5.A virtual function in a base class must be defined, even though it may not be used. 6.The prototypes of the base class version of a virtual function and all the derived class versions must be identical. 7.We cannot have virtual constructors, but we can have virtual destructors. 8.While a base pointer can point to any type of the derived objects, the reverse is not true. 9.If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases, calls will invoke the base function.

9 Pure Virtual Functions A Pure virtual function or Pure virtual method is a virtual function that is required to be implemented by a derived class. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly. A subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class.subclass Pure virtual methods typically have a declaration (signature) and no definition (implementation).declaration

10 Abstract Classes An abstract class is a class that cannot be instantiated (object is not created). An abstract class is usually implemented as a class that has one or more pure virtual functions. Characteristics of Abstract Classes – 1.Abstract class cannot be instantiated,but pointers & references of abstract class type can be created. 2.Abstract class can have normal functions & variables along with a pure virtual function. 3.Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface. 4.Classes inheriting an abstract class must implement all pure virtual functions, or else they will become abstract too.

11 Virtual Base Classes When two or more classes are derived from a common base class, we can prevent multiple copies of the base class being present in an object, derived from those objects, by declaring the base class as virtual. Such a base class is known as virtual base class. This can be achieved by preceding the base class name with the word virtual.

12 class A { public: int i; }; class B : virtual public A { public: int j; }; class C: virtual public A { public: int k; }; class D: public B, public C { public: int sum; }; int main() { D ob; ob.i = 10; ob.j = 20; ob.k = 30; ob.sum = ob.i + ob.j + ob.k; cout << “Value of i is : ”<< ob.i; cout << “Value of j is : ”<< ob.j; cout << “Value of k is :”<< ob.k; cout << “Sum is : ”<< ob.sum; return 0; }

13 Static Functions We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

14 #include using namespace std; class gamma { private: static int total; int id; public: gamma() { total++; id = total; } ~gamma() { total--; cout << id; } int main() { gamma g1; gamma::showtotal(); gamma g2, g3; gamma::showtotal(); g1.showid(); g2.showid(); g3.showid(); return 0; } static void showtotal() { cout <<total; } void showid() { cout << id; } }; int gamma::total = 0;

15 This Pointer The ‘this’ pointer is passed as a hidden argument to all non- static member function calls and is available as a local variable within the body of all non-static functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).

16 #include class max { int a; public: void getdata() { cout<<"Enter the Value :"; cin>>a; } max &greater(max &x) { if(x.a>=a) return x; else return *this; } void display() { cout<<a; } }; main() { max one,two,three; one.getdata(); two.getdata(); three=one.greater(two); three.display(); getch(); }


Download ppt "Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment."

Similar presentations


Ads by Google