Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function.
Define a class called Circle. It should be a derived class of the BasicShape class. This class will have 3 private data members. It will have 2 long integer data members called centerX and centerY. The last data member is a double called radius. It will have a constructor that accepts values for centerX, centerY and radius. This should call the overridden calcArea function of this class. This class defines its version of the calcArea function which determines the area of the circle using the formula area = 3.14159 * radius * radius. The class will also provide two functions called getCenterX and getCenterY which return the correct values.
Define a class called Rectangle. It should be a derived class of the BasicShape class. This class will have 2 private data members called width and length. Both data members should be long integers. Its constructor will have parameters for both the width and length. It will also override the calcArea function. For this class the calcArea function will use the formula area = length * width. It will provide member function called getWidth and getLength which should return the correct values.
In main create instances of the Circle and Rectangle classes. It should then display the area of the two shapes using a function defined as
void DisplayArea(BasicShape* shape)
What I have in bold is my main problem.. I did almost everything but i'm lost here.. references throw me off.
//***********************************************
// Pure Abstract Base
Class *
// This program defines a Circle object and
a *
// Rectangle object and calculates the area of *
//
each. *
//***********************************************
#include "Circle.h"
#include "Rectangle.h"
#include <iostream>
using namespace std;
// Function prototype
void displayArea();
void main()
{
displayArea();
}
void displayMessage()
{
// Create a Circle Object
Circle circle(5, 5, 10);
Rectangle rec(5,5);
cout << "Circle Output" << endl;
cout << "_____________" << endl;
cout << "Center x: " << circle.getCenterX() <<
endl;
cout << "Center y: " << circle.getCenterY() <<
endl;
cout << "Area: " << circle.calcArea() << endl
<< endl;
cout << "Rectangle Output" << endl;
cout << "________________" << endl;
cout << "Length: " << rec.getLength() <<
endl;
cout << "Width: " << rec.getWidth() <<
endl;
cout << "Area: " << rec.calcArea() << endl;
}
#ifndef BASICSHAPE_H
#define BASICSHAPE_H
using namespace std;
class BasicShape
{
protected:
double area; // To hold area
public:
// Accessor function to return area
double getArea() const
// Pure virtual function
virtual double calcArea() const = 0;
};
#endif
#ifndef CIRCLE_H
#define CIRCLE_H
#include "BasicShape.h"
using namespace std;
class Circle : public BasicShape
{
private:
long centerX; // To hold x coordinate
long centerY; // To hold y coordinate
double radius; // To hold radius
public:
// Constructor
Circle(long, long, double );
// Accessors
long getCenterX() const;
long getCenterY() const;
// Overridden calcArea function
virtual double calcArea() const;
};
#endif
// Circle.cpp
#include "Circle.h"
//***********************************************
// Constructor that accepts arguments and uses *
// the this-> pointer to access the class member*
//***********************************************
Circle::Circle(long centerX, long centerY, double radius)
{ this->centerX = centerX;
this->centerY = centerY;
this->radius = radius;
// Call the overriden calcArea function
calcArea(); }
//***********************************************
// getCenterX function returns the x coodinates *
//***********************************************
long Circle::getCenterX() const
{ return centerX; }
//***********************************************
// getCenterY function returns the y coodinates *
//***********************************************
long Circle::getCenterY() const
{ return centerY; }
//***********************************************
// Circle::calcArea() calculates the area of a *
// circle and returns
it *
//***********************************************
double Circle::calcArea() const
{ double area = 3.14159*radius*radius;
return area; }
#ifndef RECTANGLE_H
#define RECTANGEL_H
#include "BasicShape.h"
class Rectangle : public BasicShape
{
private:
long width; // To hold width
long length; // To hold length
public:
// Constructor
Rectangle(long, long);
//Accessors
long getWidth() const;
long getLength() const;
// Overridden calcArea function
virtual long calcArea() const;
};
#endif
// Rectangle.cpp
#include "Rectangle.h"
//***********************************************
// Constructor that accepts arguments and uses *
// the this-> pointer to access the class member*
//***********************************************
Rectangle::Rectangle(long width, long length)
{ this->width = width;
this->length = length;
// Call the overriden calcArea function
calcArea(); }
//***********************************************
// getWidth function returns the
width *
//***********************************************
long Rectangle::getWidth() const
{ return width; }
//***********************************************
// getLength function returns the
length *
//***********************************************
long Rectangle::getLength() const
{ return length; }
//***********************************************
// Rectangle::calcArea() calculates the area of a
//
Rectangle *
//***********************************************
long Rectangle::calcArea() const
{ long area = length*width; }
//***********************************************
// Pure Abstract Base Class *
// This program defines a Circle object and a *
// Rectangle object and calculates the area of *
// each. *
//***********************************************
// SAVE IT AS BasicShape.h
#ifndef BASICSHAPE_H
#define BASICSHAPE_H
using namespace std;
class BasicShape
{
protected:
double area; // To hold area
public:
// Accessor function to return area
double getArea() const
{
return area;
}
// Pure virtual function
virtual double calcArea() const = 0;
};
#endif
// SAVE IT AS Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include "BasicShape.h"
using namespace std;
class Circle : public BasicShape
{
private:
long centerX; // To hold x coordinate
long centerY; // To hold y coordinate
double radius; // To hold radius
public:
// Constructor
Circle(long, long, double );
// Accessors
long getCenterX() const;
long getCenterY() const;
// Overridden calcArea function
virtual double calcArea() const;
};
#endif
// SAVE IT AS Circle.cpp
// Circle.cpp
#include "Circle.h"
//***********************************************
// Constructor that accepts arguments and uses *
// the this-> pointer to access the class member*
//***********************************************
Circle::Circle(long centerX, long centerY, double radius)
{ this->centerX = centerX;
this->centerY = centerY;
this->radius = radius;
// Call the overriden calcArea function
calcArea(); }
//***********************************************
// getCenterX function returns the x coodinates *
//***********************************************
long Circle::getCenterX() const
{ return centerX; }
//***********************************************
// getCenterY function returns the y coodinates *
//***********************************************
long Circle::getCenterY() const
{ return centerY; }
//***********************************************
// Circle::calcArea() calculates the area of a *
// circle and returns it *
//***********************************************
double Circle::calcArea() const
{ double area = 3.14159*radius*radius;
return area; }
// SAVE IT AS Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGEL_H
#include "BasicShape.h"
class Rectangle : public BasicShape
{
private:
long width; // To hold width
long length; // To hold length
public:
// Constructor
Rectangle(long, long);
//Accessors
long getWidth() const;
long getLength() const;
// Overridden calcArea function
virtual double calcArea() const;
};
#endif
// SAVE IT AS Rectangle.cpp
#include "Rectangle.h"
//***********************************************
// Constructor that accepts arguments and uses *
// the this-> pointer to access the class member*
//***********************************************
Rectangle::Rectangle(long width, long length)
{ this->width = width;
this->length = length;
// Call the overriden calcArea function
calcArea(); }
//***********************************************
// getWidth function returns the width *
//***********************************************
long Rectangle::getWidth() const
{ return width; }
//***********************************************
// getLength function returns the length *
//***********************************************
long Rectangle::getLength() const
{ return length; }
//***********************************************
// Rectangle::calcArea() calculates the area of a
// Rectangle *
//***********************************************
double Rectangle::calcArea() const
{
return
length*width;
}
// MAIN PROGRAM.....
#include "Circle.h"
#include "Rectangle.h"
#include
using namespace std;
// Function prototype
void DisplayArea(BasicShape* shape)
{
cout <<"Area Given by "
<< shape->calcArea() << endl;
}
void main()
{
Rectangle R1(4,5);
Circle C1(0,0,5);
cout <<"Rectangle
";
DisplayArea(&R1);
cout <<"Circle
";
DisplayArea(&C1);
}
Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...
Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...
C++
Could you check my code, it work but professor said that
there are some mistakes(check virtual functions, prin() and others,
according assignment) .
Assignment:
My code:
#include<iostream>
#include<string>
using namespace std;
class BasicShape { //Abstract base class
protected:
double area;
private:
string name;
public:
BasicShape(double a, string n) {
area=a;
name=n;
}
void virtual calcArea()=0;//Pure Virtual Function
virtual void print() {
cout<<"Area of "<<getName()<<" is:
"<<area<<"\n";
}
string getName(){
return name;
}
};
class Circle : public...
C++ program.
int main()
{
Rectangle box; // Define an
instance of the Rectangle class
double rectWidth; // Local variable for width
double rectLength; // Local variable for length
string rectColor;
// Get the rectangle's width and length from the
user.
cout << "This program will calculate the area of
a\n";
cout << "rectangle. What is the width? ";
cin >> rectWidth;
cout << "What is the length? ";
cin >>...
My assignment is to create a rectangle class in java. •Create a MyRectangleClass •There are two private instance variables, length l and width w with double data type. •There is a constructor with two parameter •There are 4 public methods which are circumference, area, getWidth, and getLength. •circumference method will return circumference to the caller.(the formula for circumference is 2 *l + 2*w ) •Area method Return the area to the caller(:l*w) •getLength method will return length to the caller....
Task:
Tasks to complete:
------------------------------------------------------------------------------------------------------------------------------------------
given code:
------------------------------------------------------------------------------------------------------------------------------------------
main.cpp
#include
#include "rectangleType.h"
using namespace std;
// part e
int main()
{
rectangleType rectangle1(10, 5);
rectangleType rectangle2(8, 7);
rectangleType rectangle3;
rectangleType rectangle4;
cout << "rectangle1: " << rectangle1 <<
endl;
cout << "rectangle2: " << rectangle2 <<
endl;
rectangle3 = rectangle1 + rectangle2;
cout << "rectangle3: " << rectangle3 << endl;
rectangle4 = rectangle1 * rectangle2;
cout << "rectangle4: " << rectangle4 << endl;
if (rectangle1 > rectangle2)
cout << "Area...
Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...
Construct a C++ class named Rectangle that has floating-point data members named length and width. The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...
Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle { private double length; private double width; public Rectangle(double l, double w) { length = l; width = w; } public double getLength() { ...
Create a program that calculates the area of various shapes.
Console
Specifications
Create an abstract class named Shape. This
class should contain virtual member function named
get_area() that returns a double type.
Create a class named Circle that inherits the
Shape class and contains these constructors and member functions:
Circle(double radius)
double get_radius()
void set_radius(double radius)
double get_area()
Create a class named Square that inherits the
Shape class and contains these constructors and member functions:
Square(double width)
double get_width()
void...
java language
Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...