//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;
}
的代碼不會由於編譯的事實,矩形是假想一個抽象類,並在main
上空盤旋的r1
的聲明時,我收到錯誤
「抽象類的對象不允許輸入「矩形」。
我查看了本網站和其他人的其他答案,但沒有遇到解決問題的東西。
注:我明白虛擬函數以= 0結尾的語句;使課堂變得抽象。 QuizShape應該是抽象的。我已經在Rectangle中定義了虛函數,但它仍然是一個抽象類。
如何修改虛擬函數Rectangle類,使Rectangle不再抽象?
那些影子。我相信有這樣的警告。 – chris
您的'getArea','getArea','getPerimeter'具有不同的函數簽名,並具有在'QuizShape'中聲明的函數 – billz