Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 132 Spring 2008 Chapter 1 Software Engineering Principles and C++ Classes.

Similar presentations


Presentation on theme: "1 CS 132 Spring 2008 Chapter 1 Software Engineering Principles and C++ Classes."— Presentation transcript:

1 1 CS 132 Spring 2008 Chapter 1 Software Engineering Principles and C++ Classes

2 2 What Do You Know Now? You can –take a problem of medium complexity –write an algorithm to solve it –code the algorithm in C++, and –determine the correctness of the solution You are reasonably adept at trouble-shooting when something does not work Next: –new data structures –object-oriented approach to program organization –a greater emphasis on software engineering

3 Software Engineering A disciplined approach to the design, production, and maintenance of computer programs that are developed on time and within cost estimates, using tools that help to manage the size and complexity of the resulting software products Life cycle: the phases a program goes through from when is conceived until it is retired

4 4 Approximate lifecycle costs

5 5 Software Development Phases Analysis –understand problem –divide it into subproblems and analyze them Design –algorithms –structured design (functions) –object-oriented design (objects/classes) Implementation –write code for classes and functions discovered in design phase Testing and Debugging –make sure the program does what it is supposed to –find and fix errors –run program through series of specific tests and test cases

6 6 Development Phases, Another View Requirements (what should the system do) Implementation Test Plan Formal Testing Our emphasis this semester: implementation

7 Design: Two Approaches to Building Manageable Modules Divides the problem into more easily handled subtasks, until the funct- ional modules (subproblems) can be coded Identifies objects composed of data and operations that can be used to solve the problem STRUCTURED DESIGN OBJECT-ORIENTED DESIGN FOCUS: processes FOCUS: data objects Which have you done?

8 8 Object-Oriented Design (OOD) Three principles –encapsulation combine data and operations in a single unit what have we encapsulated to date? –inheritance create new data types from existing types e.g. car → suv –polymorphism use the same expression to denote different operations where have we done this?

9 9 Algorithm Analysis: the Big-O notation n = # houses effort = 1 + 1 +... + 1 + n = 2n to each house back

10 10 Algorithm Analysis: Big-O Notation n = # houses effort = 2(1 + 2 + 3 +... + n) = 2(n+1)n/2 = n 2 + n to each house and back

11 11 Algorithm Analysis: Big-O Notation

12 12 Algorithm Analysis: Big-O Notation We will refer to the order of algorithms, but not do much with it

13 13 Class The BIG concept A collection of a fixed number of components Components: –public (members of class accessible outside class) –private (members of class not accessible outside the class) –protected (members of class that only "friends" can use)

14 14 Syntax for Defining a Class reserved word the name you give a class a variable in the class a function applied to it variables and functions Where have you seen something like this? fout.open classIdentifier classVariableName; Declaring a variable: classIdentifier classVariableName; classVariableName.memberName Invoking a function: classVariableName.memberName

15 15 class clockType { public: void setTime(int hours, int minutes, int seconds); void getTime(int& hours, int& minutes, int& seconds); void printTime() const; void incrementSeconds(); clockType(int hours, int minutes, int seconds); clockType(); private: int hr; int min; int sec; }; Class name Private members* Public members* * members are variables or methods (functions)

16 16 class clockType clockType myClock; myClock.SetTime(2, 26, 47); clockType yourClock; yourClock.SetTime(14, 39, 28);

17 17 class clockType After myClock = yourClock;

18 18 How Do We Use This? clockType.h // the file that specifies the member functions clockTypeImp.cpp // implementation of the member functions clock1.cpp // a driver that uses the class A project that links them –compile the driver –this creates a project –add the implementation to the project –project files are visible via View/Workspace/FileView Try it: –add a member function that returns just the hour –augment the driver (clock1.cpp) to test it –run it

19 19 Constructors Invoked when a class instance is created. (analogous to a when variable is declared last semester) Functions included in public clockType(); //default clockType(int hours, int minutes, int seconds); clockType(int hours, int minutes, int seconds); Syntax to invoke the default constructor clockType myClock; //initializes components to 0,0,0

20 20 Constructors Syntax to invoke a constructor with a parameter: clockType myClock(2, 26,47); //initializes to 2,26,47 If no parameters are given, the default constructor applies See clock2.cpp Destructors Function like constructors Called when the object goes out of scope Used to return memory Later

21 21 Data Abstraction Abstract data type (ADT): –specify the logical properties without the implementation details Information hiding –separate implementation details from the logical properties of an ADT In C++ classes –a header file (clockType.h) that contains the specification –a separate file (clockTypeImp.cpp) with implementation details How does clockTypeImp.cpp implement clockType.h? Note, we are hiding how clockType is represented Could it be done differentl y?

22 22 Example: listType (p.38) Why is this a good candidate for data abstraction? What functions would you want for a list? –check to see if list is empty –check to see if list is full –search for a given item –delete an item –insert an item –sort How might you implement it? –an array –an integer for the number of elements

23 23 Example: personType Components: first and last name Functions: print, change the name, retrieve the name Code: person.h, personImp.cpp, persondr.cpp Exercise: add a function to return the last name personType -firstName: string -lastName: string +print(): void +setName(string, string): void +getName(string&, string&): void +personType(string="", string="")

24 24 complexType Complex numbers: a + bi or (a,b) where i 2 = -1 (a,b) + (c,d) = (a+c, b+d) (a,b) - (c,d) = (a-c, b-d) (a,b) * (c,d) = (ac - bd, bd + ac) (a,b) / (c,d) = ((ac+bd)/(c 2 + d 2 ), (bc-ad)/(c 2 + d 2 )) How would we make a class? Code: complexType.h, complexType.cpp, testComplex.cpp Later: a version with function overloading

25 25 Programming Example: Candy Machine Problem statement: A common place to buy candy is from a candy machine. A new candy machine is bought for the gym, but it is not working properly. The machine sells candies, chips, gum, and cookies. You have been asked to write a program for this machine so that it can be put into operation. The program should do the following: 1.Show the customer the different products sold by the candy machine. 2.Let the customer make a selection. 3.Show the customer the cost of the item selected. 4.Accept money from the customer. 5.Release the item.

26 26 Candy Machine Input –Item selection –Cost of Item Output –The selected item Components –Cash register –Several dispensers

27 27 Candy Machine: sample run


Download ppt "1 CS 132 Spring 2008 Chapter 1 Software Engineering Principles and C++ Classes."

Similar presentations


Ads by Google