类继承和使用setter和getters

时间:2016-03-06 19:44:24

标签: c++ class inheritance setter getter

这里我从基类GradedActivity中创建了一个名为Essay的派生类。我在主要的被称对象中创建了一个Essay类的对象。当我在main()中编写object.setGrammar(grammarPts)时,我希望在setGrammar()函数中提供要在变量语法中保存的分数。我究竟做错了什么?谢谢! 我收到一个错误:

99 8 F:\ lab6part3.cpp [错误]请求'对象'中的成员'setGrammar',它是非类型的'Essay(浮动,浮动,浮动,浮动)'

#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;
}

1 个答案:

答案 0 :(得分:0)

这可能只是因为您从未为Essay定义默认构造函数。

无论如何,我定义了一个默认构造函数,你的代码运行正常,这可能是问题所在。 https://ideone.com/yNxV8N