answersLogoWhite

0


Best Answer

A pure-virtual method is similar to a virtual method. A virtual method is a method that is expected to be overridden in a derived class while a pure-virtual method must be overridden in a derived class. The base class may provide a default implementation for a pure-virtual method, but it is not a requirement, whereas it must provide an implementation for a virtual method. In addition,

any base class that has a pure-virtual method becomes abstract; you cannot instantiate abstract classes -- you are expectedto derive from them.

#include

class Abstract

{

public:

// Pure-virtual method; note the '=0' to denote pure-virtual.

virtual void Show()=0;

};

class Derived : public Abstract

{

public:

virtual void Show()

{

// Implementation is required here!

std:: cout << "Derived" << std::end;

}

};

void main()

{

// Abstract a; // Compiler error!

Derived d;

d.Show(); // Call pure-virtual method.

return( 0 );

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

The following program demonstrates pure virtual functions.

#include<iostream>

#include<list>

class shape

{

public:

// pure-virtual

virtual void draw() = 0 { std::cout<<"Drawing..."; }

};

class circle: public shape

{

public:

void draw(){ shape::draw(); std::cout<<"circle"<<std::endl; }

};

class square: public shape

{

public:

void draw(){ shape::draw(); std::cout<<"square"<<std::endl; }

};

void process_shapes(std::list<shape*>& shapes)

{

for(std::list<shape*>::iterator i=shapes.begin(); i!= shapes.end(); ++i )

{

shape& sh=*(*i);

sh.draw();

}

}

int main()

{

circle c;

square s;

std::list<shape*> shapes;

shapes.push_back( &c );

shapes.push_back( &s );

process_shapes( shapes );

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a Pure Virtual Function Write a C plus plus program using Pure Virtual Function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is meant by extension?

extensible means to enhance the program with new capabilities. In c++, extensibility can be achieved by using virtual function. now first we discuss what is virtual function in c++. " C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class." Through this u can be able to promote extensibility in your program by using Virtual function. Very simple concept.


Write a program that read phrase and print the number of lower-case letter in it using function of counting?

write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program


Write a c program for matrix addition using function?

#include&lt;


Can you write a program without using any semicolon inside the main function?

not possible dude


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


Write a program to calculate the area of a cylinder using a function?

give an example of calculation of mathematics


How do you write a c program to find area of rectangle using function?

There will be a function in it like this: double RectangleArea (double a, double b) { return a*b; }


How do you write a program using function that counts the number of vowels in a string in java language?

Use text-editor notepad++


Write a program to swap two numbers using function?

swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }


How do you write a programm in c plus plus without using function main?

I don't think its possible. Every C++ program must at least have the main function.


How do you write a program using the gotoxy statement and print function to display letters of the alphabet on the computer screen?

There is no gotoxy statement in C.


Write a sample program using ASPNET explaining all the syntax and semantics of the program?

write a sample program using asp.net explaining all the syntax and semantics of the program