The point of the program I am making is to have a pure abstract base class called BasicShape and have 2 child classes, Circle and Rectangle. I have the program written but with one unresolved error.


Main.cpp file

#include "Pure.h"
#include <iostream>
using namespace std;


int main() {
	
	Circle pizza(2,2,10);
	Rectangle box(10,5);
	
	cout << "The Circle's information: " << endl;
	cout << "Center x = " << pizza.getCenterX() << endl;
	cout << "Center y = " << pizza.getCenterY() << endl;
	cout << "Area = " << pizza.getArea() << endl << endl << endl;

	cout << "The Rectangle's information: " << endl;
	cout << "Length = " << box.getLength() << endl;
	cout << "Width = " << box.getWidth() << endl;
	cout << "Area = " << box.getArea() << endl;
}

.H file

#ifndef PURE_H
#define PURE_H


class BasicShape {
protected: 
	double area;
public:
	double getArea() const;
	virtual void calcArea() ; //used to make BasicShape a pure abstract base class

};

class Circle : public BasicShape {
private:
	long centerX;
	long centerY;
	double radius;
public:
	//constructor
	Circle(long, long, double);
	//accessors
	long getCenterX() const;
	long getCenterY() const;
	//mutator
	virtual void calcArea() ;

};

class Rectangle : public BasicShape {
private:
	long width;
	long length;
public:
	//constructor
	Rectangle(long,long);
	//accessors
	long getWidth() const;
	long getLength() const;
	//mutator
	virtual void calcArea() ;


};

#endif

.cpp file

#include "Pure.h"


double BasicShape :: getArea() const {
	return area;
}

Circle::Circle(long x, long y,double r) {
	centerX = x;
	centerY = y;
	radius = r;
	calcArea();
}

long Circle::getCenterX() const {
	return centerX;
}

long Circle::getCenterY() const {
	return centerY;
}

void Circle::calcArea()  {
	area = 3.14159 * radius * radius;
	
}

Rectangle::Rectangle(long L, long W) {
	length = L;
	width = W;
	calcArea();
}

long Rectangle::getWidth() const {
	return width;
}

long Rectangle::getLength() const {
	return length;
}

void Rectangle::calcArea()  {
	area = length * width;
	
}

The error I get is this: Error 1 error LNK2001: unresolved external symbol "public: virtual void __thiscall BasicShape::calcArea(void)" (?calcArea@BasicShape@@UAEXXZ)
Any help will be much appreciated. Thanks in advance.

You should make calcArea a pure virual funtion, by defining it as:

virtual void calcArea() = 0;

Right now the compiler thinks that this function is unimplemented. The calcArea in the derived shapes don't necessarily need to be virual.

This article has been dead for over six months. Start a new discussion instead.