Defining class in header file and function in separate file
Just my practice. I/O a student's information.
//Header file(student.h) #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED #include <string> using namespace std; class Student { public: int outputStudentInfo(); int inputStudentInfo(); private: int studentIDNumber; string studentName; char studentSex; }; #endif // STUDENT_H_INCLUDED
//Class functions file(studentFunctions.cpp) #include <iostream> #include "student.h" using namespace std; int Student::inputStudentInfo() { cin >> studentIDNumber; cin >> studentName; cin >> studentSex; return 0; } int Student::outputStudentInfo() { cout << "Information you entered is:" <<endl; cout << "ID:" << studentIDNumber << endl; cout << "Name:" << studentName << endl; cout << "Sex:" << studentSex << endl; return 0; }
//main.cpp #include <ostream> #include "student.h" using namespace std; int main() { Student student; student.inputStudentInfo(); student.outputStudentInfo(); return 0; }
评论