Title: Wavelet transform And Its Applications to Image Processing
1Writing a Good Program 5. Objects and Classes
in C
2Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
5.3 Modular Programming with C
3Computer 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.
4Computer 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 ltiostreamgt // 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.
int CatGetAge() return itsAge void
CatSetAge(int age) itsAge age
5Computer 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?
6Computer 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
7Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
Explain why public and private
CatClass.h
main() of the Customer
class CAT
Show that the class CAT has a public function
called GetAge() It will return an integer.
Main Program
MainProject.cpp
include "CatClass.h"
public int GetAge()
void main()
CAT Frisky Age Frisky.GetAge()
Library
Contain the implementation of GetAge(), however,
the source code cannot be seen by the customer.
10 20 2d 35 5f 43 23
43 23
When the customer builds the main(), it links
with CatClass.h and the library.
22 6f
21 44 dd 23
8Computer 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
9Example 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.
10Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
Example step 2 design the program flow
Start
- Based on the requirements, the system analyst
designs the program flow using, for example, a
flow chart.
Draw animal
All animal drawn?
No
Yes
B
User click on animal?
No
Yes
A
11Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
Click Dog?
Yes
A
B
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?
Yes
End
No
12Computer 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.
CatClass.h
HorseClass.h
DogClass.h
class CAT
class HORSE
SheepClass.h
class DOG
public int DrawShape() int
PlaySound()
class SHEEP
public int DrawShape() int
PlaySound()
public int DrawShape() int
PlaySound()
public int DrawShape() int
PlaySound()
13Computer 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.
14Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
Example step 5 integrate the modules
Integrating all modules by the system analyst
CatClass.h
Main Program
MainProject.cpp
class CAT
include DogClass.h"
include CatClass.h"
void main()
public int DrawShape() int
PlaySound()
DOG Bobby CAT Frisky Bobby.DrawShape() Fris
ky.DrawShape()
Library for DOG
10 20 2d 35 5f 43 23
Frisky.PlaySound()
43 23
22 6f
Skeleton
21 44 dd 23
15Computer Programming and Basic Software
Engineering
Interface versus Implementation
5. Objects and Classes in C
- 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.
16Computer 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 - It should have a function that allows the setting
of CATs age. The function is - void SetAge(int age)
- It should have another function that allows the
reading of CATs age. The function is - int GetAge()
17Computer 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.
18Computer 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 CATGetAge() return
itsAge void CATSetAge(int age) itsAge
age
To be put to header file
- For each member function, develop the required
codes for building the library - Keep this restricted
19Computer 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.
MainTest.cpp
include ltiostreamgt using namespace std include
"CatClass.h" // --- put implementation here int
main() CAT Frisky Frisky.SetAge(7) int Age
Frisky.GetAge() cout ltlt Age ltlt endl return
0
CatClass.h
class CAT public void SetAge (int age)
int GetAge() private int itsAge
20Computer 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.
21Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
22Computer 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.
23Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
- Type your class declaration here
- Remember to comment your codes
24Computer 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
25Computer 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
Solution Explorer window
- After building the library, result is shown.
26Computer 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.
27Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
e\temp\ENG236\Ch5\LibCat\ debug
e\temp\ENG236\Ch5\LibCat\LibCat
28Computer Programming and Basic Software
Engineering
Step 6 Integrating into the application
5. Objects and Classes in C
- 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\Ma
inProject\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
29Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
After building the application, the result is
shown
See the added CatClass.h and LibCat.lib here
30Computer 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
31Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
To see how to use the class CAT, double-click
CatClass.h
32Computer Programming and Basic Software
Engineering
Exercise 5.3 - Requirements
5. Objects and Classes in C
- 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 - Set the weight of a cat
- Get the weight of a cat
- Ask the cat to Meow!
- 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.
33Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
Exercise 5.3 (cont.) - Activity
- Two students in a group. One plays the role of a
System Analyst. One plays the role of a
Programmer. - The System Analyst should design the program
structure using flow chart and define the
specifications of the class required. - 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. - After receiving the files from the Programmer,
the System Analyst should integrate them into the
application . - Show the result to your tutor.
34Computer Programming and Basic Software
Engineering
Exercise 5.3b - Requirements
Not marked
5. Objects and Classes in C
- 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 - Set the serial no. of a phone
- Get the serial no. of a phone
- Ask the phone to ring!
- 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.
35Computer Programming and Basic Software
Engineering
5. Objects and Classes in C
Exercise 5.3b (cont) - Activity
- 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. - The System Analyst should design the program
structure using flow chart and define the
specifications of the class required. - 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. - After receiving the files from the Programmer,
the System Analyst should integrate them into the
application . - Show the result to your tutor.
36Acknowledgment
- The slides are based on the set developed by Dr.
Frank Leung (EIE).