// Ch. 12 Demo Program #4
// Mr. Minich
// purpose - to illustrate the use of a class
#include <iostream.h>
#include "M:\C++ Programming\AP classes\apstring.h"
class Student
{
public:
void GetNameAndGradeLevel();
private:
apstring name;
int gradeLevel;
char grade;
};
int main()
{
Student teachersPet;
Student classHacker;
teachersPet.GetNameAndGradeLevel();
classHacker.GetNameAndGradeLevel();
return 0;
}
void Student::GetNameAndGradeLevel()
{
cout << "Enter a name and grade level: ";
cin >> name;
cin >> gradeLevel;
}
// A class is used instead of a struct in order to combine the former getNameAndGradeLevel external function
// with the data (the member variables). The function is GetNameAndGradeLevel is a member along with the 3
// member variables. It is a member function, or more commonly called a method, of the class Student. The function
// prototype was removed from this program since a function prototype for the method is placed in the public
// partition of the class. Since the method is public, it can be called by programmers within a main function where
// there is a Student variable (i.e. object).
// Notice how the scope resolution operator ( :: ) is being used in the header to the method. This is necessary since
// C++ allows and even encourages OOP programmers to use the same function name for methods in related or inherited
// classes.
// teachersPet and classHacker are considered to be objects. They are instanciated (do not say "declared" in this case)
// at the top of the main function.
// Notice the familiar syntax of the call statements inside of the main function. Since GetNameAndGradeLevel is a method
// of the Student class, it is necessary to call this function by using the dot operator ( . ) with the object variable name in
// the same way that we have used statements like name.length(); where name is a string object (or informally called a
// variable).
// It is very common for OOP programs to hide all of a class' data (i.e. member variables) by placing them in the private partition.
// Then the program creates methods (i.e. member functions) to get, input, and modify that data. This is considered to be safe and
// efficient object-oriented programming.