0

My question is actual two fold. I am going through this tutorial https://www.youtube.com/watch?v=6y0bp-mnYU0 and at 1:07:45, he talks about using the override keyword when defining a virtual fucnction of an abstract class. I kept my classes declaration and definintion in different files. When I try to use override in my definition, it gives me the "'override' specifier illegal" on function definition on Visual Studio 2019. Why is this?

include "Circle.h" include "Shape.h"

double Circle::Area() override {    
    return 3.14159 * pow((width / 2), 2);
}

Also, what does this code snippet do? I am new to c++:

Circle::Circle(double width) : Shape(width) {

}  

Why is circle using a constructor from an abstract class? Is this even possible? What does the : Shape(width) do.

This is what "Shape" class looks like:

class Shape
{
protected:  //means that inherited classes will be able to access as long as it is part of protected
    double height;
    double width;
public:

    static int numofShapes; 

    Shape(double length);
    Shape(double height, double width);
    Shape();
    Shape(const Shape& orig); 
    virtual ~Shape();

//Setters and getters for privat mems
void Setheight(double height);
double Getheight();
void Setwidth(double height);
double Getwidth();

static int Getnumofshapes();
virtual double Area() = 0; //makes it an abstract base class

// private: only class code

edo101
  • 629
  • 6
  • 17
  • If the override is not needed since definition is outside of the declaration, how will the compiler know to properly override? – edo101 Apr 26 '20 at 05:29
  • `Shape` is not abstract (it doesn't have any pure virtual functions) and it also doesn't have a `virtual double Area()` function you could `override` – UnholySheep Apr 26 '20 at 14:37

1 Answers1

0
Circle::Circle(double width) : Shape(width) {

}  

This code snippet is the implementation of the constructor of Circle. The stuff behind the : is the initializer list, which is used to initialize the data members of your class.

class A {
    public:
        A() : val1(1), val2(2) {

        }

    private:
        int val1;    // is initialized to 1
        int val2;    // is initialized to 2
}

In the initializer list, you not only have to initialize the fields, but you also have to call the constructor of the base class (in this case Shape) when the base class doesn't have a default constructor (constructor with no arguments). Therefore, as first element in the initializer list you have the name of the base class, and in parenthesis the parameters for Shape's constructor.


You get an error that the override specifier is illegal, since you haven't declared a virtual function with the signature of your Area-function in the base class (Shape). See this stackoverflow post to learn how virtual functions work and for what they're used.

DasElias
  • 569
  • 8
  • 19
  • But I called Area virtual in my Shape class: virtual double Area() = 0. – edo101 Apr 26 '20 at 17:12
  • I have editted my original question to include the other functions. You will now see desigined Area() to be pure virtual function. Also the Shape class does have a default constructor. Its in the original question. So I have a default constructor, why would I need to inherit the base class's constructor? – edo101 Apr 26 '20 at 17:15
  • I have editted my original question to include the other functions. You will now see desigined Area() to be pure virtual function. Also the Shape class does have a default constructor. Its in the original question. So I have a default constructor, why would I need to inherit the base class's constructor @DasElias – edo101 Apr 27 '20 at 01:18