0% found this document useful (0 votes)
121 views16 pages

Abstract & Virtual Classes in C++

An abstract class is a base class that contains at least one pure virtual function. A pure virtual function is defined using the "= 0" syntax. An abstract class cannot be instantiated but can be used as a base class for derived classes to inherit from. Virtual functions allow derived classes to override the implementation of functions in the base class. The correct overridden function is called at runtime based on the actual object type, rather than the type of the pointer variable.

Uploaded by

kumararguru
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views16 pages

Abstract & Virtual Classes in C++

An abstract class is a base class that contains at least one pure virtual function. A pure virtual function is defined using the "= 0" syntax. An abstract class cannot be instantiated but can be used as a base class for derived classes to inherit from. Virtual functions allow derived classes to override the implementation of functions in the base class. The correct overridden function is called at runtime based on the actual object type, rather than the type of the pointer variable.

Uploaded by

kumararguru
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

ABSTRACT CLASSES

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration. The following is an example of an abstract class: class AB { public: virtual void f() = 0; }; Function AB::f is a pure virtual function.

You cannot use an abstract class as a parameter type, a function return type, as the type of an explicit conversion, declare an object of an abstract class. You can, declare pointers references to an abstract class. The following example demonstrates this: class A { virtual void f() = 0; };
class B : A { virtual void f() { } };

// Error: // Class A is an abstract class // A g();


// Error: // Class A is an abstract class // void h(A); int main() { // Error: // Class A is an abstract class // A a; A* pa; B b; }

VIRTUAL BASE CLASS

Student
Grand Parent

Test
Parent1 Parent2

Sports

Result
Child

#include< iostream.h> #include< conio.h>


class student { private : int rn; char na[20]; public: void getdata() { cout< < "Enter Name And Roll No : "; cin>>na>>rn; } void putdata() { cout< < endl< < na< < "\t"< < rn< < "\t"; } };

class test : virtual public student { protected: float m1,m2; public: void gettest() { cout< < \nEnter your marks In CP 1 &Cp 2 :"; cin>>m1>>m2; } void puttest() { cout< < m1< < "\t"< < m2< < "\t"; } };

class sports : virtual public student { protected: float score; public: void getscore() { cout< < endl< < "Enter your score :"; cin>>score; } void putscore() { cout< < score< < "\t"; } }; class results : public test , public sports { private : float total; public : void putresult() { total = m1+m2+score; cout< < total; } };

void main() { results s; clrscr(); s.getdata();

s.gettest();
s.getscore(); s.putdata();

s.puttest();
s.putscore(); s.putresult(); getch(); }

VIRTUAL FUNCTIONS
1. We use the same function name in both the base and derived classes, the function in the base class is declared as Virtual using the keyword

Virtual preceding its normal declaration.


2. When a function is made Virtual, C++ determines which function to use at the run time based on the type of object pointed to by the base

pointer, rather than the type of the pointer.


3. Thus by making the base pointer to different objects, we can execute different versions of the Virtual function. 4. Definition of the derived function overrides the definition of the base class version 5. Determination of which virtual function to use cannot always be made at compile time.

#include <iostream.h> class base { public: void diplay() { cout<<\n Display base; } virtual void show() { cout<<\n show base;}; class derived : public base { public: void diplay() { cout<<\n Display base; void show() { cout<<\n show base;

};

Void main() { base b;


derived d;

base *bptr;
cout<<\n bptr points to base\n; bptr=&b; bptr->display(); bptr->show(); cout<<\n bptr points to Derived\n;

bptr=&d;
bptr->display(); bptr->show();

#include<iostream.h>
Class media { Protected: char title[50]; float price; Public: media(char *s,float a) { strcpy(title,s); price=a; } virtual void display(){}

};

Class book: public media { Int pages; Public: book(char *s,float a,int p):media(s,a) { pages=p; } void display(); }; Class tape: public media { float time; Public: book(char *s,float a,float t):media(s,a) { time=t; } void display(); };

Void book::display() { Cout<<Title<<title; Cout<<pages<<pages; Cout<<price<<price; } Void tape::display() { Cout<<Title<<title; Cout<<play time<<time; Cout<<price<<price; } Void main() { Char *title=new char[30]; Float price,time; Int pages;

Cout<<enter book details; Cout<<Title; cin>>title; Cout<<pages; cin>>pages; Cout<<price; cin>>price; Book book1(title,price,pages);

Cout<<enter tape details; Cout<<Title; cin>>title; Cout<<play time; cin>>time; Cout<<price; cin>>price; Tape tape1(title,price,time); Media *list[2]; list[0]=&book1; list[1]=&tape1; Cout<<media details ; Cout<<BOOK; list[0]->display(); Cout<<TAPE; list[0]->display(); }

RULE FOR VIRTUAL FUNCTION

1. The virtual function must be members of some class.


2. They cannot be static members. 3. They are accessed by using object pointers.

4. A virtual function can be a friend function


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 can have virtual destructors. 8. We cannot use a pointer of derived class to access an object of the base type. 9. If a virtual function is defined in the base class, it need not be necessarily refined in the derived class. In such cases, calls invoke the base function itself.

You might also like