//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
Это типы классов.Объект абстрактного класса типа «Прямоугольник» не допускается
//QuizShape.cpp
#include "QuizShape.h"
В настоящее время это не делает ничего, кроме моста файлов.
//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;
}
Код не компилируется из-за того, что прямоугольник, предположительно является абстрактным классом, и при наведении курсора мыши над объявлением r1
в main
, я получаю ошибку
«Объект абстрактного класса тип «Прямоугольник» не допускается ».
Я проверил другие ответы на этом сайте и другие, и не сталкивался с чем-то, что решает проблему.
ПРИМЕЧАНИЕ: Я понимаю, что утверждения для виртуальных функций, оканчивающиеся на = 0; заставьте класс стать абстрактным. QuizShape ДОЛЖЕН быть абстрактным. Я определил виртуальные функции в Rectangle, но все же остается абстрактным классом.
Как изменить класс Rectangle виртуальных функций, чтобы Rectangle больше не был абстрактным?
Это тени. Я считаю, что для этого есть предупреждения. – chris
Ваши 'getArea',' getArea', 'getPerimeter' имеют разные функции с функциями, объявленными в' QuizShape' – billz