Inheritance - out of scope error

For some reason my syntax is wrong and I'm getting an error. I am writing code to learn inheritance, composition, etc. I am making a Shape base class, which will have Circle, Square, and Triangle derived classes.

I keep getting an "out of scope error" when I try to call the method I wrote in the Circle class.

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
using namespace std;
#include <iostream>
#include <conio.h>

class Shape {
	public:
	virtual double area(double radius) const = 0;

};
class Circle : public Shape {
	public:
	double radius;
	Circle() : radius(0) {};								
	Circle(double radius);									
	double area (double radius) const{
		double area;
		area = 3.1416*radius*radius;
		return area;
	}
};	
int main() 
{
	double r1 = 23;
	Circle circ;
        circ.radius = r1;
	area(circ);            // error here
}///:~ 


Any tips or help?
Hi,

Try this:

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
#include <iostream>

class Shape {
	public:
	virtual double area() const = 0;
	virtual ~Shape() = default;

};
class Circle : public Shape {
    private:
    
    double radius;
    
	public:
	
	Circle() : radius(0) {};								
	Circle(double radiusArg) : radius(radiusArg)	{}
	
	double area () const override {
		 
		double area = 3.1416 * radius * radius;
		return area;
	}
};	
int main() 
{
	double r1 = 23.0;
	Circle circ(r1);
        
	std::cout << "Area is " << circ.area() << "\n";    
}


You could review the tutorial here:
http://www.cplusplus.com/doc/tutorial/polymorphism/
Last edited on
Topic archived. No new replies allowed.