Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.

Similar presentations


Presentation on theme: "Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas."— Presentation transcript:

1 Topic 1 11/18/2015

2 Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas Khan : Roll No: 6743 2 11/18/2015

3 Virtual Function: 3 11/18/2015 A type of function that appears to exist in some part of a program but does not exist really is called virtual function. A virtual function is a member function that is declared within a base class (Parent Class) and redefined by a derived class (Child Class). Virtual functions are used to implement polymorphism. They enable the user to execute completely different function by the same function call. To create virtual function, precede the function’s declaration in the base class with the keyword virtual.

4 Example: #include Class A { int a; public: A( ) { a = 1; } virtual void show( ) { cout <<a; } }; Class B: public A { int b; public: B( ) { b = 2; } virtual void show() { cout <<b; } }; int main() { A *pA; B oB; pA = &oB; pA->show(); return 0; } Since pA points to object of B and show( ) is virtual in base class A. Output =2 4 11/18/2015

5 Pure Virtual Function: A type of virtual function that has no body is known as pure virtual function. A pure virtual function that has the notation "= 0" in the declaration of that function. 5 11/18/2015

6 Example: Simple example of a pure virtual function in C++ Class someclass { public: virtual void pure_virtual ( ) = 0; / / a pure virtual function / / note that there is no function body }; 6 11/18/2015

7 When should pure virtual functions be used in C++? In C++, a regular, "non-pure" virtual function provides a definition, which means that the class in which that virtual function is defined does not need to be declared abstract. The pure virtual functions are used in the classes which are not used directly. The user can inherit the class and then override the pure virtual function in the child class. 7 11/18/2015

8 Abstract classes: A type of class that contains any pure virtual function is called abstract class. An abstract class cannot be used direclty. It means that no object of an abstract class can be created. However, a child class can inherit an abstract class and use it by overriding its pure virtual function. It is also defined by using the keyword “virtual”. 8 11/18/2015

9 Characteristics of Abstract Class: Abstract classes are used to create a model class. Abstract class can have normal functions and variables along with a pure virtual function. Abstract classes are mainly used for Up casting, so that its derived classes can use its interface. 9 11/18/2015

10 Example: class Base //Abstract base class{ public: virtual void show() = 0; //Pure Virtual Function}; class Derived: public Base{ public: void show() { cout << "Implementation of Virtual Function in Derived class"; }}; int main(){ Base obj; //Compile Time Error Base *b; Derived d; b = &d; b->show( );} 10 11/18/2015

11 Virtual Base Classes A class that is inherited by child classes it is known as virtual base class. The virtual base classes are necessary in a situation where the member of base class is duplicated in multilevel inheritance. This can be achieved by preceding the base class’ name with the word virtual. 11 11/18/2015

12 Example: #include Class Parent { protected: int n; }; class Child 1: public Parent { }; class Child 2 : public Parent { }; class Baby : public Child 1, public Child 2 { public : void set ( ) { n = 5; cout <<”n=”<<n<<endl’ } }; void main ( ) { Baby obj; Obj.set ( ); getch ( ); } Output is: “5” 12 11/18/2015

13 Flow chart about above program is: 13 11/18/2015

14 14 11/18/2015


Download ppt "Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas."

Similar presentations


Ads by Google