這裏我從基類GradedActivity中創建了一個名爲Essay的派生類。我在主要被調用的對象中創建了一個Essay類的對象。當我在main()中編寫object.setGrammar(grammarPts)時,我希望能夠在setGrammar()函數的變量語法中提供分數。我究竟做錯了什麼?謝謝! 我得到一個錯誤:類繼承和使用setter和getters
99 8 F:\ lab6part3.cpp [錯誤]請求'對象'中的成員'setGrammar',它是非類類型'Essay(float,float,float,float) '
#include <iostream>
using namespace std;
//class gradedactivity (page 900)
class GradedActivity
{
protected:
double score;
public:
//default constructor
GradedActivity()
{
score = 0.0;
}
//parameterized constructor
GradedActivity(double s)
{
score = s;
}
setScore(double s)
{
score = s;
}
double getScore() const
{
return score;
}
char getLetterGrade() const;
};
class Essay : public GradedActivity
{
private:
float grammar;
float spelling;
float length;
float content;
public:
Essay(float g, float s, float l, float c)
{
setGrammar(g);
setSpelling(s);
setLength(l);
setContent(c);
}
void setGrammar(float);
float getGrammar();
void setSpelling(float);
float getSpelling();
void setLength(float);
float getLength();
void setContent(float);
float getContent();
};
void Essay::setGrammar(float g)
{
grammar = g;
}
float Essay::getGrammar() {return grammar;}
void Essay::setSpelling(float s)
{
spelling = s;
}
float Essay::getSpelling() {return spelling;}
void Essay::setLength(float l)
{
length = l;
}
float Essay::getLength() {return length;}
void Essay::setContent(float c)
{
content = c;
}
float Essay::getContent() {return content;}
int main()
{
float grammarPts;
cout << "How many points, out of 30, did the student get for grammar?";
cin >> grammarPts;
Essay object;
object.setGrammar(grammarPts);
return 0;
}
你能否在你的硬盤上多付幾個字節並使用縮進? – LogicStuff
在這個例子中,我沒有看到你需要繼承GradedActivity的原因。至少在代碼的和平中,沒有實際的用法。 – Joel
您在'setScore'之前錯過了'void',並且'Essay'沒有生成默認的構造函數。 – LogicStuff