繁体   English   中英

类继承和使用 setter 和 getter

[英]class inheritance and using setters and getters

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

99 8 F:\\lab6part3.cpp [错误] 请求'object' 中的成员'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;
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM