Electrical Engineering - Chapter 8: Classes and Objects

Classes form tightly knit group of functions Distinct program boundaries More reliable large programs Reduces amount of programming done “from scratch”

ppt22 trang | Chia sẻ: thuongdt324 | Lượt xem: 407 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Electrical Engineering - Chapter 8: Classes and Objects, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chapter 8 – Classes and ObjectsAdvantages of Using ObjectsClasses form tightly knit group of functionsDistinct program boundariesMore reliable large programsReduces amount of programming done “from scratch”Lesson 8.1structData type created by grouping other data typesContains data membersDefined by programmer – derived data typeDeclares space for all data membersLesson 8.1Syntaxstruct fluid{ public: char initial; double temp, density, velocity;};KeywordsName of structData membersData type declaration for each data memberLesson 8.1Declaring struct VariablesUse struct name followed with names of variablesExample: Fluid water, oil;typevariablesLesson 8.1Assigning Values to Data MembersUse dot operator with variable name to store and access valuesExamples: water.density = 9.81; oil.density = water.density;variables of type fluiddata member of structLesson 8.1ClassClass is a typeAggregation of both data and functionsGeneral form:Lesson 8.2class Class_name{ private: private_members; public: public_members;};Class MembersListed as in ordinary declarationsType and identifier in sequenceClass definition does NOT reserve memory for class membersMemory reserved when class declaredCannot use storage class specifiers: auto, extern or registerCan be objects of other classesLesson 8.2Access SpecifiersPrivateRestricts access of data to member functionsmember data cannot be accessed directly from mainPublicAllows member function to be called from main or any other function in programLesson 8.2Member Function DefinitionsThose not included inside class definitionRequire class name and scope resolution operator (::) to immediately precede function nameLesson 8.2type class_name :: function header { type variable; statements return (value );}ObjectsNew memory reserved for each data member of class when object declaredEncapsulationLink between data and functions of objectdata hiding: data access restricted to member functionsClasses create programming units with distinct boundariesInstantiationDeclaration of objects of a particular class (instances)Lesson 8.2Function CallsEach member function called must be associated with an objectExample: object_name . function_nameobject_name called invoking object ob1 . calc_area (left_limit, right_limit);double Parabola : : calc_area (double x1, double x2)Function callFunction headerLesson 8.2Constructor FunctionClass member function executed automatically upon declaration of objectFrequently used to initialize values of data membersCan be called just ConstructorLesson 8.3Constructor CharacteristicsName identical to class nameDoes not return a valueHas public accessMay or may not have argumentsLesson 8.3Constructor Function FormLesson 8.3class Name{ private: type data_member; public: Name ( ); type function_member ( ) ;};class name andconstructor functionname identicalConstruction Function DefinitionLesson 8.3Name : : Name ( ){ data_member = value;}Refers to class Refers to function Constructor Functions - ArgumentsCannot return a value but can receive value through argument listFunction can assign value to data memberPassing a value to a constructorLesson 8.4Weather_station (int);Weather_station station (5);Weather_station : : Weather_station (int wind_speed_var)constructor function declarationconstructor_name (type);Constructor function callconstructor function (value);Initializing Data Member with ConstructorInitialization list which follows single colon written after headerData member name and temporary storage variable name (enclosed in parentheses)General FormLesson 8.4Class : : Class (type dummy) : data_member (dummy)data member set equal to value passed to dummydata member typetemporary storagevariable nameIf more than one data member to initialize, separate with commas, Explicitly Calling a Constructor FunctionCannot call like other member functionsCan call constructor a second time for that object using an assignment statementLesson 8.4station1 = Weather_station (10);Right side creates nameless object that is assigned to station1Overloading Constructor FunctionCan have two or more constructor functions in class definitionDefault copy constructor automatically executed when one object declared to be identical to anotherDistinguishable in terms of number or type of argumentsLesson 8.5Overload DeclarationsSame name with different argument listsExample:Lesson 8.5Microwave_instruction ( ) ;Microwave_instruction (int);Microwave_instruction (int, int);SummaryDefine a classUse an objectSelect class data membersSelect class function membersCreate a constructor functionCreate overloaded constructor functionsLearned how to: