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

Slides:



Advertisements
Similar presentations
Stored procedures and views You can see definitions for stored procedures and views in the demo databases but you can’t change them. For views, expand.
Advertisements

Writing a Good Program 6. Pointers and Arrays
COSC 120 Computer Programming
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 Lab Session-I CSIT120 Spring2001 Using Windows Using An Editor Using Visual C++ Using Compiler Writing and Running Programs Lab-1 continues (Session.
Computer Science 1620 Programming & Problem Solving.
1 Writing a Good Program 5. Objects and Classes in C++
Assembler Compiler Interpreter ASSEMBLER To convert the assembly language into machine code. Translate mnemonic operation codes to their machine language.
Chapter 6: Functions.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
A First Program Using C#
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Working with Unmanaged Code.
CS Tutorial 1 Getting Started with Visual Studio 2012 (Visual Studio 2010 are no longer available on MSDNAA, please choose Visual Studio 2012 which.
Intro to C++. Getting Started with Microsoft Visual Studios Open Microsoft Visual Studios 2010 Click on file Click on New Project Choose Visual C++ on.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
A1 Visual C++.NET Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Quick Introduction The following.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Lecture 6: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept.
Computer Science I How to Configure Visual Studio.NET 2003 for C++ Colin Goble.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Active-HDL Interfaces Building VHPI Applications C Compilation Course 9.
What does a computer program look like: a general overview.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
N from what language did C++ originate? n what’s input, output device? n what’s main memory, memory location, memory address? n what’s a program, data?
C++ Classes and Data Structures Jeffrey S. Childs
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
SNPL1 Woochang Lim C+OOP = C++ C (non OOP)  C++ (non OOP+OOP)  Java (OOP) Object-Oriented Design  Object-Oriented Programming Programming with C++
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
The Hashemite University Computer Engineering Department
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Fundamental Programming Fundamental Programming Introduction to Functions.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Software Engineering Algorithms, Compilers, & Lifecycle.
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
Tutorial 1 Writing Your First C++ Program CSC1110C Introduction to Computer Programming By Paul Pun Acknowledgement: Special thanks to Dr. Michael Fung.
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
Computer Terms Review from what language did C++ originate?
Computer Programming Lecturer: Ir Dr Frank H.F. LEUNG
Separate Compilation and Namespaces
Chapter 1: An Overview of Computers and Programming Languages
1. Open Visual Studio 2008.
Lab 1 Introduction to C++.
Computer Terms Review from what language did C++ originate?
Presentation transcript:

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

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

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 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 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 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 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 d 35 5f … … 22 6f … 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 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 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 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 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 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 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 { 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 d 35 5f … … 22 6f … dd 23 : }; : Library for DOG d 35 5f … … 22 6f … dd 23 : : Skeleton

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 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 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 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 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 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 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. CatClass Click Add and the header file CatClass.h will be added to the project LibCat. 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 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. 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 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 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 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 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 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 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 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 Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Exercise 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 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.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 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 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 The slides are based on the set developed by Dr. Frank Leung (EIE).