Ryan's District
November 22, 2008, 03:52:04 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Ryan's District Lottery: Claim your ticket or check
Jackpot details  
 
   Home   Help Search Chat Calendar Chess Shop Login Register  
Digg This!
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: c++ tips - Pure virtual member func, printing (FAQ 73 & 74)  (Read 2659 times)
0 Members and 2 Guests are viewing this topic.
ben2ong2
Quality Poster
Paid
Hero Member
*****

Reputation: 17
Offline Offline

Gender: Male
Posts: 2374
9976.80 RD$

View Inventory
Send Money to ben2ong2

View Profile Awards
« on: October 07, 2006, 08:47:31 AM »

Q73: What is a `pure virtual' member function?

A: Some member functions exist in concept, but can't have any actual defn.  Ex:
Suppose I asked you to draw a Shape at location (x,y) that has size 7.2.  You'd
ask me `what kind of shape should I draw', since circles, squares, hexagons,
etc, are drawn differently.  In C++, we indicate the existence of the `draw()'
method, but we recognize it can only be defined in subclasses:

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

This pure virtual makes `Shape' an ABC.  The `const' says that invoking the
`draw()' method won't change the Shape object (ie: it won't move around on the
screen, change sizes, etc).  If you want, you can think of it as if the code
were at the NULL pointer.

Pure virtuals allow you to express the idea that any actual object created from
a [concrete] class derived from the ABC *will* have the indicated member fn,
but we simply don't have enough information to actually *define* it yet.  They
allow separation of interface from implementation, which ultimately allows
functionally equivalent subclasses to be produced that can `compete' in a free
market sense (a technical version of `market driven economics').


Q74: How can I provide printing for an entire hierarchy rooted at `class X'?

A: Provide a friend operator<< that calls a protected virtual function:

    class X {
    public:
      friend ostream& operator<< (ostream& o,const X& x)
        { x.print(o); return o; }
      //...
    protected:
      virtual void print(ostream& o) const;  //or `=0;' if `X' is abstract
    };

Now all subclasses of X merely provide their own `print(ostream&)const' member
function, and they all share the common `<<' operator.  Friends don't bind
dynamically, but this technique makes them *act* as if they were.


Logged

You are not allowed to view links.
Register or Login
Free Paid Survey & Home Business Resources.

You are not allowed to view links.
Register or Login
Free Article Directory | Quality Content
Ryan's District
« on: October 07, 2006, 08:47:31 AM »

 Logged
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

Archive - WAP2 - WAP - imode
Sponsors: RAYAN.tv
-

Powered by MySQL Powered by PHP Powered by SMF 1.1.7 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.263 seconds with 31 queries.

Google visited last this page Today at 06:43:38 AM