重载构造函数和数组的C ++问题 [英] C++ trouble with overloaded constructor and arrays

查看:91
本文介绍了重载构造函数和数组的C ++问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚我如何调用成员函数将数组的值复制到适当的成员变量中的语法。这是专门为重载的构造函数,因为我认为所有其他函数都是正确的,但它们可能不是。



Student.cpp

I am having trouble figuring out the syntax for how i am suppose to Call the member function to copy the values of my array into the appropriate member variables. This is specifically for the overloaded constructor because I think all the other functions are correct but they might not be.

Student.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "Student.h"
using namespace std;

void Student::setID(int tID)
{
	ID = tID;
}

void Student::setFName(string f)
{
	firstName = f;
}

void Student::setLName(string l)
{
	lastName = l;
}

void Student::setScores(int list[])
{
	for (int i = 0; i < 5; i++)
		scores[i] = list[i];
	//scores[5] = list[5];
}

int Student::getID()
{
	return ID;
}

string Student::getFName()
{
	return firstName;
}

string Student::getLName()
{
	return lastName;
}

int Student::getWeightedTotal()
{
	int WeightedTotal = 0;
	WeightedTotal = 25 % (scores[0]) + 25 % (scores[1]) + 30 % (scores[2]) + 10 % (scores[3]) + 10 % (scores[4]);
	return WeightedTotal;
}

char Student::getGrade() // should be working 
{
	char grade = 0;
	if (getWeightedTotal() >= 90)
		grade = 'A';
	else if (getWeightedTotal() >= 80)
		grade = 'B';
	else if (getWeightedTotal() >= 70)
		grade = 'C';
	else if (getWeightedTotal() >= 60)
		grade = 'D';
	else
		grade = 'F';
	return grade;
}

void Student::printStudent()
{
	cout << "Student ID" << "    " << "Full Name" << "         " << "Scores" << "     " << "Weighted Total" << "     " << "Grade" << endl;
	cout << ID << "       " << firstName << " " << lastName << " " <<
		scores[0] << scores[1] << scores[2] << scores[3] << scores[4] << " " << getWeightedTotal() << "   " << getGrade() << endl;
}

Student::Student()
{
	ID = 0;
	firstName = "";
	lastName = "";

	for (int i = 0; i < 5; i++) // for loop to set each element to 0
		scores[i] = 0;


	//scores[5] = { 0 };

}

Student::Student(int tID, string f, string l, int list[])
{
	setID(tID);
	setFName(f);
	setLName(l);
	for (int i = 0; i < 5; i++)
		setScores(list[i]);
	//setScores(list);


}



问题就在重载构造函数的正文之上,我不知道该怎么做



什么我试过了:



我试过了


The problem is right above this text in the overloaded constructor and i'm not sure what to do

What I have tried:

I've tried

for (int i = 0; i < 5; i++)
		setScores(list[i]);



也试过


Also tried just

setScores(list);



也尝试了


also tried

for (int i = 0; i < 5; i++)
		scores[i] = list[i];

推荐答案

正确的语法是

The correct syntax is
Student::Student(int tID, string f, string l, int list[])
{
  setID(tID);
  setFName(f);
  setLName(l);
  setScores(list);
}







假设 Student.h 包含类似




Assuming Student.h contains something like

class Student
{
  int ID;
  int scores[5];
  string firstName, lastName;
public:

  void setID(int tID);
  void setFName(string f);
  void setLName(string l);
  void setScores(int list[]);
  int getID();
  string getFName();
  string getLName();
  int getWeightedTotal();
  char getGrade(); // should be working 
  void printStudent();
  Student();
  Student(int tID, string f, string l, int list[]);

};





代码使用以下主要编译并正确运行:



The code compiles and runs correctly with the following main:

int main()
{
  int score[] = {10,9,8,7,6}; 
  Student s(42, "Foo", "Bar", score);
  s.printStudent();
}


这篇关于重载构造函数和数组的C ++问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