Presentation is loading. Please wait.

Presentation is loading. Please wait.

2.1 Program Defects and "Bugs''

Similar presentations


Presentation on theme: "2.1 Program Defects and "Bugs''"— Presentation transcript:

1 2.1 Program Defects and "Bugs''
S.N.A.P 2.1 Program Defects and "Bugs'' Syntax Errors Run-time Errors Logic Errors 2.1, pgs

2 Attendance Quiz #8 Inheritance

3 Tip #9: Standard Integer typedefs
Iterators The actual size of integer types varies by implementation. The standard only requires size relations between the data types (i.e., a long long is not smaller than long, which is not smaller than int, which is not smaller than short.) As char's size is always the minimum supported data type, no other data types (except bit-fields) can be smaller. The following pre-processor macros are defined in the header file <stdint.h>: signed unsigned Description int8_t uint8_t Integer type with a width of exactly 8, 16, 32, or 64 bits. For signed types, negative values are represented using 2's complement. No padding bits. int16_t uint16_t int32_t uint32_t int64_t uint64_t std::size_t Largest unsigned integer type; the size of any object in bytes; widely used in the standard library to represent sizes, lengths, and counts.

4 Lab 02 - SNAP

5 S.N.A.P Lab Lab 02 - SNAP Class design is type design - defining effective classes can be challenging. For the SNAP lab you are to design classes for a school database with has-a and is-a relationships that have natural syntax, intuitive semantics, and efficient memory allocation. The class objects are populated from parsed input strings. Use object inheritance, polymorphism, and function and operator overloading where needed. All member data and internal functions are private. All classes have a "toString" method with friends for the insertion ("<<") operator. Use UML diagrams to describe your implementation.

6 Example SNAP Output Student # Name Address Phone # Course Student #
Lab 02 - SNAP Input Strings: snap(12345,Charlie Brown,Manager, ). snap(67890,Lucy,Right Field, ). csg(CS101,12345,A). csg(CS101,67890,B). **Error: csgs(CS101,67890,B). cdh(CS101,M,9AM). cdh(CS101,W,9AM). cr(CS101,1170 TMCB). Vectors: snap(12345,Charlie Brown,Manager, ) snap(67890,Lucy,Right Field, ) csg(CS101,12345,A) csg(CS101,67890,B) cdh(CS101,M,9AM) cdh(CS101,W,9AM) cr(CS101,1170 TMCB) Course Grades: CS101,Charlie Brown,A CS101,Lucy,B Student Schedules: Charlie Brown, 12345, Manager, CS101 MW 9AM, 1170 TMCB Lucy, 67890, Right Field, Student # Name Address Phone # Course Student # Grade Course Day Hour Course Room Input Error List in csg order List in snap order List in csg order

7 Student # Name Address Phone # Course Student # Grade Course Day Hour Input Error Course Room List in csg order List in snap order List in csg order

8 vector<Snap> snaps;
vector<Csg> csgs; vector<Cdh> cdhs; vector<Cr> crs; string toString() const { stringstream out; out << "snap(" << this->studentId; out << "," << this->studentName; out << "," << this->studentAddress; out << "," << this->studentPhone << ")"; return out.str(); } friend std::ostream& operator<< (ostream& os, const Snap& snap) os << snap.toString(); return os;

9 UML Private Class Name Data Members Class Methods Public
Lab 02 - SNAP This class is called Passenger. The data members are all listed by visibility, name, and type. Visibility: A '-' in front of a data member or function means that the data or function is private. A '+' indicates public visibility and a '#' indicates a protected visibility Type: You may indicate parameter type and return type before or after the name of the data member/function. Both of the following are acceptable: +string getName() +getName():string Private Class Name Data Members Class Methods Public

10 UML This class is called Passenger.
Lab 02 - SNAP This class is called Passenger. The data members are all listed by visibility, name, and type. Visibility: A '-' in front of a data member or function means that the data or function is private. A '+' indicates public visibility and a '#' indicates a protected visibility Type: You may indicate parameter type and return type before or after the name of the data member/function. Both of the following are acceptable: +string getName() +getName():string

11 UML: Interfaces / Abstract Functions
Lab 02 - SNAP There is an important distinction between a concrete class and an abstract class or interface. An interface has strictly pure virtual functions. An abstract class has at least one pure virtual function, but other functions may be defined. Both interfaces and abstract classes have italicized class names. Interfaces are denoted with <<interface>> (not in italics) above the class name. Virtual functions are italicized. In this example, because not all of Locomotive's functions are pure virtual, Locomotive is not an interface.

12 UML Inheritance ("Is-a")
Lab 02 - SNAP In this example, the class Steam Engine inherits from the abstract class Locomotive. It has access to all of the concrete functions of Locomotive, and also defines the abstract functions of Locomotive. Note that not all of the concrete functions from Locomotive are included in SteamEngine. This is because they are accessed through inheritance structure. On the other hand, the abstract functions appear in both places. It is a good practice to show a function both where it is declared and where it is defined. For the interfaces that you will use in this class, it is acceptable to omit the inherited functions from the child class. Inheritance is indicated with a white triangle arrowhead facing the parent class.

13 UML Inheritance ("Has-a")
Lab 02 - SNAP In this example, the class Steam Engine references the class Passenger, placing pointers to Passenger objects inside of its private vector. This relationship is indicated by using a filled diamond arrow. The arrow faces towards the class that "has" the other thing in it. Note: For the purposes of our class, we do not distinguish between having an object and having a pointer to an object. Just use the same type of arrow.

14 UML Inner Classes Lab 02 - SNAP Sometimes, we will define a class within a class, or a struct within a class. When we do this, we include the class/struct declaration inside of the outer class, and then create another box to hold the information for the class/struct. To show this connection, we use a circle with a cross through it. The circle should be closest to the class that contains the inner class/struct.

