polymorphism c++


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  class Butterflyfish
{
protected:
    string Btype;
public:
    virtual string type()
    {
       	return Btype;
    }
    
    void display()
    {
        cout<<"Fish type= "<<Btype<<endl;
    }
};
class Forcipiger :public Butterflyfish{
private:
    string Ftype;
public:
    virtual string type()
    {
       	return Ftype;
    }
};


int  main()
{
     Forcipiger f;
     Butterflyfish *b[3];
     b[0]=&f;
     b[0]->display();
    return 0;}


Q1: to print every derived type, what's better
1) copy display function and make it virtual
2) replace "attribute name" with "virtual type function name" in Base Class
as
1
2
3
4
void display()
    {
        cout<<"Fish type= "<<type()<<endl;
    }
Last edited on
it is useful, but I'm not asking about type specifiers!
Thank you
What type specifiers?
public, protected and private
Look at Guideline #2. What does it imply?
So you mean
2) replace "attribute name" with "virtual type function name" in Base Class
then make virtual function private?
because I tried it when it is public and it works good
Good.
Topic archived. No new replies allowed.