Solved

virtual function?

Posted on 1998-11-15
2
177 Views
Last Modified: 2013-11-15
for a project that I am developing, a friend told me to use 'pure virtual' functions in my classes. I am a beginner to C++ and hace no clue what she meant.
this project is due on Tuesday, so please answer fast!
0
Comment
Question by:dito
  • 2
2 Comments
 
LVL 8

Accepted Solution

by:
MaDdUCK earned 960 total points
Comment Utility
answer pending...
0
 
LVL 8

Expert Comment

by:MaDdUCK
Comment Utility
A member function declaration that turns a normal class into an abstract class (i.e., an ABC). You normally only implement it in a derived class.

Some member functions exist in concept; they don't have any reasonable definition. E.g., suppose I asked you to draw a Shape at location (x,y) that has size 7. You'd ask me "what kind of shape should I draw?" (circles, squares, hexagons, etc, are drawn differently). In C++, we must indicate the existence of the draw() member function (so users can call it when they have a Shape* or a Shape&), but we recognize it can (logically) be defined only in subclasses:

    class Shape {
    public:
      virtual void draw() const = 0;  // = 0 means it is "pure virtual"
      // ...
    };

This pure virtual function makes Shape an ABC. If you want, you can think of the "= 0;" syntax as if the code were at the NULL pointer. Thus Shape promises a service to its users, yet Shape isn't able to provide any code to fulfill that promise. This forces any actual object created from a [concrete] class derived from Shape to have the indicated member function, even though the base class doesn't have enough information to actually define it yet.

Note that it is possible to provide a definition for a pure virtual function, but this usually confuses novices and is best avoided until later.

hope this helps,
MaDdUCK

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Replication has always been one of those technologies that people run scared from. The main reason is usually cost. When you think of replication, your mind drifts to solutions that replicate from one disk frame to another using block level technolo…
VM backup deduplication is a method of reducing the amount of storage space needed to save VM backups. In most organizations, VMs contain many duplicate copies of data, such as VMs deployed from the same template, VMs with the same OS, or VMs that h…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now