15 UML Lab 02 - SNAP

16 UML Summary Lab 02 - SNAP

17 Refactoring Classes Inheritance A better solution than multiple inheritance is to recognize that both Employee and Student have common data fields. Common data fields and their associated member functions could be collected into a separate class, which would become a common base class Person. This process is known as refactoring and is often used in object-oriented design. However, it leads to similar problems, because there are two Person components in the StudentWorker class: one inherited from Employee and the other inherited from Student A workable solution to this problem is beyond the scope of the chapter you can find this solution in Appendix A.4. int hours double rate major Employee double gpa string major Student string name Address* p_address Person string name int hours double rate major Address* p_address Employee double gpa string major Student Student Worker

18 OOP vs. Procedural Programming
Lab 02 - SNAP Division Of Program: In procedural programming a program is divided into small functions while in OOP it is divided into objects. Importance: In procedural programming importance is not given to data but to procedures. In OOP importance is given to data rather than functions, because OOP works with real world objects. Approach: Procedural programming is top down (design functions first.) OOP is bottom up (design objects then interactions.) Access Specifiers: In procedural programming no access specifiers are used. In OOP different type of specifiers are used for different levels of data protection. Data Moving: In procedural programming, data can move freely from function to function. In OOP, data can only move via member functions. Data Access: In procedural programming global data is acceptable and freely accessible. In OOP, public or private access specifiers are used to control data access. Data Hiding: In procedural programming, no particular method is used for data hiding and therefore it is less secure. A particular method is used for data hiding in OOP so it is more security. Overloading: Overloading is not possible in procedural programming. Overloading is possible in OOP in the form of function overloading and overloading operator.

19 Follow up… Inheritance class MyClass { private: string name; public: MyClass(const string n) : name(n) {} ~MyClass() = default; friend std::ostream& operator<< (ostream& lhs, const MyClass& rhs) lhs << "MyClass(" << name << ")"; return lhs; } }; class MyClass { private: string name; public: MyClass(const string n) : name(n) {} ~MyClass() = default; string toString() const ostringstream out; out << "MyClass(" << name << ")"; return out.str(); } friend std::ostream& operator<< (ostream& lhs, const MyClass& rhs) lhs << rhs.toString(); return lhs; }; A friend function of a class has the same access privileges to private and protected data as the class member functions. Using the public toString member function makes more sense.

20 2.1 Program Defects and "Bugs''
Syntax Errors Run-time Errors Logic Errors 2.1, pgs

21 Program Defects and "Bugs"
Program Correctness The term "bug" refers to a software defect. Debugging is a commonly used term for removing defects. Easier to eliminate defects by careful design than through testing. Testing is used to show that a program is correct, but it is difficult to determine how much testing needs to be done. testing can never demonstrate the complete absence of defects. complete testing in some environments (missile control software, nuclear power plant controls) is very difficult. You may encounter three kinds of defects or errors: Syntax errors, Run-time errors or exceptions, Logic errors.

22 Syntax Errors Program Correctness Syntax errors are mistakes in using the grammar (syntax) of the C++ language. The C++ compiler will detect most syntax errors during compilation. Some common syntax errors: Omitting or misplacing braces that bracket compound statements. Invoking a member function that is not defined for the object to which it is applied. Not declaring a variable before using it. Providing multiple declarations of a variable. Not assigning a value to a local variable before referencing it. Not returning a value from a function whose result type is not void. Unfortunately, the compiler may not detect syntax errors that are the result of typographical errors, such as using = when you intended to use ==.

23 Run-time Errors Program Correctness Run-time errors occur during program execution when the computer or the C++ run-time library detects an operation that it knows to be incorrect. Most run-time errors cause the operating system or run-time library to issue an error message and halt the program. Different computers, operating systems, and compilers handle these errors differently. Some common run-time errors include:

24 Logic Errors A logic error occurs when the programmer or analyst
Program Correctness A logic error occurs when the programmer or analyst makes a mistake in the design of a class. makes a mistake in the design of a class function. has implemented an algorithm incorrectly. uses an incorrect algorithm. The C++ code will compile and run, but will not meet the specifications—it will produce incorrect results. Most logic errors do not cause syntax or run-time errors and, consequently, are difficult to find. Test cases can help find logic errors. Test cases allow the programmer to compare actual program output with the expected results. Software professionals must be diligent in detecting and correcting logic errors.

25 Logic Error Results Program Correctness The original Mars Lander spacecraft crashed because of inconsistent calculations (feet versus meters). Billing programs have sent out bills for exorbitant amounts of money. ATM programs and off-track betting programs have caused machine owners to lose significant amounts of money. Many popular operating systems have been released with bugs that have allowed hackers to access computers easily and illicitly. A popular word processor when integrated with a document management system "ate" documents off the server leaving an empty file. In the 1980s several patients died after software controlling therapeutic radiation machines gave them massive overdoses. In August 2004, two automobile recalls that could have led to fatal accidents occurred because of software errors in control systems.

26 Quiz: Identify any syntax, run-time, and/or logic errors:
#include <iostream> int findMax(int a[], n) { int max = numeric_limits<int>::max(); for (int i = 0; i <= n; i++) if (a[i] > max) max = a[i]; } return max - numeric_limits<int>::max(); int main(int argc, char* argv) cout << "Program: " << argv[1] << endl; int a[] = { 7, 5, 3, 10, 2, 6 } cout << "findMax(a) = " << findMax(a, 7); return;

27


Download ppt "2.1 Program Defects and "Bugs''"

Similar presentations


Ads by Google