Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
初学者C++;遗产 我有少量的C经验,但是在OOP和AM中没有经验,试图解决C++中的继承问题。我想我把派生类的定义搞砸了,因为我的average变量计算不正确。此外,我甚至不知道如何打印测试用例以进行调试,因为当我向calculate()添加cout语句时,它不会做任何事情 #include <iostream> #include <vector> using namespace std; // given this definition class Person{ protected: string firstName; string lastName; int id; public: Person(string firstName, string lastName, int identification){ this->firstName = firstName; this->lastName = lastName; this->id = identification; } void printPerson(){ cout<< "Name: "<< lastName << ", "<< firstName <<"\nID: "<< id << "\n"; } }; // this is what I'm supposed to be creating class Student : public Person{ private: vector<int> testScores; public: Student(string firstName, string lastName, int id, vector<int> testScores) : Person(firstName,lastName, id) {}; char calculate(){ double average = 0; for (int i = 0; i < testScores.size(); i++){ average += testScores[i]; cout << average; } average = average / testScores.size(); if ((average >= 90) && (average <= 100)) return 'O'; else if ((average >= 80) && (average < 90)) return 'E'; else if ((average >= 70) && (average < 80)) return 'A'; else if ((average >= 55) && (average < 70)) return 'P'; else if ((average >= 40) && (average < 55)) return 'D'; else if (average < 40) return 'T'; else return 'X'; // always returns this?? } }; // also given this main int main() { string firstName; string lastName; int id; int numScores; cin >> firstName >> lastName >> id >> numScores; vector<int> scores; for(int i = 0; i < numScores; i++){ int tmpScore; cin >> tmpScore; scores.push_back(tmpScore); } Student* s = new Student(firstName, lastName, id, scores); s->printPerson(); cout << "Grade: " << s->calculate() << "\n"; return 0; } #包括 #包括 使用名称空间std; //根据这个定义 班主任{ 受保护的: 字符串名; 字符串lastName; int-id; 公众: Person(字符串名、字符串名、整数标识){ 这个->名字=名字; 此->lastName=lastName; 此->id=标识; } void printPerson(){ cout_C++_Inheritance - Fatal编程技术网 名字=名字; 此->lastName=lastName; 此->id=标识; } void printPerson(){ cout,c++,inheritance,C++,Inheritance" /> 名字=名字; 此->lastName=lastName; 此->id=标识; } void printPerson(){ cout,c++,inheritance,C++,Inheritance" />

初学者C++;遗产 我有少量的C经验,但是在OOP和AM中没有经验,试图解决C++中的继承问题。我想我把派生类的定义搞砸了,因为我的average变量计算不正确。此外,我甚至不知道如何打印测试用例以进行调试,因为当我向calculate()添加cout语句时,它不会做任何事情 #include <iostream> #include <vector> using namespace std; // given this definition class Person{ protected: string firstName; string lastName; int id; public: Person(string firstName, string lastName, int identification){ this->firstName = firstName; this->lastName = lastName; this->id = identification; } void printPerson(){ cout<< "Name: "<< lastName << ", "<< firstName <<"\nID: "<< id << "\n"; } }; // this is what I'm supposed to be creating class Student : public Person{ private: vector<int> testScores; public: Student(string firstName, string lastName, int id, vector<int> testScores) : Person(firstName,lastName, id) {}; char calculate(){ double average = 0; for (int i = 0; i < testScores.size(); i++){ average += testScores[i]; cout << average; } average = average / testScores.size(); if ((average >= 90) && (average <= 100)) return 'O'; else if ((average >= 80) && (average < 90)) return 'E'; else if ((average >= 70) && (average < 80)) return 'A'; else if ((average >= 55) && (average < 70)) return 'P'; else if ((average >= 40) && (average < 55)) return 'D'; else if (average < 40) return 'T'; else return 'X'; // always returns this?? } }; // also given this main int main() { string firstName; string lastName; int id; int numScores; cin >> firstName >> lastName >> id >> numScores; vector<int> scores; for(int i = 0; i < numScores; i++){ int tmpScore; cin >> tmpScore; scores.push_back(tmpScore); } Student* s = new Student(firstName, lastName, id, scores); s->printPerson(); cout << "Grade: " << s->calculate() << "\n"; return 0; } #包括 #包括 使用名称空间std; //根据这个定义 班主任{ 受保护的: 字符串名; 字符串lastName; int-id; 公众: Person(字符串名、字符串名、整数标识){ 这个->名字=名字; 此->lastName=lastName; 此->id=标识; } void printPerson(){ cout

