On completion of this chapter students will know how to: Define a class, use an object, select class data members, select class function members, create a constructor function, create overloaded constructor functions.
Chapter 8 – Classes and Objects Advantages of Using Objects Classes form tightly knit group of functions Distinct program boundaries More reliable large programs Reduces amount of programming done “from scratch” Lesson 8.1 struct Data type created by grouping other data types Contains data members Defined by programmer – derived data type Declares space for all data members Lesson 8.1 Syntax struct fluid Name of struct Keywords { public: Data members char initial; double temp, density, velocity; }; Data type declaration for each data member Lesson 8.1 Declaring struct Variables Use struct name followed with names of variables Example: Fluid water, oil; type Lesson 8.1 variables Assigning Values to Data Members Use dot operator with variable name to store and access values Examples: water.density = 9.81; oil.density = water.density; variables of type fluid of struct data member Lesson 8.1 Class Class is a type – Aggregation of both data and functions General form: class Class_name { private: private_members; public: public_members; }; Lesson 8.2 Class Members Listed as in ordinary declarations – Type and identifier in sequence Class definition does NOT reserve memory for class members – Memory reserved when class declared Cannot use storage class specifiers: auto, extern or register Can be objects of other classes Lesson 8.2 Access Specifiers Private – Restricts access of data to member functions member data cannot be accessed directly from main Public – Allows member function to be called from main or any other function in program Lesson 8.2 Member Function Definitions Those not included inside class definition – Require class name and scope resolution operator (::) to immediately precede function name type class_name :: function header { type variable; statements return (value ); } Lesson 8.2 Objects New memory reserved for each data member of class when object declared Encapsulation – Link between data and functions of object data hiding: data access restricted to member functions – Classes create programming units with distinct boundaries Instantiation – Declaration of objects of a particular class (instances) Lesson 8.2 Function Calls Each member function called must be associated with an object Example: object_name . function_name – object_name called invoking object Function call ob1 . calc_area (left_limit, right_limit); double Parabola : : calc_area (double x1, double x2) Function header Lesson 8.2 Constructor Function Class member function executed automatically upon declaration of object Frequently used to initialize values of data members Can be called just Constructor Lesson 8.3 Constructor Characteristics Name identical to class name Does not return a value Has public access May or may not have arguments Lesson 8.3 Constructor Function Form class Name class name and { constructor function name identical private: type data_member; public: Name ( ); type function_member ( ) ; }; Lesson 8.3 Construction Function Definition Refers to class Refers to function Name : : Name ( ) { data_member = value; } Lesson 8.3 Constructor Functions Arguments Cannot return a value but can receive value through argument list Function can assign value to data member Passing a value to a constructor constructor function declaration Weather_station (int); Constructor function call Weather_station station (5); constructor_name (type); constructor function (value); Weather_station : : Weather_station (int wind_speed_var) Lesson 8.4 Initializing Data Member with Constructor Initialization list which follows single colon written after header – Data member name and temporary storage variable name (enclosed in parentheses) General Form temporary storage data member type variable name Class : : Class (type dummy) : data_member (dummy) , … data member set equal to value If more than one data member to initialize, separate with commas passed to dummy Lesson 8.4 Explicitly Calling a Constructor Function Cannot call like other member functions Can call constructor a second time for that object using an assignment statement station1 = Weather_station (10); Right side creates nameless object that is assigned to station1 Lesson 8.4 Overloading Constructor Function Can have two or more constructor functions in class definition Default copy constructor automatically executed when one object declared to be identical to another Distinguishable in terms of number or type of arguments Lesson 8.5 Overload Declarations Same name with different argument lists Example: Microwave_instruction ( ) ; Microwave_instruction (int); Microwave_instruction (int, int); Lesson 8.5 Summary Learned how to: Define a class Use an object Select class data members Select class function members Create a constructor function Create overloaded constructor functions ... Allows member function to be called from main or any other function in program Lesson 8.2 Member Function Definitions Those not included inside class definition – Require class name and scope resolution operator ... data hiding: data access restricted to member functions – Classes create programming units with distinct boundaries Instantiation – Declaration of objects of a particular class (instances) Lesson 8.2 Function Calls... Weather_station : : Weather_station (int wind_speed_var) Lesson 8.4 Initializing Data Member with Constructor Initialization list which follows single colon written after header – Data member name and temporary storage