Download presentation
Presentation is loading. Please wait.
1
1 Writing a Good Program 5. Objects and Classes in C++
2
2 5.3 Modular Programming with C++ Computer Programming and Basic Software Engineering 5. Objects and Classes in C++
3
3 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ 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. Doing Business by Selling Classes
4
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 // for cout using namespace std; class Cat// declare the class object { public: void SetAge (int age); int GetAge(); void SetWeight (int weight); int GetWeight(); private: int itsAge; int itsWeight; }; You do not want to give him the source codes of the member functions since your customer may copy your codes. They may modify it and re-sell it to somebody else. You do not want to give him the source codes of the member functions since your customer may copy your codes. They may modify it and re-sell it to somebody else. int Cat::GetAge() {return itsAge; } void Cat::SetAge(int age) {itsAge = age; }
5
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? 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
6 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Static 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. Header library
7
7 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Main Program main() of the Customer #include "CatClass.h" void main() { CAT Frisky; Age = Frisky.GetAge(); } MainProject.cpp : : { public: int GetAge(); }; CatClass.h : : class CAT Library 10 20 2d 35 5f 43 23 43 23 … … 22 6f … 21 44 dd 23 Contain the implementation of GetAge(), however, the source code cannot be seen by the customer. Show that the class CAT has a public function called GetAge() It will return an integer. : Explain why public and private When the customer builds the main(), it links with CatClass.h and the library.
8
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
9 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Example – step 1: obtain the 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. The system analyst talks with the customer and obtains the following program requirements:
10
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 All animal drawn? User click on animal? No Yes A No Yes B Draw animal
11
11 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ A Click Dog? Click Cat? Click Sheep? Click Horse? Click End? B Play dog sound Play cat sound Play sheep sound Play horse sound End Yes No
12
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. { public: int DrawShape(); int PlaySound(); }; DogClass.h : : class DOG { public: int DrawShape(); int PlaySound(); }; CatClass.h : : class CAT { public: int DrawShape(); int PlaySound(); }; SheepClass.h : : class SHEEP { public: int DrawShape(); int PlaySound(); }; HorseClass.h : : class HORSE
13
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
14 { public: int DrawShape(); int PlaySound(); DogClass.h : class CAT }; : Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Example – step 5: integrate the modules Main Program Integrating all modules by the system analyst #include “DogClass.h" void main() { DOG Bobby; CAT Frisky; Bobby.DrawShape(); Frisky.DrawShape(); } MainProject.cpp : : Frisky.PlaySound(); : #include “CatClass.h" { public: int DrawShape(); int PlaySound(); CatClass.h : class CAT Library for CAT 10 20 2d 35 5f 43 23 43 23 … … 22 6f … 21 44 dd 23 : }; : Library for DOG 10 20 2d 35 5f 43 23 43 23 … … 22 6f … 21 44 dd 23 : : Skeleton
15
15 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. Computer Programming and Basic Software Engineering 5. Objects and Classes in C++
16
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 SetAge(int age); 2.It should have another function that allows the reading of CAT ’s age. The function is int GetAge();
17
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 SetAge (int age); int GetAge(); private: int itsAge; }; Based on the specifications, you may design the above class CAT.
18
18 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Step 2: Implement the class class CAT// declare the class { public: void SetAge (int age); int GetAge(); private: int itsAge; }; int CAT::GetAge() {return itsAge; } void CAT::SetAge(int age) {itsAge = age; } For each member function, develop the required codes for building the library Keep this restricted For each member function, develop the required codes for building the library Keep this restricted To be put to header file
19
19 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Step 3: Test the class class CAT { public: void SetAge (int age); int GetAge(); private: int itsAge; }; 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. 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. #include using namespace std; #include "CatClass.h" // --- put implementation here int main() {CAT Frisky; Frisky.SetAge(7); int Age = Frisky.GetAge(); cout << Age << endl; return 0; } CatClass.h MainTest.cpp
20
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:\temp\ENG236\Ch5 Set the project name, e.g. LibCat However, when choosing the Application type in the Application Settings window, choose Static library and uncheck the option Precompiled header.
21
21 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++
22
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. CatClass Click Add and the header file CatClass.h will be added to the project LibCat. Type in the class declaration.
23
23 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Type your class declaration here Remember to comment your codes Type your class declaration here Remember to comment your codes
24
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. CatCodes Click OK and the C++ source file CatCodes.cpp will be added to the project LibCat 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 LibCat.lib
25
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 CatClass.h is in the current folder of CatCodes.cpp You need to enter the correct path of the header file It shows that the CatClass.h is in the current folder of CatCodes.cpp After building the library, result is shown. Solution Explorer window
26
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 CatClass.h and LibCat.lib and send to the System Analyst. CatClass.h shows the class definition. From its content, the System Analyst knows how to use the class. LibCat.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
27 Computer Programming and Basic Software Engineering e:\temp\ENG236\Ch 5\LibCat\LibCat 5. Objects and Classes in C++ e:\temp \ENG236 \Ch5\Li bCat\ debug
28
28 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Step 6: Integrating into the application Open a Win32 Console project, e.g. MainProject in the folder e.g. e:\temp\ENG236\Ch5\. Enter the codes for the main() and other functions if necessary to implement the application. Copy CatClass.h and LibCat.lib sent from the programmer to current folder e:\temp\ENG236\Ch5\MainProject\MainProject In Visual C++, click Project/Add Existing Item... under MainProject. Select All Files to show all the files in the folder. Add CatClass.h and LibCat.lib to the project. Build Solution and run the application. Prog.cpp
29
29 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ See the added CatClass.h and LibCat.lib here After building the application, the result is shown
30
30 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ You may double-click LibCat.lib. You can only see some hex codes. No source codes can be found
31
31 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ To see how to use the class CAT, double-click CatClass.h
32
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
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
34 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Exercise 5.3b - Requirements 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. Not marked
35
35 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Exercise 5.3b (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
36 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.