返回整个数组
Return entire Array
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string name;
int year;
string semester;
int AmtClass;
string *CName;
static string cohort[4];
public:
Student();
Student(int AmtClass);
Student(Student &);
void setCourses(string courses[], int n);
void setName(string name);
void setYear(int year);
void setSemester(string semester);
string getName();
int getYear();
string getSemester();
int getAmtClass();
string getCohort();
string getCNames();
// ~Student();
};
string Student::cohort[4] = "";
Student::Student()
{
name = "";
year = 0;
semester = "";
AmtClass = 0;
}
//I am trying to create a dynamically allocated array of strings
Student::Student(int amount)
{
AmtClass = amount;
CName = new string[AmtClass];
}
void Student::setCourses(string courses[], int n)
{
CName[n] = courses[n];
}
Student::Student(Student &obj)
{
*this = obj;
}
void Student::setName(string Name)
{
name = Name;
}
void Student::setYear(int Year)
{
year = Year;
switch(year)
{
case 1:
cohort[0] = "Freshman";
break;
case 2:
cohort[1] = "Sophomore";
break;
case 3:
cohort[2] = "Junior";
break;
case 4:
cohort[3] = "Senior";
break;
}
}
void Student::setSemester(string Semester)
{
semester = Semester;
}
string Student::getName()
{
return name;
}
int Student::getYear()
{
return year;
}
string Student::getSemester()
{
return semester;
}
int Student::getAmtClass()
{
return AmtClass;
}
string Student::getCohort()
{
year -= 1;
return cohort[year];
}
//Returns only the first course name i entered, i want to return all
string Student::getCNames()
{
return *CName;
}
void readStudentData(Student &);
void printStudentData(Student);
int main()
{
int amount;
cout << "How many courses are you currently taking? ";
cin >> amount;
Student kid(amount);
readStudentData(kid);
printStudentData(kid);
}
void readStudentData(Student &kid)
{
cin.ignore(10000, 'n');
int amount = kid.getAmtClass();
string name = "";
string semester = "";
int year = 0;
string *pName = new string[amount];
cout << "What is your full name? ";
getline(cin,name);
kid.setName(name);
cout << "Type in 1 for Freshman, 2 for Sophomore, 3 for Junior or 4 for Senior.";
cout << "nWhat year are you? ";
cin >> year;
while(cin)
{ if(year >=1 && year <= 4)
{
kid.setYear(year);
break;
}
else
{
cout << "Number must be between 1 and 4 Please try again." << endl;
year = 0;
cin >> year;
}
}
cin.ignore();
cout << "What is your current semester? ";
getline(cin,semester);
kid.setSemester(semester);
cout << "Please enter the name of all your courses." << endl;
for(int i = 0; i < amount; i++)
{
cout << "Course #" << i+1 << " : ";
getline(cin,pName[i]);
kid.setCourses(pName, i);
}
}
void printStudentData(Student kid)
{
Student kid2 = kid;
cout << "nHere is your information:" << endl;
cout << "Name : " << kid2.getName() << endl;
cout << "Semester : " << kid2.getSemester() << endl;
cout << "Year : " << kid2.getCohort() << endl;
for(int i = 0; i < kid2.getAmtClass(); i++)
{
cout << "Course #" << i+1 << " : " << kid2.getCNames() << endl;
}
}
在这个函数中,我试图创建一个程序来存储一个学生的信息。当我输入学生的课程名称然后打印出来时,我只得到输入的第一个课程名称。我正在尝试打印用户输入的所有课程名称。我认为问题出在accessor函数里面。
这是我的结果
How many courses are you currently taking? 2
What is your full name? Testing Name
Type in 1 for Freshman, 2 for Sophomore, 3 for Junior or 4 for Senior.
What year are you? 4
What is your current semester? Spring 2014
Please enter the name of all your courses.
Course #1 : C++
Course #2 : Adv C++
Here is your information:
Name : Testing Name
Semester : Spring 2014
Year : Senior
Course #1 : C++
Course #2 : C++
可以使用数组元素的索引作为函数getCohort
的参数
string getCohort( int i ) const;
,函数必须返回与给定索引对应的元素,如果索引无效,则返回空字符串。
另一种方法是使用标准类std::array<std::string, 4>
并将其用作返回值。例如
class Student
{
private:
string name;
int year;
string semester;
int AmtClass;
string *CName;
std::array<std::string, 4> cohort;
//...
我删除了关键字static,因为我不明白为什么这个数据成员是静态的。
函数可以声明为
std::array<std::string, 4> getCohort() const;
如果数据成员string *CName;
对应课程,并且你有这样的函数
void setCourses(string courses[], int n);
则CName必须指向动态分配的数组,或者它应该是std::vector<std::string>
类型的对象
std::向量CName;//…
void setCourses( const std::string courses[], int n )
{
CName.assign( courses, courses + n );
}
或
#include <algorithm>
//...
std::string *CName;
//...
void setCourses( const std::string courses[], int n )
{
CName = new std::string[n];
std::copy( courses, courses + n, CName );
}
在最后一种情况下不要忘记删除指针
相关文章:
- 为什么 C++ 中的字符数组返回其项目而不是地址?
- C++ bool 数组返回 1 或 0 以外的值
- C++ ostream 从对象数组返回数据
- 类中的重载运算符 [],因此它可以从模板类中的数组返回对象
- 指向对象的指针数组返回jibberish
- 具有基本结构的结构数组返回错误的答案
- 数组返回乐趣 movie_check()
- C++从字符数组返回类型为T的指针
- C++-将数组返回到主函数,并将数组传递给另一个函数
- C++:如何通过异常处理从越界数组返回值
- 整数数组返回类型
- 从字符串流复制到 char * 数组返回为函数参数
- C++函数从对象数组返回对象的副本.这是默认行为吗?
- 8字节数组返回长(C#到C++)
- 向c#数组返回一个字符数组c++
- 从并行数组返回值,并在以后的函数中使用这些值
- 将c++中的浮点数数组返回给Python
- 如何在gsoap中使字符串数组返回值
- 将两个字符相乘并向数组返回int
- C++套接字 - 从结构数组返回单个地址信息