初学者C++;遗产 我有少量的C经验,但是在OOP和AM中没有经验,试图解决C++中的继承问题。我想我把派生类的定义搞砸了,因为我的average变量计算不正确。此外,我甚至不知道如何打印测试用例以进行调试,因为当我向calculate()添加cout语句时,它不会做任何事情 #include <iostream> #include <vector> using namespace std; // given this definition class Person{ protected: string firstName; string lastName; int id; public: Person(string firstName, string lastName, int identification){ this->firstName = firstName; this->lastName = lastName; this->id = identification; } void printPerson(){ cout<< "Name: "<< lastName << ", "<< firstName <<"\nID: "<< id << "\n"; } }; // this is what I'm supposed to be creating class Student : public Person{ private: vector<int> testScores; public: Student(string firstName, string lastName, int id, vector<int> testScores) : Person(firstName,lastName, id) {}; char calculate(){ double average = 0; for (int i = 0; i < testScores.size(); i++){ average += testScores[i]; cout << average; } average = average / testScores.size(); if ((average >= 90) && (average <= 100)) return 'O'; else if ((average >= 80) && (average < 90)) return 'E'; else if ((average >= 70) && (average < 80)) return 'A'; else if ((average >= 55) && (average < 70)) return 'P'; else if ((average >= 40) && (average < 55)) return 'D'; else if (average < 40) return 'T'; else return 'X'; // always returns this?? } }; // also given this main int main() { string firstName; string lastName; int id; int numScores; cin >> firstName >> lastName >> id >> numScores; vector<int> scores; for(int i = 0; i < numScores; i++){ int tmpScore; cin >> tmpScore; scores.push_back(tmpScore); } Student* s = new Student(firstName, lastName, id, scores); s->printPerson(); cout << "Grade: " << s->calculate() << "\n"; return 0; } #包括 #包括 使用名称空间std; //根据这个定义 班主任{ 受保护的: 字符串名; 字符串lastName; int-id; 公众: Person(字符串名、字符串名、整数标识){ 这个->名字=名字; 此->lastName=lastName; 此->id=标识; } void printPerson(){ cout,c++,inheritance,C++,Inheritance,问题在于,您将从控制台读取的分数交给了构造函数: Student* s = new Student(firstName, lastName, id, scores); 不幸的是,在您的类中,您没有使用它初始化Student对象的分数: Student(string firstName, string lastName, int id, vector<int> testScores) : Person(firstName,lastName, id) {}; //

问题在于,您将从控制台读取的分数交给了构造函数:

 Student* s = new Student(firstName, lastName, id, scores);
不幸的是,在您的类中,您没有使用它初始化Student对象的分数:

Student(string firstName, string lastName, int id, vector<int> testScores)
       : Person(firstName,lastName, id) 
{};   // ouch testScores is lost

问题在于,您将从控制台读取的分数交给了构造函数:

 Student* s = new Student(firstName, lastName, id, scores);
不幸的是,在您的类中,您没有使用它初始化Student对象的分数:

Student(string firstName, string lastName, int id, vector<int> testScores)
       : Person(firstName,lastName, id) 
{};   // ouch testScores is lost

学生的构造函数不会将测试分数复制到学生的测试分数中。不相关,但您不应该经常使用
new
Student s(名字、姓氏、id、分数)
很好。另一方面,忘了HackerRank吧。出于好奇,我最近研究了这些挑战,它们简直太可怕了。我没有写
main()<代码> >包含<代码>新< /代码>,所以你必须让他们知道;除了HR之外,还有什么建议吗?我正在尝试学习一点算法、数据结构、C++、Python、java和一些概率编程。在夏天,他们的域名列表包括所有这些,所以我认为这是一个很好的资源。如果你选择不同的参数r名称,您不必使用
this->
语法。学生的构造函数不会将测试分数复制到学生的测试分数中。不相关,但您不应该经常使用
new
Student s(名字、姓氏、id、分数)
很好。另一方面,忘了HackerRank吧。出于好奇,我最近研究了这些挑战,它们简直太可怕了。我没有写
main()<代码> >包含<代码>新< /代码>,所以你必须让他们知道;除了HR之外,还有什么建议吗?我正在尝试学习一点算法、数据结构、C++、Python、java和一些概率编程。在夏天,他们的域名列表包括所有这些,所以我认为这是一个很好的资源。如果你选择不同的参数r名称,您不必使用
this->
语法。