使用类和函数c ++ [英] Using classes and functions c++

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

问题描述

STUDENT.H



  #ifndef STUDENT_H 
#define STUDENT_H
#include < string >
使用 命名空间标准;

class 学生
{
私人
int studentNumber;
string studentName;
double GPA;
public
Students();
void setStudentNumber( int );
void setStudentName(string);
void setGPA( double );
string getStudentName();
int getStudentNumber();
void displayGPA();
};

#endif



STUDENT.CPP



  #include     student.h 
#include < span class =code-preprocessor> < iostream >
使用 命名空间标准;

学生::学生()
{
studentNumber = 0 ;
studentName = - ;
GPA = 0 0 ;
}

void Students :: setStudentNumber( int studentNumber)
{
studentNumber = studentNumber;
}

void Students :: setStudentName(string studentName)
{
studentName = studentName;
}

void 学生:: setGPA( double GPA)
{
GPA = GPA;
}

字符串Students :: getStudentName()
{
return studentName;
}

int Students :: getStudentNumber()
{
返回 studentNumber;
}

void 学生:: displayGPA()
{
cout<< GPA;
}



STUDENTCLASS.CPP



  #include     student。 h 
#include < iostream >
#include < iomanip >
#include < string >
使用 命名空间标准;

int main()
{
学生student1;
学生[ 3 ];

cout<< ------------------------- -----------------------------------<< endl
<< --------------------单个对象 - ----------------------------<< ENDL;
cout<< setw( 5 )<<左<< student1.getStudentName()
<< setw( 5 )<<左<< student1.getStudentNumber()
<< student1.displayGPA;

student1.setStudentName( John Doe);
student1.setStudentNumber( 1 );
student1.setGPA( 3 5 );

cout<< ------------------------- -----------------------------------<< endl
<< Student1现在有信息:;
cout<< setw( 10 )<<左<< student1.getStudentName()
<< setw( 10 )<<左<< student1.getStudentNumber()
<< setw( 10 )<<左<< student1.displayGPA;

cout<< ------------------------- -----------------------------------<< endl
<< --------------------一个对象数组 - ----------------------<< ENDL;

for int x = 0 ; x< 3 ; x ++)
{
cout<< 请输入学生信息<< x<< << ENDL;
cout<< 名称:;
cin>>学生[X] .setStudentName;

while (students [x] .getStudentName()== < span class =code-string> - )
{
cout<< 输入无效!;
cout<< 名称:;
cin>>学生[X] .setStudentName;
}

cout<< ID:;
cin>>学生[X] .setStudentNumber;

while (students [x] .getStudentNumber()< 1
{
cout<< 输入无效!;
cout<< ID:;
cin>>学生[X] .setStudentNumber;
}

cout<< GPA:;
cin>>学生[X] .setGPA;

while (students [x] .displayGPA == 0
{
cout<< 输入无效!;
cout<< GPA:;
cin>>学生[X] .setGPA;
}
}

cout<< ------------------------- -----------------------------------<< endl
<< setw( 10 )<<左<< 名称
<< setw( 10 )<<左<< Number
<< setw( 10 )<<左<< GPA<< ENDL;

for int x = 0 ; x< 3 ; x ++)
{
cout<<学生[X] .getStudentName();
cout<<学生[X] .getStudentNumber();
cout<<学生[X] .displayGPA;
}

return 0 ;
}



请帮助...我一直在使用displayGPA函数和二进制错误>>与cin不起作用。任何指导都将非常感谢。我的老师显然没有回复她的电子邮件,我迫切需要帮助。

解决方案

此功能

 void学生:: displayGPA()
{
cout<< GPA;
}





应该是



 double Students :: displayGPA()
{
返回GPA;
}


1。为什么displayGPA()? 只需将其更改为

  double  getGPA()

.getStudentNumber();
学生[x] .displayGPA;
cout<< ENDL;
}





2.你需要临时变量才能使用cin。



eg



 string studentName; 

cin>>学生姓名;

学生[x] .setStudentName(studentName);





这需要一些重做,但一旦完成就会回来。


Quote:

<< setw(10)<<左<< student1.displayGPA;



如上所述,您无法将对displayGPA的调用链接到输出流中。这就产生了一个问题:你显然想要一种特定的格式,但即使你正确地调用了displayGPA(),格式规范也是在学生班级之外完成的,并且在显示功能中会被遗忘!



您声明您的说明要求displayGPA()返回void。鉴于此,如果你想要那种格式。你需要找到另一种方式:

