A Complete Guide to Programming in C++ part 28 pdf

10 386 0
A Complete Guide to Programming in C++ part 28 pdf

Đang tải... (xem toàn văn)

Thông tin tài liệu

DEFINING METHODS ■ 249 A class definition is not complete without method definitions. Only then can the objects of the class be used. ᮀ Syntax When you define a method, you must also supply the class name, separating it from the function name by means of the scope resolution operator ::. Syntax: type class_name::function_name(parameter_list) { . . . } Failure to supply the class name results in a global function definition. Within a method, all the members of a class can be designated directly using their names. The class membership is automatically assumed. In particular, methods belonging to the same class can call each other directly. Access to private members is only possible within methods belonging to the same class. Thus, private members are completely controlled by the class. Defining a class does not automatically allocate memory for the data members of that class. To allocate memory, you must define an object. When a method is called for a given object, the method can then manipulate the data of this object. ᮀ Modular Programming A class is normally defined in several source files. In this case, you will need to place the class definition in a header file. If you place the definition of the class Account in the file Account.h, any source file including the header file can use the class Account. Methods must always be defined within a source file. This would mean defining the methods for the class Account in a source file named Account.cpp, for example. The source code of the application program, for example, the code containing the function main, is independent of the class and can be stored in separate source files. Sep- arating classes from application programs facilitates re-use of classes. In an integrated development environment, a programmer will define a project to help manage the various program modules by inserting all the source files into the project. When the project is compiled and linked, modified source files are automatically re-com- piled and linked to the application program. 250 ■ CHAPTER 13 DEFINING CLASSES "Cheers, Mary" 1234567 2002.22 "Dylan, Bob" 87654321 –1300.13 current name nr balance savings name nr balance ■ DEFINING OBJECTS The objects current and savings in memory DEFINING OBJECTS ■ 251 Defining a class also defines a new type for which variables, that is, objects, can be defined. An object is also referred to as an instance of a class. ᮀ Defining Objects An object is defined in the usual way by supplying the type and the object name. Syntax: class_name object_name1 [, object_name2, ] The following statement defines an object current of type Account: Example: Account current; // or: class Account Memory is now allocated for the data members of the current object. The current object itself contains the members name, nr, and balance. ᮀ Objects in Memory If multiple objects of the same class type are declared, as in Example: Account current, savings; each object has its own data members. Even the object savings contains the members name, nr, and balance. However, these data members occupy a different position in memory than the data members belonging to current. The same methods are called for both objects. Only one instance of the machine code for a method exists in memory—this applies even if no objects have been defined for the class. A method is always called for a particular instance and then manipulates the data members of this object. This results in the memory content as shown on the opposite page, when the method init() is called for each object with the values shown. ᮀ Initializing Objects The objects belonging to the Account class were originally defined but not initialized. Each member object is thus defined but not explicitly initialized. The string name, is empty, as it is thus defined in the class string. The initial values of the members nr and balance are unknown, however. As is the case for other variables, these data mem- bers will default to 0 if the object is declared global or static. You can define exactly how an object is created and destroyed. These tasks are per- formed by constructors and destructors. Constructors are specifically responsible for initial- izing objects—more details are given later. 252 ■ CHAPTER 13 DEFINING CLASSES // account_t.cpp // Uses objects of class Account. // #include "Account.h" int main() { Account current1, current2; current1.init("Cheers, Mary", 1234567, -1200.99); current1.display(); // current1.balance += 100; // Error: private member current2 = current1; // ok: Assignment of // objects is possible. current2.display(); // ok // New values for current2 current2.init("Jones, Tom", 3512347, 199.40); current2.display(); // To use a reference: Account& mtr = current1; // mtr is an alias name // for object current1. mtr.display(); // mtr can be used just // as object current1. return 0; } ■ USING OBJECTS Sample program USING OBJECTS ■ 253 ᮀ Class Member Access Operator An application program that manipulates the objects of a class can access only the pub- lic members of those objects. To do so, it uses the class member access operator (in short: dot operator). Syntax: object.member Where member is a data member or a method. Example: Account current; current.init("Jones, Tom",1234567,-1200.99); The expression current.init represents the public method init of the Account class. This method is called with three arguments for current. The init() call cannot be replaced by direct assignments. Example: current.name = "Dylan, Bob"; // Error: current.nr = 1234567; // private current.balance = -1200.99; // members Access to the private members of an object is not permissible outside the class. It is therefore impossible to display single members of the Account class on screen. Example: cout << current.balance; // Error current.display(); // ok The method display() displays all the data members of current. A method such as display() can only be called for one object. The statement display(); would result in an error message, since there is no global function called display(). What data would the function have to display? ᮀ Assigning Objects The assignment operator = is the only operator that is defined for all classes by default. However, the source and target objects must both belong to the same class. The assign- ment is performed to assign the individual data members of the source object to the cor- responding members of the target object. Example: Account current1, current2; current2.init("Marley, Bob",350123, 1000.0); current1 = current2; This copies the data members of current2 to the corresponding members of current1. 254 ■ CHAPTER 13 DEFINING CLASSES // ptrObj.cpp // Uses pointers to objects of class Account. // #include "Account.h" // Includes <iostream>, <string> bool getAccount( Account *pAccount); // Prototype int main() { Account current1, current2, *ptr = &current1; ptr->init("Cheer, Mary", // current1.init( ) 3512345, 99.40); ptr->display(); // current1.display() ptr = &current2; // Let ptr point to current2 if( getAccount( ptr)) // Input and output a new ptr->display(); // account. else cout << "Invalid input!" << endl; return 0; } // // getAccount() reads data for a new account // and adds it into the argument. bool getAccount( Account *pAccount ) { string name, line(50,'-'); // Local variables unsigned long nr; double startcapital; cout << line << '\n' << "Enter data for a new account: \n" << "Account holder: "; if( !getline(cin,name) || name.size() == 0) return false; cout << "Account number: "; if( !(cin >> nr)) return false; cout << "Starting capital: "; if( !(cin >> startcapital)) return false; // All input ok pAccount->init( name, nr, startcapital); return true; } ■ POINTERS TO OBJECTS Sample program POINTERS TO OBJECTS ■ 255 An object of a class has a memory address—just like any other object. You can assign this address to a suitable pointer. Example: Account savings("Mac, Rita",654321, 123.5); Account *ptrAccount = &savings; This defines the object savings and a pointer variable called ptrAccount. The pointer ptrAccount is initialized so that it points to the object savings. This makes *ptrAccount the object savings itself. You can then use the statement Example: (*ptrAccount).display(); to call the method display() for the object savings. Parentheses must be used in this case, as the operator . has higher precedence than the * operator. ᮀ Arrow Operator You can use the class member access operator -> (in short: arrow operator) instead of a combination of * and . . Syntax: objectPointer->member This expression is equivalent to (*objectPointer).member The operator -> is made up of a minus sign and the greater than sign. Example: ptrAccount->display(); This statement calls the method display() for the object referenced by ptrAccount, that is, for the object savings. The statement is equivalent to the statement in the pre- vious example. The difference between the class member access operators . and -> is that the left operand of the dot operator must be an object, whereas the left operand of the arrow operator must be a pointer to an object. ᮀ The Sample Program Pointers to objects are often used as function parameters. A function that gets the address of an object as an argument can manipulate the referenced object directly. The example on the opposite page illustrates this point. It uses the function getAccount() to read the data for a new account. When called, the address of the account is passed: getAccount(ptr) // or: getAccount(&current1) The function can then use the pointer ptr and the init() method to write new data to the referenced object. 256 ■ CHAPTER 13 DEFINING CLASSES // structs.cpp // Defines and uses a struct. // #include <iostream> #include <iomanip> #include <string> using namespace std; struct Representative // Defining struct Representative { string name; // Name of a representative. double sales; // Sales per month. }; inline void print( const Representative& v) { cout << fixed << setprecision(2) << left << setw(20) << v.name << right << setw(10) << v.sales << endl; } int main() { Representative rita, john; rita.name = "Strom, Rita"; rita.sales = 37000.37; john.name = "Quick, John"; john.sales = 23001.23; rita.sales += 1700.11; // More Sales cout << " Representative Sales\n" << " " << endl; print( rita); print( john); cout << "\nTotal of sales: " << rita.sales + john.sales << endl; Representative *ptr = &john; // Pointer ptr. // Who gets the if( john.sales < rita.sales) // most sales? ptr = &rita; cout << "\nSalesman of the month: " << ptr->name << endl; // Representative's name // pointed to by ptr. return 0; } ■ structs Sample program structs ■ 257 ᮀ Records In a classical, procedural language like C, multiple data that belong together logically are put together to form a record. Extensive data such as the data for the articles in an auto- mobile manufacturer’s stocks can be organized for ease of viewing and stored in files. From the viewpoint of an object-oriented language, a record is merely a class contain- ing only public data members and no methods. Thus, you can use the class keyword to define the structure of a record in C++. Example: class Date { public: short month, day, year; }; However, it is common practice to use the keyword struct, which is also available in the C programming language, to define records. The above definition of Date with the members day, month, and year is thus equivalent to: Example: struct Date { short month, day, year; }; ᮀ The Keywords class and struct You can also use the keyword struct to define a class, such as the class Account. Example: struct Account { private: // . . . as before public: // . . . }; The keywords class and struct only vary with respect to data encapsulation; the default for access to members of a class defined as a struct is public. In contrast to a class defined using the class keyword, all the class members are public unless a pri- vate label is used. This allows the programmer to retain C compatibility. Example: Date future; future.year = 2100; // ok! Public data Records in the true sense of the word, that is, objects of a class containing only pub- lic members, can be initialized by means of a list during definition. Example: Date birthday = { 1, 29, 1987}; The first element in the list initializes the first data member of the object, and so on. 258 ■ CHAPTER 13 DEFINING CLASSES w (16 bit word) Low byte b[0] High byte b[1] // unions.cpp // Defines and uses a union. // #include <iostream> using namespace std; union WordByte { private: unsigned short w; // 16 bits unsigned char b[2]; // Two bytes: b[0], b[1] public: // Word- and byte-access: unsigned short& word() { return w; } unsigned char& lowByte() { return b[0]; } unsigned char& highByte(){ return b[1]; } }; int main() { WordByte wb; wb.word() = 256; cout << "\nWord: " << (int)wb.word(); cout << "\nLow-byte: " << (int)wb.lowByte() << "\nHigh-byte: " << (int)wb.highByte() << endl; return 0; } ■ UNIONS An object of union WordByte in memory Defining and using union WordByte Screen output of the program Word: 256 Low-Byte: 0 High-Byte: 1 . Bob" 87654321 –1300.13 current name nr balance savings name nr balance ■ DEFINING OBJECTS The objects current and savings in memory DEFINING OBJECTS ■ 251 Defining a class also defines a new type for which variables, that is,. method can then manipulate the data of this object. ᮀ Modular Programming A class is normally defined in several source files. In this case, you will need to place the class definition in a header. objects, can be defined. An object is also referred to as an instance of a class. ᮀ Defining Objects An object is defined in the usual way by supplying the type and the object name. Syntax: class_name

Ngày đăng: 06/07/2014, 17:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan