Everything is working fine and the program runs, the only issue is I keep getting 0 for the area when it does the calculation for rectangle. Can someone point me in the right direction on why that is?
//main.cpp
#include <iostream>
#include "Circle.h"
#include "Square.h"
#include "Shape.h"
#include "Rectangle.h"
usingnamespace std;
int main( )
{
double radius = 0;
double length = 0;
double width = 0;
Square square;
Circle circle;
Rectangle rectangle;
//When user puts in 1 this will ask for the radius of the circle then it will call the function for the calculation.
cout<<"Enter the radius of the circle : " ;
cin>> radius;
circle.setRadius(radius);
cout<< "\nArea of the circle is "<<circle.getArea();
//When user puts in 2 this will ask for the length of the side of the square then it will call the function for the calculation.
cout<< "\nEnter length of one side of the square : "<<endl;
cin >> length;
square.setLength(length);
cout<<"\nArea of the square is "<<square.getArea();
cout<< "\nEnter length of one side of the rectangle : "<<endl;
cin >> length;
square.setLength(length);
cout<< "\nEnter width of one side of the rectangle : "<<endl;
cin>> width;
rectangle.setWidth(width);
cout<<"\nArea of the rectangle is "<<rectangle.getArea();
}
//Circle.cpp
#include "Circle.h"
#include <iostream>
usingnamespace std;
// Makes the Calculation for the user and outputs it.
Circle::Circle (double r)
{
radius = r;
}
double Circle::getRadius()const {
return radius;
}
void Circle::setRadius (double r){
radius = r;
}
double Circle::getArea()const{
return radius * radius * 3.1415;
}
//Circle.h
#define CIRCLE_H_INCLUDED
#include <iostream>
#include "Shape.h"
usingnamespace std;
class Circle {
// Gets called and stores the input from user.
public:
Circle (double radius =0);
double getRadius()const;
void setRadius(double radius);
double getArea ()const;
private:
double radius;
};
//rectangle.cpp
#include "Rectangle.h"
#include <iostream>
usingnamespace std;
Rectangle::Rectangle(double len, double wid)
{
length = len;
width = wid;
}
double Rectangle::getLength() {
return length;
}
void Rectangle::setLength(double len){
length = len;
}
double Rectangle::getWidth() {
return width;
}
void Rectangle::setWidth(double wid){
width= wid;
}
double Rectangle::getArea(){
return length * width;
}
//Rectangle.h
#define RECTANGLE_H_INCLUDED
#include <iostream>
#include "Shape.h"
class Rectangle : public Shape
{
public:
Rectangle (double length =0, double width =0);
double getLength();
double getWidth();
void setLength(double length);
void setWidth(double width);
virtualdouble getArea ();
private:
double length;
double width;
};
//shape.cpp
#include "shape.h"
Shape::Shape(){
area = 0;
}
double Shape::getArea(){
return area;
}
//Shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include <string>
class Shape{ // Base Class
public:
Shape();
double getArea();
private:
double area;
};
#endif
//square.cpp
#include "Square.h"
#include <iostream>
usingnamespace std;
// Makes the Calculation for the user and outputs it.
Square::Square(double len)
{
length = len;
}
double Square::getLength()const {
return length;
}
void Square::setLength (double len){
length = len;
}
double Square::getArea()const{
return length * length;
}
//square.h
#define SQUARE_H
#include <iostream>
#include "Shape.h"
usingnamespace std;
// Gets called and stores the input from user.
class Square {
public:
Square (double length =0);
double getLength()const;
void setLength(double length);
double getArea ()const;
private:
double length;
};
Thirty seconds to type and then you would have known for sure what was happening. If you're getting a value in your code and you don't know why, find where it comes from and start logging out the variables that make it until you see the mistake.
Your indentation could use some work. I suggest always using spaces instead of tabs (you should be able to set your editor to insert spaces instead of tabs for indentation. Four spaces is average these days.
The base class Shape is not being used properly. The proper use of the base class in this situation is to define the abstract interface of the Shape objects (with virtual functions), Something like this: