class 继承和使用 setter 和 getter
class inheritance and using setters and getters
在这里,我从基础 class GradedActivity 中派生了一个名为 Essay 的 class。我在主要的对象中创建了论文 class 的对象。当我在 main() 中编写 object.setGrammar(grammarPts) 时,我希望在 setGrammar() 函数的变量语法中提供分数。我究竟做错了什么?谢谢!
我收到一个错误:
99 8 F:\lab6part3.cpp [错误] 请求 'object' 中的成员 'setGrammar',属于非 class 类型 '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
在这里,我从基础 class GradedActivity 中派生了一个名为 Essay 的 class。我在主要的对象中创建了论文 class 的对象。当我在 main() 中编写 object.setGrammar(grammarPts) 时,我希望在 setGrammar() 函数的变量语法中提供分数。我究竟做错了什么?谢谢! 我收到一个错误:
99 8 F:\lab6part3.cpp [错误] 请求 'object' 中的成员 'setGrammar',属于非 class 类型 '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