1 Writing a Good Program 5 Objects and

1 Writing a Good Program 5. Objects and Classes in C++

2 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ 5. 3 Modular Programming with C++

3 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Doing Business by Selling Classes • The development of Object Oriented Programming has enabled a new business in software development. People develop different classes and sell to other people. • These classes often provide functions that are popularly used in applications. For example, the Java calculator used in some Web pages • People post the advertisement of their classes on the Web. Those who are interested in their classes can directly download it and perhaps pay by credit card.

4 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ • Assume that you have designed the following class CAT and would like to sell it to a customer. #include <iostream> // for cout using namespace std; class Cat // declare the class object { public: void Set. Age (int age); int Get. Age(); void Set. Weight (int weight); int Get. Weight(); private: int its. Age; • You do not want to give him int its. Weight; the source codes of the member }; int Cat: : Get. Age() { return its. Age; } void Cat: : Set. Age(int age) { its. Age = age; } functions since your customer may copy your codes. • They may modify it and re-sell it to somebody else.

5 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ • If the customer has the source code, he may try to modify it by himself. • It may introduce a lot of hidden errors as he knows very little about your program. • The resulting program can be very difficult to debug. Conclusion: It is better to give your customer executable codes such that they cannot read or modify it. Problem 1: A program without main() cannot be built. No executable codes can be generated without main(). Problem 2: If your customer only has the executable codes, how can he know the way to use your class?

6 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Static Library Header library • Rather than giving your customer the executable codes, you can give him a (static) library. • Inside a library, it does not contain the executable codes, but the object codes – machine code waiting for linking. • Since object codes cannot be read directly, your customer cannot modify your codes. • To enable your customer to know how to use your class, give also your customer a header file that contains the definition of your class.

7 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Cat. Class. h main() of the Customer Main Program Main. Project. cpp #include "Cat. Class. h" class CAT { public: int Get. Age(); void main() { : : : }; CAT Frisky; Age = Frisky. Get. Age(); } : Library 10 20 2 d 35 5 f 43 23 … : When the customer builds the main(), it links with Cat. Class. h and the library. Explain why public and private Show that the class CAT has a public function called Get. Age() It will return an integer. … 22 6 f … 21 44 dd 23 Contain the implementation of Get. Age(), however, the source code cannot be seen by the customer.

8 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Modular Program Design • Besides doing business with classes, the use of static library also facilitates modular program design. • The system analyst analyses the requirement of a program and divides it into a number of modules. • The specifications of each module, such as its functions, the calling methods, the parameters returned, are well defined. • Based on the specifications, the development of each module will be done by different programmers. Very useful for team work

9 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Example – step 1: obtain the requirements • The system analyst talks with the customer and obtains the following program requirements: • A program is to be designed to show a zoo that contains different kind of animals, including dog, cat, sheep, and horse. • At the beginning of the program, all animals are drawn on the screen. • When user clicks on an animal, the sound of it is played.

10 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Example – step 2: design the program flow • Based on the requirements, the system analyst designs the program flow using, for example, a flow chart. Start Draw animal No All animal drawn? Yes B No User click on animal? Yes A

11 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ A Click Dog? Yes Play dog sound No Click Cat? Yes Play cat sound No Click Sheep? Yes Play sheep sound No Click Horse? Yes Play horse sound No Click End? No Yes End B

12 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Example – step 3: divide into modules • From the flow chart, identify the classes required and their functions. Cat. Class. h Horse. Class. h Dog. Class. h class CAT Sheep. Class. h class HORSE class DOG { public: { class SHEEP int Draw. Shape(); public: { int Play. Sound(); int Draw. Shape(); public: int Play. Sound(); }; int Draw. Shape(); }; int Play. Sound(); }; { : : }; :

13 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Example – step 4: ask programmers to develop the modules • For each module, the system analyst will ask a programmer to develop the codes required (to implement the class). • To speed up the process, the system analyst may ask 4 programmers to develop the modules simultaneously. • The codes developed by the 4 programmers may be different. • If the specifications are correct, all modules are correct irrespective to the differences in the codes. • Hence facilitate team work.

14 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Example – step 5: integrate the modules Dog. Class. h Integrating all modules by the system analyst Cat. Class. h class CAT Main Program Main. Project. cpp #include “Dog. Class. h" #include “Cat. Class. h" void main() { class CAT { { : Frisky. Play. Sound(); } : : : Library 10 20 2 dfor 35}; DOG 5 f 43 23 … 10 20 2 d 35 5 f 43 23 … : : : Skeleton : public: int Draw. Shape(); public: int Play. Sound(); int Draw. Shape(); int Play. Sound(); Library for CAT}; : DOG Bobby; CAT Frisky; Bobby. Draw. Shape(); Frisky. Draw. Shape(); : … 22 6 f … 21 44 dd 23

15 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Interface versus Implementation • The declaration of a class stored in the header file is in fact a contract between the System Analyst and Programmer, and between the developer and customer. • System Analyst who wants to use the object of this class should follow the rules given in the declaration. • Class should be implemented exactly the same way it is declared; otherwise the customer using it will have errors. • C++ is strongly typed, i. e. , the compiler will enforce the contracts and set the error flag if someone violates them. • In the language of OO programming, this contract is named as interface.

16 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Create a Static Library with Visual C++ • Suppose you are one of the programmers and is asked to develop the class CAT. • Now, the system analyst sets out two specifications for the class CAT: 1. It should have a function that allows the setting of CAT’s age. The function is void Set. Age(int age); 2. It should have another function that allows the reading of CAT’s age. The function is int Get. Age();

17 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Step 1: Create the class required class CAT // declare the class { public: void Set. Age (int age); int Get. Age(); private: int its. Age; • Based on the }; specifications, you may design the above class CAT.