1.将格式规范(对齐,间距)作为附加参数传递给displayGPA()函数并在函数中应用格式

2.添加另一个函数来指定任何显示函数的格式,并将格式说明符存储在学生班级中(不是一个很好的解决方案imho。学生班不应该关注显示格式信息)

3.添加另一个函数直接获取GPA值而不是打印(唯一合理的解决方案imho)



引用:

while(students [x] .getStudentName()== - )



以下是一些重大问题:

1.您专门检查学生班级中定义的默认值。假设函数实现在不同的源文件中,或者甚至只能以二进制格式作为lib或dll使用,那么使用类的程序通常如何知道该值?

1.a)你也将默认值与无效输入相提并论,这是完全错误的:你的循环将接受任何不是 - 的输入,即使是一个空字符串,一个包含字符串的字符串数字或其他非字母字符,所有这些都显然无效!

2.你要求输入的方式和检查它的有效性只是学生班需要知道的事情,而不是你的主程序!这显然属于学生班级的代码

3.您(尝试)直接将来自cin的输入流式传输到变量中。除了代码不正确之外,您通常不应直接将输入流式传输到最终目标变量。你怎么知道输入是一个有效的字符串?你怎么知道它不会以某种方式引起问题?您是否愿意在没有检查的情况下用完整的乱码覆盖现有的有效值?更好的方法是将输入读入临时变量,检查该变量的有效性,然后将其复制到真正的目标。同样,只有学生班需要知道如何最好地处理它,并提供从输入流中读取这些值的功能。



引用:

while(students [x] .getStudentNumber()< 1)



同样的问题:你正在使用内部知识学生班,我。即值<< 1无效。将该代码留给学生班!提供阅读学号并正确检查的功能。此外,课程都是关于可重用性的。可以在程序内的数十个或数百个位置使用实现良好的类。如果将来某个时候学生格式改变为允许使用字母数字ID代替简单数字,您是想更改数百个地方的代码 - 还是仅仅在学生班级内? ;)

引用:

while(students [x] .displayGPA == 0)



我是否需要提及这与上述相同的问题?更重要的是,对函数displayGPA()的指令限制使得无法实际检查有效输入!再次,将该代码放在学生班的一个函数中,你没事。





摘要:

1.向学生班级添加GetGPA()函数

2.为学生班级编写成员函数,从cin中读取自己的成员变量,并在这些函数中进行所有查询和验证

3.始终使用临时变量来保存来自cin的输入。在将它们复制到实际目的地之前检查。



(你可能还想插入一些<< endl;))


STUDENT.H

#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;

class Students
{
private:
    int studentNumber;
    string studentName;
    double GPA;
public:
    Students();
    void setStudentNumber(int);
    void setStudentName(string);
    void setGPA(double);
    string getStudentName();
    int getStudentNumber();
    void displayGPA();
};

#endif


STUDENT.CPP

#include "student.h"
#include <iostream>
using namespace std;

Students::Students()
{
    studentNumber = 0;
    studentName = "-";
    GPA = 0.0;
}

void Students::setStudentNumber(int studentNumber)
{
    studentNumber = studentNumber;
}

void Students::setStudentName(string studentName)
{
    studentName = studentName;
}

void Students::setGPA(double GPA)
{
    GPA = GPA;
}

string Students::getStudentName()
{
    return studentName;
}

int Students::getStudentNumber()
{
    return studentNumber;
}

void Students::displayGPA()
{
    cout << GPA;
}


STUDENTCLASS.CPP

#include "student.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    Students student1;
    Students students[3];

    cout << "------------------------------------------------------------" << endl
         << "--------------------A Single Object------------------------------" << endl;
    cout << setw(5) << left << student1.getStudentName() 
     << setw(5) << left << student1.getStudentNumber()
         << student1.displayGPA;

    student1.setStudentName("John Doe");
    student1.setStudentNumber(1);
    student1.setGPA(3.5);

    cout << "------------------------------------------------------------" << endl
     << "Student1 now has information:";
    cout << setw(10) << left << student1.getStudentName()
     << setw(10) << left << student1.getStudentNumber()
     << setw(10) << left << student1.displayGPA;

cout << "------------------------------------------------------------" << endl
     << "--------------------An Array of Objects-----------------------" << endl;

for(int x = 0; x < 3; x++)
{
    cout << "Please enter information for student" << x << ":" << endl;
    cout << "Name:";
    cin >> students[x].setStudentName;

    while(students[x].getStudentName() == "-")
    {
        cout << "Invalid input!";
        cout << "Name:";
        cin >> students[x].setStudentName;
    }

    cout << "ID: ";
    cin >> students[x].setStudentNumber;

    while(students[x].getStudentNumber() < 1)
    {
        cout << "Invalid input!";
        cout << "ID: ";
        cin >> students[x].setStudentNumber;
    }

    cout << "GPA: ";
    cin >> students[x].setGPA;

    while(students[x].displayGPA == 0)
    {
        cout << "Invalid input!";
        cout << "GPA: ";
        cin >> students[x].setGPA;
    }
}

cout << "------------------------------------------------------------" << endl
     << setw(10) << left << "Name"
     << setw(10) << left << "Number" 
     << setw(10) << left << "GPA" << endl;

for(int x = 0; x < 3; x++)
{
    cout << students[x].getStudentName();
    cout << students[x].getStudentNumber();
    cout << students[x].displayGPA;
}

return 0;
}


Please help... I keep getting errors with the displayGPA function and a binary error that >> with the cin does not work. Any guidance would be much appreciated. My teacher apparently doesn't answer her email and I desperately need help.

解决方案

This function

void Students::displayGPA()
{
    cout << GPA;
}



should be

double Students::displayGPA()
{
    return GPA;
}


1. Why displayGPA()? Just change that to

double getGPA()

[Edit} OK keep displayGPA()

cout << displayGPA()

will result in:

cout << (cout << GPA);


just do:

for(int x = 0; x < 3; x++)
{
    cout << students[x].getStudentName();
    cout << students[x].getStudentNumber();
    students[x].displayGPA;
    cout << endl;
}



2. You will need temporary variables to use cin.

e.g.

 string studentName;

cin >> studentName;

students[x].setStudentName(studentName);



That will require some reworking but come back once that is done.


Quote:

<< setw(10) << left << student1.displayGPA;


As pointed out above, you can't chain the call to displayGPA into the output stream like that. This poses a problem: you obviously want a specific format, but even if you call displayGPA() correctly, the format specification is done outside the student class, and will be forgotten within the display function!

You stated that your instructions require that displayGPA() returns void. Given that, if you want that formatting. you need to find another way:
1. pass the format specification (alignment, spacing) as additional parameters to your displayGPA() function and apply the formatting within the function
2. add another function to specify the formatting for any display function and store the formatting specifiers within your student class (not a great solution imho. The student class shouldn't have to concern itself with display format info)
3. Add another function to get the GPA value directly rather than having it printed (the only reasonable solution imho)

Quote:

while(students[x].getStudentName() == "-")


Here are some big issues:
1. you are specifically checking for a default value defined within the student class. How is the program using a class supposed to know of that value in general, assuming that the function implementation is in a different source file, or may even only be vailable in binary format as a lib or dll?
1.a) you are also equating that default with an invalid input, which is plain wrong: your loop will accept any input that isn't "-", even an empty string, a string containg numbers or other non-alphabetic characters, all of which are clearly invalid!
2. the way you ask for input and check it's validity are things that only the student class should need to know about, not your main program! This is clearly code that belongs into the student class
3. You (try to) directly stream input from cin into a variable. Apart from the fact that the code is incorrect, you should normally never directly stream input into the final destination variable. How do you know the input is a valid string? How do you know it doesn't somehow cause an issue? Are you willing to overwrite an existing valid value with one that is complete gibberish without checking? A better way is to read the input into a temporary variable, check the validity of that variable, and only then copy it to the true destination. Again, only the student class should need to know how to best handle that, and provide functions for reading these values from an input stream.

Quote:

while(students[x].getStudentNumber() < 1)


Same problem: you are using internal knowledge of the student class, i. e. that a value < 1 is invalid. Leave that code to the student class! Provide a function for reading the student number and checking it properly. Besides, classes are all about reusability. A well implemented class may be used in dozens or hundreds of places within a program. If some time in the future the student format changes to allow for alphanumeric IDs in place of simple numbers, would you rather change your code in hundreds of places - or just within the student class? ;)

Quote:

while(students[x].displayGPA == 0)


Do I need to mention this is the same problem as above? What's more, the restrictionsin your instructions on the function displayGPA() make it impossible to actually check a valid input! Again, put that code inside a function in the student class, and you're fine.


Summary:
1. Add a GetGPA() function to your student class
2. Write member functions for your student class to read their own member variables from cin and do all the querying and validation within those functions
3. Always use temporary variables to hold input from cin. Check before copying them over to the actual destination.

(and you might also want to insert a few more <<endl ;) )


这篇关于使用类和函数c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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