/*******************************************************/ //R.A. Hillyard //classIntro2.cpp //Last Modified 11/11/2001 // // class with member variables and member functions // for input and output /*******************************************************/ #include<iostream> #include<string> using namespace std; class Name //class definition goes before main { public: //member functions void output(); void input(); //mutator function to set data member values void setName(string first, string last); //accessor functions to get data member values string getFirst(); string getLast(); private: //member data variables string first; string last; }; int main() { Name name1, name2, name3; //declare 3 objects of type Name name1.input(); name2.input(); //when member data variables are declared private //we can no longer access them directly //name3.first = "Analog"; //name3.last = "Device"; //instead - use member function to set values name3.setName("Analog", "Device"); name1.output(); name2.output(); name3.output(); //use member function to get data member values if(name3.getLast() == "Device") {cout << "First name is : " << name3.getFirst() << endl;} return 0; } /************************************************/ void Name::output() { cout << first + " " + last << endl; } /************************************************/ void Name::input() { cout << "Enter a first name and last name: "; cin >> first >> last; } /************************************************/ void Name::setName(string fname, string lname) { first = fname; last = lname; } /************************************************/ string Name::getFirst() { return first; } /************************************************/ string Name::getLast() { return last; } /************************************************/ /**************Program Output********************/ Enter a first name and last name: Bob Dingle Enter a first name and last name: Kelly Lee Bob Dingle Kelly Lee Analog Device First name is : Analog