18 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Step 2: Implement the class CAT // declare the class { public: void Set. Age (int age); int Get. Age(); private: int its. Age; }; To be put to header file int CAT: : Get. Age() { return its. Age; } void CAT: : Set. Age(int age) { its. Age = age; } • For each member function, develop the required codes for building the library • Keep this restricted

19 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Step 3: Test the class • Create the header file and a main() for testing the functionality of the class. • This main() is only for testing purpose, not supposed to be used in real application. Main. Test. cpp Cat. Class. h class CAT { public: void Set. Age (int age); int Get. Age(); private: int its. Age; }; #include <iostream> using namespace std; #include "Cat. Class. h" // --- put implementation here int main() { CAT Frisky; Frisky. Set. Age(7); int Age = Frisky. Get. Age(); cout << Age << endl; return 0; }

20 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Step 4: Create the library with Visual C++ • Assume your class is fully tested. You can create your library using Visual C++. • Start your Visual C++. NET. • In Visual C++. NET, start a new project as usual. • Set the location to store your library project to, e. g. e: tempENG 236Ch 5 • Set the project name, e. g. Lib. Cat • However, when choosing the Application type in the Application Settings window, choose Static library and uncheck the option Precompiled header.

21 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

22 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ • In Visual C++. NET Solution Explorer, under Header Files, right-click Add New Item. . . and choose Header File (. h) • Set the Header File name to, e. g. Cat. Class • Click Add and the header file Cat. Class. h will be added to the project Lib. Cat. • Type in the class declaration.

23 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ • Type your class declaration here • Remember to comment your codes

24 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ • In Visual C++, click Project/Add New Item. . . and choose C++ File (. cpp) • Set the C++ source file name, e. g. Cat. Codes • Click OK and the C++ source file Cat. Codes. cpp will be added to the project Lib. Cat • You may verify it by checking the Solution Explorer. • Type the codes for the implementation of the member functions. • Remember to include your header file and be careful of its location. • Build the library by clicking Build Solution. Give Lib. Cat. lib

25 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ • You need to enter the correct path of the header file • It shows that the Cat. Class. h is in the current folder of Cat. Codes. cpp Solution Explorer window • After building the library, result is shown.

26 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Step 5: Send the library and header file to the System Analyst. • Locate the files Cat. Class. h and Lib. Cat. lib and send to the System Analyst. • Cat. Class. h shows the class definition. • From its content, the System Analyst knows how to use the class. • Lib. Cat. lib contains the codes for the implementation of the member functions. • It is in object code form such that even the System Analyst cannot interpret or modify the codes.

27 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ e: temp ENG 236 Ch 5Li b. Cat debug e: tempENG 236Ch 5Lib. Cat

28 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Step 6: Integrating into the application • Open a Win 32 Console project, e. g. Main. Project in the folder e. g. e: tempENG 236Ch 5. • Enter the codes for the main() and other functions if necessary to implement the application. Prog. cpp • Copy Cat. Class. h and Lib. Cat. lib sent from the programmer to current folder e: tempENG 236Ch 5Main. Project • In Visual C++, click Project/Add Existing Item. . . under Main. Project. Select All Files to show all the files in the folder. • Add Cat. Class. h and Lib. Cat. lib to the project. • Build Solution and run the application.

29 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ After building the application, the result is shown See the added Cat. Class. h and Lib. Cat. lib here

30 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ You may double-click Lib. Cat. lib. You can only see some hex codes. No source codes can be found

31 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ To see how to use the class CAT, double-click Cat. Class. h

32 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Exercise 5. 3 - Requirements • Write a program using Visual C++ to do the following: • • A class CAT must be created and your program will repeatedly ask user to choose one of the following a. Set the weight of a cat b. Get the weight of a cat c. Ask the cat to Meow! d. Quit If the user chooses (a), the user will be asked to input the weight of the cat and the program will store it up. • If the user chooses (b), the weight of the cat will be shown on the screen. • If the user chooses (c), show the following message on the screen “Meow, Meow. . . Meow”. • If the user chooses (d), quit the program.

33 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Exercise 5. 3 (cont. ) - Activity 1. Two students in a group. One plays the role of a System Analyst. One plays the role of a Programmer. 2. The System Analyst should design the program structure using flow chart and define the specifications of the class required. 3. The Programmer should develop the class and build a library (with the header file required). You may email your library file and header file to the System Analyst. 4. After receiving the files from the Programmer, the System Analyst should integrate them into the application. 5. Show the result to your tutor.

34 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Exercise 5. 3 b - Requirements • Not marked Write a program using Visual C++ to do the following: • • A class PHONE must be created and your program will repeatedly ask user to choose one of the following a. Set the serial no. of a phone b. Get the serial no. of a phone c. Ask the phone to ring! d. Quit If the user chooses (a), the user will be asked to input a 6 -digit serial no. of the phone and the program will store it up. • If the user chooses (b), the 6 -digit serial no. of the phone will be shown on the screen. • If user chooses (c), show the following message on the screen “Ring. . . Ring, Ring. . . Ring” • If user chooses (d), quit the program.

35 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Exercise 5. 3 b (cont) - Activity 1. For the same group, interchange the role of group members. The Programmer will play the role of System Analyst now; and the System Analyst will play the role of Programmer now. 2. The System Analyst should design the program structure using flow chart and define the specifications of the class required. 3. The Programmer should develop the class and build a library (with the header file required). You may email your library file and header file to the System Analyst. 4. After receiving the files from the Programmer, the System Analyst should integrate them into the application. 5. Show the result to your tutor.

36 Acknowledgment l The slides are based on the set developed by Dr. Frank Leung (EIE).
- Slides: 36