Exam Revision

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
 class Shape {
  protected:
   Shape();
  public:
   int getShapeNumber() const;
  private:
   int shapeNumber;
   static int nextNum;
};
int Shape::nextNum = 0;

class Circle : public Shape {
 public:
  Circle(double r);
  double getRadius() const;
 private:
  double radius;
};

class Square : public Shape {
 public:
  Square(double s);
  double getSize() const;
 private:
  double size;
};


The implementations of the Shape and Circle constructors are as follows:
1
2
Shape::Shape() : shapeNumber(++nextNum) {}
Circle::Circle(double r) : Shape(), radius(r) {}


What is the effect of making the constructor of the Shape Class Protected ?
What is the effect of not making getShapeNumber() virtual in the Shape class?
Last edited on
Making the constructor protected, means that this class cannot be instantiated unless by a friend class, therefore Shape Class is essentially an abstract class.

Virtual Functions, enables run time polymorphism. If getShapeNumber() is not virtual, then the derived(Circle and Square) classes do not have to provide a definition for this function.

These are my answers, which I am not to sure are correct.

Topic archived. No new replies allowed.