Why am I getting the address in the output. Rather I should get the Output Length= (value input by user) , Width = (value input by user).
As in the main body of program after getting input R1.getdata() , ptr->result() should display the result of Rectangle class.
#include <iostream>
using namespace std;
class Rectangle {
protected:
float length;
float width;
public:
void getdata() {
cout << "Enter length and width= ";
cin >> length >> width;
}
void result() {
cout << "Length = " << length << "\nWidth = " << width << endl;
}
};
class Area : public Rectangle {
private:
float area;
public:
void calc_area() { area = length * width; }
void result() { cout << "Area = " << area << endl; }
};
class Perimeter : public Rectangle {
private:
float perimeter;
public:
void calc_peri() { perimeter = 2 * (length + width); }
void result() { cout << "Perimeter = " << perimeter << endl; }
};
void main() {
Rectangle R1;
Area A1;
Perimeter P1;
Rectangle *ptr;
R1.getdata();
ptr = &A1;
ptr->result();
}
You are getting the wrong values, because you are calling ptr->result(); on a uninitialized Area object (A1), which has been upcasted from pointer to Rectangle object.
The values the user inputs though are used in the R1 object, which you then don't use anymore. Moreover, you should make the result() method virtual.
Lastly, the syntax for calling base class method on a pointer to an inheriting class is: ptr->Rectangle::result();.
Below you will find your code with some fixes that demonstrate things I wrote about:
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Rectangle {
protected:
float length;
float width;
public:
void getdata() {
cout << "Enter length and width= ";
cin >> length >> width;
std::cout << length << " " << width << std::endl;
}
virtual void result() {
cout << "(Rectangle) Length = " << length << "\nWidth = " << width
<< endl;
}
};
class Area : public Rectangle {
private:
float area;
public:
void calc_area() { area = length * width; }
void result() { cout << "Area = " << area << endl; }
};
class Perimeter : public Rectangle {
private:
float perimeter;
public:
void calc_peri() { perimeter = 2 * (length + width); }
void result() { cout << "Perimeter = " << perimeter << endl; }
};
int main() {
Rectangle R1;
Area* A1;
Perimeter P1;
Rectangle* ptr;
R1.getdata();
ptr = &R1;
A1 = static_cast<Area*>(ptr);
// or:
// A1 = (Area*)ptr;
ptr->Rectangle::result();
}
Ptr points to the address of a child of class Rectangle (the Area class) and therefore it calls the member (result) of the object it refers to (A1 of type Area)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I have tried to run the program with more than one inheritance i got many errors
For this program ,but when i use it for Source File, it will looks good
I don't know why but I think because i update my version or does make any sense
please help me with my project
int main
#include"Shapes.h"
#include"Cylinder.h"
#include"Sphere.h"
#include"Triangle.h"
#include"Square.h"
#define MAX_SHAPES 100
int main() {
ofstream myfile;
myfile.open("Shapes.dat");
Shape* shapes[MAX_SHAPES];
int currentShapes = 0;
int n = 0;
cout << "Select any Number to calculate : \n";
cout << "1. Square\n";
cout << "2. Triangle\n";
cout << "3. Sphere\n";
cout << "4. Cylinder\n";
cout << "Enter your choice: ";
cin >> n;
cin.ignore();
if (n == 1) {
Square* obj2 = new Square();
obj2->readData();
shapes[currentShapes] = obj2;
}
if (n == 2) {
Triangle* obj3 = new Triangle();
obj3->readData();
shapes[currentShapes] = obj3;
}
if (n == 3) {
Sphere* obj5 = new Sphere();
obj5->readData();
shapes[currentShapes] = obj5;
}
if (n == 4) {
Cylinder* obj7 = new Cylinder();
obj7->readData();
obj7->computeSurfaceArea();
obj7->computeVolume();
obj7->print();
obj7->printToFile();
shapes[currentShapes] = obj7;
}
return 0;
}
shape.h
class Shape {
private:
string color, name;
public:
// default constructor
Shape()
{
color = 1.0;
name = "";
cout << "Base Class is Shape for this element . \n";
}
// parameterized constructor
Shape(string color, string name) {
this->color = color;
this->name = name;
}
// read data
void readData() {
cout << "Enter color: ";
getline(cin, color);
cout << "Enter name: ";
getline(cin, name);
}
virtual void print() = 0;
virtual void printToFile() = 0;
virtual void readFromFile(ifstream& input) = 0;
};
shape2d.h
class Shape2D : public Shape {
public:
double area;
double perimeter;
Shape2D() :Shape()
{
cout << "Parent Class is Shape2D for this element .\n";
}
// parameterized constructor
Shape2D(string color, string name) :Shape(color, name) {
}
// read data
void readData() {
Shape::readData();
}
virtual void computeArea() = 0;
virtual void computePerimeter() = 0;
virtual void print() = 0;
virtual void printToFile() = 0;
virtual void readFromFile(ifstream& input) = 0;
};
Shape3d
class Shape3D : public Shape {
public:
double surfaceArea;
double volume;
Shape3D() :Shape()
{
surfaceArea = 1.0;
volume = 1.0;
cout << "Parent Class is Shape3D for this element .\n";
}
// parameterized constructor
Shape3D(string color, string name) :Shape(color, name) {
}
void readData() {
Shape::readData();
}
virtual void computeSurfaceArea() = 0;
virtual void computeVolume() = 0;
virtual void print() = 0;
virtual void printToFile() = 0;
virtual void readFromFile(ifstream& input) = 0;
};
Square.h
class Square : public Shape2D {
private:
double side;
public:
Square() :Shape2D()
{
side = 1.0;
cout << "Calculating square area and perimeter \n";
}
// parameterized constructor
Square(string color, string name, double side) :Shape2D(color, name) {
this->side = side;
}
void readData() {
Shape2D::readData();
cout << "Square Side: ";
cin >> side;
cin.ignore();
computeArea();
computePerimeter();
}
void computeArea()
{
area = side * side;
}
void computePerimeter() {
perimeter = 4 * side;
}
void print(string x, double y)
{
cout << x << " of Square = " << y << "\n";
}
void print() {
print("Area: ", area);
print("Perimeter", perimeter);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Square = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Area: ", area);
printToFile("Perimeter", perimeter);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
Triangle.h
class Triangle : public Shape2D {
private:
double base;
double height;
public:
Triangle() :Shape2D()
{
cout << "This is a Triangle\n";
}
// parameterized constructor
Triangle(string color, string name, double base, double height) :Shape2D(color, name) {
this->base = base;
this->height = height;
}
void readData() {
Shape2D::readData();
cout << "Triangle Base: ";
cin >> base;
cout << "Triangle Height: ";
cin >> height;
cin.ignore();
computeArea();
computePerimeter();
}
void computeArea() {
area = (height * base) / 2;
}
void computePerimeter() {
perimeter = 2 * height + base;
}
void print(string x, double y)
{
cout << x << " of Triangle = " << y << "\n";
}
void print() {
print("Area: ", area);
print("Perimeter: ", perimeter);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Triangle = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Area: ", area);
printToFile("Perimeter", perimeter);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
Sphere.h
class Sphere : public Shape3D {
private:
double radius;
public:
Sphere() :Shape3D()
{
radius = 1.0;
cout << "This is a Sphere\n";
}
// parameterized constructor
Sphere(string color, string name, double side) :Shape3D(color, name) {
this->radius = radius;
}
void readData() {
Shape3D::readData();
cout << "Sphere Radius: ";
cin >> radius;
cin.ignore();
computeSurfaceArea();
computeVolume();
}
void computeSurfaceArea() {
surfaceArea = 4 * 3.14 * radius * radius;
}
void computeVolume() {
volume = (4 / 3) * (3.14 * radius * radius * radius);
}
void print(string x, double y)
{
cout << x << " of Sphere = " << y << "\n";
}
void print() {
print("Surface Area: ", surfaceArea);
print("Volume: ", volume);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Sphere = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Surface Area: ", surfaceArea);
printToFile("Volume", volume);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
Cylinder .h
class Cylinder : public Shape3D {
private:
double radius;
double height;
public:
Cylinder() :Shape3D()
{
radius = 1.0;
height = 1.0;
cout << "This is a Cylinder\n";
}
// parameterized constructor
Cylinder(string color, string name, double radius, double height) :Shape3D(color, name) {
this->radius = radius;
this->height = height;
}
void readData() {
Shape3D::readData();
cout << "Cylinder Radius: ";
cin >> radius;
cout << "Cylinder Height: ";
cin >> height;
cin.ignore();
//computeSurfaceArea();
//computeVolume();
}
void computeSurfaceArea() {
surfaceArea = (2 * 3.14 * radius) * (radius + height);
}
void computeVolume() {
volume = 3.14 * radius * radius * height;
}
void print(string x, double y)
{
cout << x << " of Cylinder = " << y << "\n";
}
void print() {
print("Surface Area: ", surfaceArea);
print("Volume: ", volume);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Cylinder = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Surface Area: ", surfaceArea);
printToFile("Volume", volume);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
In shape2d.h and shape3d.h with have to add
#include "shape.h"
In square.h and triangle.h with have to add
#include "shape2d.h"
In sphere.h and cylinder.h you have to add
#include "shape3d.h"
And to avoid multiple inclusions add at the beginning of each .h file
#pragma once
or if your compiler doesn't support it enclose the hole code inside any .h in something like
#ifndef YOUR_H_FILE_NAME_H
#define YOUR_H_FILE_NAME_H
// ... your .h code here
#endif
Further in main.cpp you include shapes.h which doesn't exist but you can remove it because you already include all the specilized .h file
I am trying to do some multilevel inheritance from the Shape class to Rectangle, Circle and Triangle classes. From Rectangle I need to inherit a Square class and print the area, info, etc.. as well as Ellipse from Circle and Isosceles from Triangle. So far my first inherited classes work fine, but whenever I try to make the "grandchildren" class work I cannot make it work. I have no idea what I might be doing wrong. Here is the code:
#include <iostream>
using namespace std;
class Shape{
protected:
string name;
float area;
public:
Shape(string nm):name(nm){}
//Getters
string getName(){ return name; }
float getArea(){}
//Setters
virtual void setArea(){}
//Print
virtual void printInfo()
{
cout << "Name: " << name << " Color: " << endl;
}
};
class Rectangle : public Shape{
private:
float length, width;
public:
Rectangle(string nm, float l, float w):Shape::Shape(nm), length(l), width(w){}
Shape::getName();
//Setters
void setArea(){ area = length*width; }
void printInfo(){
//Shape::printInfo();
cout << "Name: " << name << " L: " << length << " W: " << width << " A: " << area << endl;
}
};
class Square : public Rectangle{
private:
float length;
public:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm){}
float getLength(){return length;}
//Setters
void setArea(){ area = length *length; }
};
class Circle : public Shape{
private:
float radius;
const float pi = 3.0;
public:
Circle(string nm, float r):Shape::Shape(nm), radius(r){}
//Setters
void setArea(){ area = pi*radius*radius; }
void printInfo(){
//Shape::printInfo();
cout << "Name: " << name << " R: " << radius << " A: " << area << endl;
}
};
//class Ellipse : public Circle{
//
//private:
// float length, width, radius1, radius2;
//
//public:
// Ellipse(string nm, int clr, float l, float w);
//
// //Setters
//void setArea(){ area = radius1 * radius2; }
//
//};
class Triangle : public Shape{
private:
float a, base, c, height;
public:
Triangle(string nm, float a, float b, float c, float h):Shape::Shape(nm), a(a), base(b), c(c), height(h){}
//Setters
void setArea(){ area = (base*height)/2; }
void printInfo(){
//Shape::printInfo();
cout << "Name: " << name << " Color: " << " A: " << a << " Base: " << base << " C: " << c << " H: " << height << " P: " << " A: " << area << endl;
}
};
//class Isosceles : public Triangle{
//
//private:
// float base, height;
//
//public:
// Isosceles(string nm, int clr, float l, float w);
//
// //Setters
// void setArea(){ area = (base*height)/2; }
//
//};
int main() {
Rectangle r("Rectangle", 10, 20);
Circle c("Circle", 1);
Triangle tt("Triangle", 2, 2, 3, 3);
Square ss("Square", 10);
Shape* s;
Shape* t;
Shape* u;
Shape* v;
s = &r;
t = &c;
u = &tt;
v = &ss;
//Set and print area of Rectangle
s->setArea();
s->printInfo();
//Set and print area of Circle
t->setArea();
t->printInfo();
//Set and print area of Triangle
u->setArea();
u->printInfo();
//Set and print area of Rectangle
v->setArea();
v->printInfo();
return 0;
}
I get an error while setting up the Square class over here:
class Square : public Rectangle{
private:
float length;
public:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm){}
I commented out the Ellipse and Isosceles classes just so I could set up correctly Square and work no them later.
This is my first time asking something, so if something is not correct please let me know.
Thank you for your help.
In your Square class I believe I found one mistake...
Try doing the following with your square constructor:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm, l, l){}
As opposed to what you had... that will fix the errors you are getting with the Square class.
The reason for the difference is because when you were passing arguments to the Rectangle constructor from the Square constructor you were leaving some arguments unintialized (in the Rectangle constructor).
I just wonder if all virtual function has got to be a const?
I'm having some issue with them as the area always return 0 for my square when ever i wanna print them out. would appreciate if someone could enlighten me.
shapetwod.h
class ShapeTwoD
{
protected:
string name, warpSpace;
bool containsWarpSpace;
public:
//constructor
ShapeTwoD();
ShapeTwoD(string, bool);
//accessors/set function
void setName(string);
//mutator/get function
string getName();
//methods
virtual double computeArea();
virtual void view();
};
shapetwod.cpp
ShapeTwoD::ShapeTwoD()
{
string name = "";
}
ShapeTwoD::ShapeTwoD(string ShapeName)
{
name = ShapeName;
}
void ShapeTwoD::setName(string shapeName)
{
name=shapeName;
}
string ShapeTwoD::getName()
{
return name;
}
double ShapeTwoD::computeArea()
{
return 0;
}
void ShapeTwoD::view()
{
cout << "Area is: " << endl;
}
square.h
class Square:public ShapeTwoD
{
private:
int xVal,yVal;
int length, breath;
double area;
public:
Square();
Square(string, int, int, double);
//acessor method
//int getSquareDetails();
int getxCord();
int getyCord();
double getArea();
virtual double computeArea();
void view();
int xvalue[4];
int yvalue[4];
};
square.cpp
Square::Square()
{
xVal = 0;
yVal = 0;
area = 0;
}
Square::Square(string ShapeName, bool warpspace, int xval, int yval, double areas):ShapeTwoD(ShapeName, warpspace)
{
xVal = xval;
yVal = yval;
area = areas;
}
void Square::setSquareCord()
{
for (int i=0; i<4; i++)
{
cout << "Please enter x-ordinate of pt " << i+1 << ": ";
cin >> xVal;
xvalue[i] = xVal;
cout << endl;
cout << "Please enter y-ordinate of pt " << i+1 << ": ";
cin >> yVal;
yvalue[i] = yVal;
cout << endl;
}
}
double Square::computeArea()
{
int xmax = xvalue[1];
int xmin = xvalue[1];
int ymax = yvalue[1];
int ymin = yvalue[1];
for(int i=0; i<4; i++)
{
if(xvalue[i]>xmax)
{
xmax = xvalue[i];
}
else if(xvalue[i]<xmin)
{
xmin = xvalue[i];
}
}
for(int i=0; i<4; i++)
{
if(yvalue[i]>ymax)
{
ymax = yvalue[i];
}
else if(yvalue[i]<ymin)
{
ymin = yvalue[i];
}
}
length = xmax - xmin;
breath = ymax - ymin;
area = length * breath;
return (area);
}
int Square::getxCord()
{
return xVal;
}
int Square::getyCord()
{
return yVal;
}
double Square::getArea()
{
return area;
}
void Square::view()
{
cout << "Name: " << getName() << endl;
cout << "Area is: " << area << endl;
}
Sorry but I don't really know how to phase my question. i'm actually using Polymorphism &
Virtual Functions here and my computeArea and view function are virtual.
so in my square.cpp under the "view" function the programme would always return me 0 and i'm not sure why is this so..
this is how i call the view function. not sure if it's helping here..
void Shape2DLink::InputSensor()
{
string shape,type;
cout<<endl<<"\n"<<"[ Input sensor data ]"<<endl;
cout << "Please enter name of shape: " << endl;
cin >> shape;
shape2D.setName(shape);
cout << "Please enter special type : " << endl;
cin >> type;
shape2D.setWarpSpace(type);
if(shape == "Square")
{
square.setSquareCord();
square.computeArea();
square.isPointOnShape();
square.isPointInShape();
Square *mySquare = new Square;
shapeobject.push_back(mySquare);
//shapeobject.push_back( new Square );
}
}
void Shape2DLink::Display()
{
vector<ShapeTwoD*>::iterator vectorIt = shapeobject.begin();
while(vectorIt != shapeobject.end())
{
(*vectorIt)->view();
vectorIt++;
}
}
I just wonder if all virtual function has got to be a const?
No.
I'm having some issue with them as the area always return 0 for my
square when ever i wanna print them out.
Hmm, ok, let's take a look.
double ShapeTwoD::computeArea()
{
return 0;
}
Yup, uh-huh.
I've got my Magic Monkey Hat on, and I predict that the problem you're having is caused by Object Slicing. Consider:
void foo (Shape2D shape)
{
cout << shape.compute_area() << "\n"
}
int main()
{
Square sq;
foo (sq);
}
Since foo above takes a Shape2D by-value, all the Squareness of whatever is passed to it is sliced away, leaving only a Shape2D. Of course since Shape2D::compute_area just returns 0, that's what is computed.
To fix this particular problem, don't copy the object or take it by-value, but take it by-reference instead:
void foo (Shape2D& shape)
{
cout << shape.compute_area() << "\n"
}
Now the object isn't sliced.
Look for similar problems in the code you haven't shown us.
No a virtual method may or may not be const. Your problem is somewhere else, but not in the code you have included.
Still working on an inheritance program, the base class is a Shape and there are three derived classes: rectangle, Circle, and Square (Square is derived from Rectangle). When I set the data values trough the respective constructors, I get false values for the data members of each derived class when I display them I'm either not setting them correctly (my guess) or I'm not displaying them correctly. Here is a code snippet.
class Shape
{
public:
Shape(double w = 0, double h = 0, double r = 0)
{
width = w;
height = h;
radius = r;
}
virtual double area() = 0;
void display();
protected:
double width;
double height;
double radius;
};
One derived class:
class Rectangle : public Shape
{
public:
Rectangle(double w, double h) : Shape(w, h)
{
}
double area();
void display();
};
Rectangle's display function:
double Rectangle::area()
{
return width * height;
}
Here is my main():
#include<iostream>
#include "ShapeClass.h"
using namespace std;
int main()
{
Rectangle r(3, 2);
Circle c(3);
Square s(3);
c.display();
s.display();
r.display();
system ("pause");
return 0;
}
Complete ShapeClass.cpp:
#include<iostream>
#include "ShapeClass.h"
using namespace std;
double Shape::area()
{
return (width * height);
}
double Rectangle::area()
{
return width * height;
}
double Circle::area()
{
return (3.14159 * radius * radius);
}
double Square::area()
{
return width * width;
}
void Square::display()
{
cout << "Side length of square: " << width << endl;
cout << "Area of square: " << this->area() << endl;
}
void Circle::display()
{
cout << "Radius of circle: " << radius << endl;
cout << "Area of circle: " << this->area() << endl;
}
void Rectangle::display()
{
cout << "Width of rectangle: " << width << endl;
cout << "Height of rectangle: " << height << endl;
cout << "Area of rectangle: " << this->area() << endl;
}
You need to make your area() function virtual in Rectangle as well:
class Rectangle : public Shape
{
public:
Rectangle(double w, double h) : Shape(w, h)
{
}
virtual double area();
void display();
};
Now, also keep in mind that if you want the display() function to be overriden in specific shapes (in your example I see that Rectangle also has a display() function), then you need to make it virtual as well, in both classes:
virtual void display();
Edit: I tried the following code and it worked perfectly. It's based on your code, so there might be a problem with how you built your project or how you compile/link it.
#include <iostream>
using namespace std;
class Shape
{
public:
Shape(double w = 0, double h = 0, double r = 0)
{
width = w;
height = h;
radius = r;
}
virtual double area() = 0;
virtual void display() = 0;
protected:
double width;
double height;
double radius;
};
class Rectangle : public Shape
{
public:
Rectangle(double w, double h) : Shape(w, h)
{
}
virtual double area() { return width * height; }
virtual void display()
{
cout << "Width of rectangle: " << width << endl;
cout << "Height of rectangle: " << height << endl;
cout << "Area of rectangle: " << this->area() << endl;
}
};
int main(int argc, char* argv[])
{
Rectangle r(3, 2);
r.display();
system ("pause");
return 0;
}
//QuizShape.h
#ifndef QUIZSHAPE_H
#define QUIZHAPE_H
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class QuizShape
{
protected:
//outer and inner symbols, and label
char border, inner;
string quizLabel;
public:
//base class constructor with defaults
QuizShape(char out = '*', char in = '+', string name = "3x3 Square")
{
border = out;
inner = in;
quizLabel = name;
cout << "base class constructor, values set" << endl << endl;
};
//getters
char getBorder() const
{ return border; }
char getInner() const
{ return inner; }
string getQuizLabel() const
{ return quizLabel; }
//virtual functions to be defined later
virtual void draw( ) = 0;
virtual int getArea( ) = 0;
virtual int getPerimeter( ) = 0;
};
class Rectangle : public QuizShape
{
protected:
//height and with of a rectangle to be drawn
int height, width;
public:
//derived class constructor
Rectangle(char out, char in, string name,
int h = 3, int w = 3):QuizShape(out, in, name)
{
height = h;
width = w;
cout << "derived class constructor, values set" << endl << endl;
}
//getters
int getHeight() const
{ return height; }
int getWidth() const
{ return width; }
//*********************************************
virtual void draw(const Rectangle &rect1)
{
cout << "draw func" << endl;
cout << rect1.height << endl;
cout << rect1.getWidth() << endl;
cout << rect1.getQuizLabel() << endl;
}
virtual int getArea(const Rectangle &rect2)
{
cout << "area func" << endl;
cout << rect2.getInner() << endl;
cout << rect2.getBorder() << endl;
}
virtual int getPerimeter(const Rectangle &rect3)
{
cout << "perim func" << endl;
cout << rect3.height << endl;
cout << rect3.getWidth() << endl;
cout << rect3.getQuizLabel() << endl;
}
//************************************************
};
#endif
These are the class types so far.
//QuizShape.cpp
#include "QuizShape.h"
This currently does nothing but bridge the files.
//pass7.cpp
#include "QuizShape.cpp"
int main()
{
Rectangle r1('+', '-', "lol", 4, 5);
cout << r1.getHeight() << endl;
cout << r1.getWidth() << endl;
cout << r1.getInner() << endl;
cout << r1.getBorder() << endl;
cout << r1.getQuizLabel() << endl;
system("pause");
return 0;
}
The code will not compile due to the fact that Rectangle is supposedly an abstract class, and when hovering over the declaration of r1 in main, I receive the error
"Object of abstract class type "Rectangle" is not allowed".
I have checked other answers on this site and others and have not come across something that solves the problem.
NOTE: I understand that the statements for virtual functions ending in =0; cause the class to become an abstract one. QuizShape SHOULD be abstract. I have defined the virtual functions in Rectangle and yet it remains an abstract class.
How can I modify the virtual functions Rectangle class so that Rectangle is no longer abstract?
Your methods int the abstract class QuizShape are:
virtual void draw( ) = 0;
virtual int getArea( ) = 0;
virtual int getPerimeter( ) = 0;
but in Rectangle they take const Rectangle &rect1 as parameter so you shadowing the methods and not overriding the abstract one at all. You need to have methods in Rectangle with the same signature as the ones in the abstract base class.
The overridden methods must have the exact same signature, in the derived class you have given them arguments.