/*************************************************/ // R. A. Hillyard // stuRecord01.cpp // November 2001 // // class example with public member functions // and private data member variables /*************************************************/ #include<iostream> #include<string> using namespace std; class StuRecord { public: //public members define the behavior of the object //functions to modify data members void input(); //input a stuRecord from input stream void set(int id, string fname, string lname); //set all values for stuRecord //functions to access data members void output(); //output a stuRecord int getIdNum(); //returns id number string getFirst(); //returns first name string getLast(); //returns last name private: //private members define the state of the object int idNum; //student id number string first; //student last name string last; //student first name }; /*************************************************/ int main() { //Declare 3 objects - give then identity StuRecord stu1, stu2, stu3; //example of calling mutator function - note different methods //mutator functions change the value of the calling object stu1.input(); stu2.input(); stu3.set(9999, "Analog", "Device"); //call member functions to output values stu1.output(); stu2.output(); stu3.output(); //example of calling accessor function //accessor functions do not change the value of the calling object if(stu3.getLast() == "Device") { cout << "First name is: " << stu3.getFirst() << endl; } return 0; }//end main /*************************************************/ /*************************************************/ // get data from user and set objects member variables // modifier function - sets state of object /*************************************************/ void StuRecord::input() { cout << "enter Id Number, first name and last name: "; cin >> idNum >> first >> last; } /*************************************************/ // print name data to screen /*************************************************/ void StuRecord::output() { cout << idNum << " " + first + " " + last << endl; } /*************************************************/ // set member variables to values in arguments // modifier function - sets state of object /*************************************************/ void StuRecord::set(int id, string fname, string lname) { idNum = id; first = fname; last = lname; } /*************************************************/ // accessor function for idNum member variable /*************************************************/ int StuRecord::getIdNum() { return idNum; } /*************************************************/ // accessor function for first member variable /*************************************************/ string StuRecord::getFirst() { return first; } /*************************************************/ // accessor function for last member variable /*************************************************/ string StuRecord::getLast() { return last; } /*************************************************/ /*****************Program Output***************** enter Id Number, first name and last name: 5546 Using Classes enter Id Number, first name and last name: 8765 Is Fun 5546 Using Classes 8765 Is Fun 9999 Analog Device First name is: Analog */