classes as record types classes and objects
play

Classes as Record Types Classes and Objects class Date { public: - PowerPoint PPT Presentation

Classes as Record Types Classes and Objects class Date { public: // access specifier int day; Wolfgang Schreiner char *month; Wolfgang.Schreiner@risc.jku.at } ; Research Institute for Symbolic Computation (RISC) Date date; // an object


  1. Classes as Record Types Classes and Objects class Date { public: // access specifier int day; Wolfgang Schreiner char *month; Wolfgang.Schreiner@risc.jku.at } ; Research Institute for Symbolic Computation (RISC) Date date; // an object Johannes Kepler University, Linz, Austria date.day = 24; http://www.risc.jku.at date.month = "December"; Date *dptr = new Date; // a pointer to an object dptr->day = 1; dptr->month = "January"; delete dptr; The keywords struct and class mean (almost) the same; however, class values are called “objects” (rather than “structures”). Wolfgang Schreiner http://www.risc.jku.at 1/70 Wolfgang Schreiner http://www.risc.jku.at 2/70 Classes as Namespaces // Date.h // Date.cpp class Date #include <iostream> 1. Classes as Namespaces { #include "Date.h" ... // declarations of static members // definitions of static data members 2. Classes as Object Types const int Date::thisDay; // data members char* Date::thisMonth = "January"; static const int thisDay = 1; 3. Objects with Functions static char* thisMonth; // definitions of static member functions void Date::print(Date *date) { // member functions std::cout << date->day << "/" static Date* create() { << date->month; 4. Objects and Arrays } Date* d = new Date; d->day = thisDay; d->month = thisMonth; // Main.cpp 5. Objects and Information Hiding return d; #include "Date.h" } int main(int argc, char* argv[]) { Date::thisMonth = "February"; 6. The Standard Class string static void print(Date *date); Date* d = Date::create(); } ; Date::print(d); return 0; } Wolfgang Schreiner http://www.risc.jku.at 3/70 Wolfgang Schreiner http://www.risc.jku.at 4/70

  2. Classes as Namespaces File Organization Classes can serve as namespaces. Typically there are two files related to a class Class . Static data members and member functions are “bound” to a class. They are also called class variables and class functions. File Class .h contains the class definition. There exists only one instance of the static members (independently Contains declarations of all members of a class. of the number of objects of the class to which the members belong). Must be included by every other file that wants to access members. Names of static members must be qualified by the class name. #include " Class .h" Class :: member If the class declaration changes, all files that include Class .h must Other static members in the same class may use the short name. be recompiled. A static data member is only declared in the class. File Class .cpp contains the corresponding member definitions. Must have a corresponding definition/initialization somewhere else. Must include Class .h Constant members of integral types may be initialized in class. A static member function may be also defined in the class. If some member definition changes, only this file must be recompiled (and the program must be relinked). Then the definition may be inlined at the point of every application. If not, there must exist a corresponding definition somewhere else. Any change to a member function that is defined in a class declaration Static member definitions outside of class must not use static . may cause a lot of recompilations. For global variables/functions, static means “internal linkage”. Use static members rather than variables/functions on namespace level. Wolfgang Schreiner http://www.risc.jku.at 5/70 Wolfgang Schreiner http://www.risc.jku.at 6/70 File Organization // C.h: declaration of C and its members #ifndef C_H_ 1. Classes as Namespaces #define C_H_ class C { 2. Classes as Object Types static T x; // declaration, no definition static T f(...); // declaration, no definition static T g(...) { ... } // declaration with definition 3. Objects with Functions } #endif /* C_H_ */ 4. Objects and Arrays // C.cpp: definitions of members of C #include "C.h" 5. Objects and Information Hiding T C::x; T C::f(...) { ... } 6. The Standard Class string // Main.cpp: use of C #include "C.h" int main() { ... C::x ... C::f(...) ... C::g( ...) ... } Wolfgang Schreiner http://www.risc.jku.at 7/70 Wolfgang Schreiner http://www.risc.jku.at 8/70

  3. Nonstatic Data Members Constructors class Date class Date // Date.h class Date // Date.h { { { public: ... ... int day; // inline declaration Date(int d, char *m); char *month; Date(int d, char *m) } ; } ; { day = d; Date::Date(int d, char *m) // Date.cpp Non-static data members “belong to” an object of the class. month = m; { They are also called object variables. } day = d; } ; month = m; For every object of the class, the exists a separate instance of the } object variable. Names of nonstatic data members must be qualified by an object. Date date(24, "December"); // calls Date(int, char*) object . member Date *dptr = new Date(26, "October"); // calls Date(int, char*) objectptr -> member A constructor is a method that initializes an object’s data members. Data members with access specifier public can be freely used like the variables of a structure. Wolfgang Schreiner http://www.risc.jku.at 9/70 Wolfgang Schreiner http://www.risc.jku.at 10/70 Constructors The this Pointer A constructor is a special method that is declared in a class. class Date // Date.h The constructor has the same name as the class. { It has no return type (also not void ). ... Date(int day, char* month); A constructor is bound to an object. } ; Called after the space for the object has been allocated. Executed in the context of the object. Date::Date(int day, char *month) // Date.cpp Can access all data members without qualification. { this->day = day; There may be multiple constructors with different argument types. this->month = month; Same constraints as for function overloading. } If defined inside the class, the constructor is inlined. The keyword this is a pointer to the current object. Same effect as if using the keyword inline . Can be used e.g. inside the body of a constructor. A constructor may execute arbitrary code. Can be used e.g. to resolve name ambiguities. Not only initializations of data members. We will see later the general rules for the use of this . Objects should be initialized by calling constructors. Wolfgang Schreiner http://www.risc.jku.at 11/70 Wolfgang Schreiner http://www.risc.jku.at 12/70

  4. The Default Constructor The Default Constructor class Date // Date.h class Date // Date.h A default constructor can be called without arguments. { { Is called for object declarations without initializers. ... ... Is called for initialization of array elements. Date() { Date(); Is called for initialization of non-static data members before a } ; day = 1; user-defined constructor is called. month = "January"; Is called for initializing static data members when program is started. } Date :: Date() { // Date.cpp } ; If a class has no user defined constructor, an implicit default day = 1; month = "January"; constructor is automatically generated. } Calls the default constructors of all non-static data members. If a class has user defined constructors, only these may be called. Date date; // calls Date() If some constructor is defined, also a default constructor has to Date date(); // WRONG: declares function date() (respectively should) be explicitly defined. Date *dptr1 = new Date; // calls Date() Without a default constructor, it is not possible to declare a variable Date *dptr2 = new Date(); // calls Date() of this type without initialization (and thus no arrays with this base Date darray[10]; // calls Date() for each object Date *darr = new Date[10]; // calls Date() for each object type can be created). All objects are initialized with the default constructor of their class. All objects are automatically initialized by constructors. Wolfgang Schreiner http://www.risc.jku.at 13/70 Wolfgang Schreiner http://www.risc.jku.at 14/70 Initialization Lists The Copy Constructor class Date // Date.h class Date // Date.h { { ... ... Date(const Date date&); Date(int d, char* m); } ; } ; // the copy constructor // explicit initialization of non-static data members Date::Date(const Date &date): day(date.day), month(date.month) { } Date::Date(int d, char *m): day(d), month(m) { static void print(Date date); // executed after data members have been initialized ... Date date0; // calls default constructor } Date date1(date0); // calls copy constructor Date date2 = date0; // calls copy constructor print(date0); // calls copy constructor The preferred way of initializing non-static data members (avoids calling return date0; // calls copy constructor their default constructors). An object duplicate is created with the copy constructor of the class. Wolfgang Schreiner http://www.risc.jku.at 15/70 Wolfgang Schreiner http://www.risc.jku.at 16/70

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend