// Chapter 5 - Program 2 #include class class_name { int private_variable;//object--can only be altered and accessed by //functions/methods within this class. public://following can be used by all. void set_value(int input_variable); //first "method" within this class--*only* //means of changing private_variable! the "void" means it returns no value, and //the (int input_variable) means it receives an int variable from caller. int get_value(void);// receives nothing from caller, but returns integer value //of private_variable! **only** means of getting to private_variable! }; //define what the methods of the class "class_name" do. //note the name of the class "prepended" to method names. void class_name::set_value(int input_variable)//receives input_variable from //caller. { private_variable = input_variable;//assigns input_variable value to //private_variable. again, *only* means of altering //value of private_variable! } int class_name::get_value(void)//receives no input_variable--but returns value //of private_variable onwhen called. again, *only* means of returning //value of private_variable! { return private_variable;//returns value to caller. } main() { class_name dog1, dog2, dog3;//declare 3 "objects" of class named "class_name". int piggy; //to access/call an method, use the "object's name, and the desired "method", //seperated by a single dot, just like a structure call: dog1.set_value(12);//passing an input_value to method "set_value. //this is known as "sending a message" to that class. dog2.set_value(17); dog3.set_value(-13); piggy = 123; //retrieving private_variable's current value by sending a "message" to the //get_value() method/function. cout << "The value of dog1 is " << dog1.get_value() << "\n"; cout << "The value of dog2 is " << dog2.get_value() << "\n"; cout << "The value of dog3 is " << dog3.get_value() << "\n"; cout << "The value of piggy is " << piggy << "\n"; } // Result of execution // // The value of dog1 is 12 // The value of dog2 is 17 // The value of dog3 is -13 // The value of piggy is 123