class and pointer

how to declare pointer for the derived class Perimeter in main and give an output?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <iostream>
 
using namespace std;


class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
     void print()
      {
          int m;
          m= width*height;
          cout<"Area : "<m<<endl;
      }
   protected:
      int width;
      int height;
};

class Perimeter:public Shape
{
      public:
      void print()
      {
           int n;
           n=2*width + 2*height;
           cout<<"Parameter :"<<n<<endl;
      }   
}



int main()
{
    Shape *p;
Shape q;
p= &q;
p->setWidth(5);
p->setHeight(4);
p->print();


    system("pause");
    return 0;
    }
i wrote an answer to similar question few days ago:
http://www.cplusplus.com/forum/beginner/116001/#msg633204
Last edited on
Topic archived. No new replies allowed.