I am having an issue with my inheritance problem that I need help with. Everything is working except for my rectangle solution. Every time i biuld and run I get these three errors.
Error: no 'double Rectangle::getLength() member function declared in class 'Rectangles'
And certain other problems with the rectangle, getWidth() and getWidth(double). I was hoping you all could help me with this problem and also tell me if I am using inheritance right. Below is my code sorry if it is a little long. I also put my code that I am needing help with at the bottom of the list. If anyone could help me I would greatly appreciate it. Thank You.
main.cpp
#include <iostream>
#include "Circle.h"
#include "Square.h"
#include "Rectangle.h"
usingnamespace std;
int main()
{
double radius = 0;
double length = 0;
double width = 0;
Square square;
Circle circle;
Rectangle rectangle;
int option;
cout << "Calculating the Area" << endl << endl;
do
{
cout << "Pick a shape in which you would like the area of:" << endl;
cout << "1: Square" << endl;
cout << "2: Circle" << endl;
cout << "3: Rectangle" << endl;
cout << "4: Exit" << endl;
cout << "Please enter your choice: ";
cin >> option;
switch(option)
{
case 1:
{
cout << endl;
cout << "Please enter the length of one side of the square: " << endl;
cin >> length;
Square square(length);
cout << "The area of the square is: " << square.getArea() << "\n\n";
break;
}
case 2:
{
cout << endl;
cout << "Please enter the radius of the circle: ";
cin >> radius;
circle.setRadius(radius);
cout << "The area of the circle is: " << circle.getArea() << "\n\n";
break;
}
case 3:
{
cout << endl;
cout << "Please enter the length of one side of the rectangle: ";
cin >> length;
rectangle.setLength(length);
cout << "Please enter the width of one side of the rectangle: ";
cin >> width;
rectangle.setWidth(width);
cout << "The area of the rectangle is: " << rectangle.getArea();
}
}
}
while (option != 4);
cout << "Bye!" << endl;
}
which is a function of the same name that isn't declared in the class but is scoped to it. So its several errors all at once, but the fix is to turn those 2 variables into function prototypes:
If you want functions in your inherited classes to work differently than the functions in tha base class, remember to use the keyword virtual. This means that the declaration of the getArea function in your Shape class is not correct. What you should do instead is:
1 2 3 4 5
class Shape
{
public:
virtualdouble getArea(); //This way, the getArea function in your derived classes overrides the implementation in your Shape class
}
Something I just noticed: you aren't inheriting anything at all. This is how inheritance works:
1 2 3 4 5 6 7 8 9 10 11
class Base
{
public:
void doSomething() { std::cout << "I am a base!\n"; }
}
class Derived : public Base //This tells the program that Derived inherits from Base, and public means it can use all non-protected members of Base
{
public:
void doSomething() { std::cout << "I am a derived\n!"; }
}
This creates two classes, a Base class and a Derived class.
If you have any more questions on inheritance, I recommend you to look at this page:
#include <iostream>
#include "Circle.h"
#include "Square.h"
#include "Rectangle.h"
usingnamespace std;
int main()
{
cout << "Calculating the Area\n\n";
int option = 0;
do
{
cout << "Pick a shape in which you would like the area of:""\n1: Square""\n2: Circle""\n3: Rectangle""\n4: Exit""\nPlease enter your choice: ";
cin >> option;
switch(option)
{
case 1:
{
cout << "\nPlease enter the length of one side of the square: ";
double length = 0;
cin >> length;
Square square(length);
cout << "The area of the square is: " << square.getArea() << "\n\n";
break;
}
case 2:
{
cout << "\nPlease enter the radius of the circle: ";
double radius = 0;
cin >> radius;
Circle circle;
circle.setRadius(radius);
cout << "The area of the circle is: " << circle.getArea() << "\n\n";
break;
}
case 3:
{
cout << "\nPlease enter the length of one side of the rectangle: ";
double length = 0;
cin >> length;
Rectangle rectangle;
rectangle.setLength(length);
cout << "Please enter the width of the other side of the rectangle: ";
double width = 0;
cin >> width;
rectangle.setWidth(width);
cout << "The area of the rectangle is: " << rectangle.getArea() << "\n\n";;
}
}
}
while (option != 4);
cout << "Bye!\n";
}
Thank you to goldenchicken and Enoizat for their very good looks into inheritance. I would also like to thank everybody for their support in this assignment, so thank you guys very much and I hope to hear from you guys again.