virtual function

hi, its me again =D...
about virtual function, all i know is:
1
2
3
4
5
6
7
8
9
10
11
12
13
class A{
 virtual void display(){ cout<<"AA"<<endl; }
};

class B:public A{
 virtual void display(){ cout<<"BB"<<endl; }
};

// main...
A *aa;
B bb;
aa=&bb;
aa->display();


The result will be BB rite?
if im correct then i know the logic but dunno the usefulness of it beside please explain pure virtual function also =DDD

Thanks for reading...

Yes. It's the type of object the pointer points too (class B), not the type of pointer (class A) that determines which method will be called.

If a class contains at least 1 pure virtual method the class become abstract, you cannot create instances of the class directly, only derived classes. Also the derived classes must provide their own overloaded versions of all the pure virtual methods.

Need to add public: for above to compile.

1
2
3
4
5
6
7
8
9
class A{
public:
 virtual void display(){ cout<<"AA"<<endl; }
};

class B:public A{
public:
 virtual void display(){ cout<<"BB"<<endl; }
};
Last edited on
Topic archived. No new replies allowed.