Typing Conversions and Visibility Virtual Functions: Example For Pointer Conversions
Typing Conversions and Visibility Virtual Functions: Example For Pointer Conversions
Typing Conversions and Visibility Virtual Functions: Example For Pointer Conversions
Example for pointer conversions: int main() { Student s(Joe Smith, 111, 2.57, student::fresh); Student *ps = &s; GradStudent gs(John Miller, 31416, 3.99, student::grad, ra, Computer Science, Eye Movements in the Dark); GradStudent *pgs; ps->Print(); // Student::Print() ps = pgs = &gs; // GradStudent::Print() pgs->Print(); ps->Print(); // Student::Print() }
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 1
Virtual Functions
Overloaded member functions are invoked by a type-matching algorithm. These types are known at compile time and allow the compiler to select the appropriate member directly. As you will see, it would be nice to dynamically select at runtime the appropriate member function from among base- and derived-class functions. Such a mechanism is provided by the keyword virtual; it may be used only to modify member function declarations. Virtual functions combined with public inheritance are a form of pure polymorphism.
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 2
Virtual Functions
When a virtual function is invoked, its semantics are the same as those of other functions. In a derived class, a virtual function can be overridden by a another function with a matching signature. The selection of which function definition to invoke for a virtual function is dynamic. A pointer to base class can point at either a baseclass object or a derived-class object. The member function selected will depend on the class of the object being pointed at, not on the pointer type.
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 3
Virtual Functions
Note the difference in selection of the appropriate overridden virtual function from an overloaded member function: An overloaded member function is selected at compile time, based on its argument types, and it can have distinct return types. A virtual function is selected at runtime, based on the objects type, which is passed to it as its this pointer argument. Once a function is declared virtual, this property is automatically carried along to all redefinitions in derived classes.
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 4
Virtual Functions
Example: Virtual function selection class B { public: int i; virtual void Print() const { cout << i << inside B << endl; } }; class D : public B { void Print() const { cout << i << inside D << endl; } };
April 10, 2012
Virtual Functions
In the Student/GradStudent example, the selection of Print() is based on the pointer type, known at compile time. In the current (B/D) example, is selected on the basis of what is being pointed at. Here, the pointers base type is not determining the function selection. Instead, different class objects are processed by different functions, determined at runtime.
int main() { B b; B *pb = &b; D d; d.i = 1 + (b.i = 1); pb->Print(); pb = &d; pb->Print(); } Output: 1 inside B 2 inside D
5
Virtual Functions
Example: class Shape { public: virtual double Area() const { return 0; } protected: double x, y; }; class Rectangle : public Shape { public: double Area() const { return (height*width); } private: double height, width; };
April 10, 2012
Virtual Functions
Notice: The declaration of an identifier in a scope hides all declarations of that identifier in outer scopes. A base class is an outer scope of any class derived from it. This rule is independent of whether the names are declared virtual or not. If the selected function is inaccessible, we get a compile-time error.
class Circle : public Shape { public: double Area() const { return (PI*radius*radius); } private: double radius; }; int main() { Shape *s[N]; for (int i = 0; i < N; i++) totArea += s[i]->Area(); } CS410 Software Engineering
Lecture #18: Advanced C++ IV
Virtual Functions
We can define abstract base classes that include pure virtual functions (also called deferred methods). These are declared as follows: virtual function prototype = 0; An abstract base class specifies the basic common properties of its derived classes but cannot itself be used to declare objects. It is used to declare pointers that can access subtype pointers derived from the abstract class.
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 9
Testing
Terminology Types of errors Dealing with errors Component testing Unit testing Integration testing Testing strategy Design patterns & testing System testing Function testing Structure testing Performance testing Acceptance testing Installation testing
10
Terminology
Reliability: The measure of success with which the observed behavior of a system confirms to some specification of its behavior. Failure: Any deviation of the observed behavior from the specified behavior. Error: The system is in a state such that further processing by the system will lead to a failure. Fault (Bug): The mechanical or algorithmic cause of an error. There are many different types of errors and different ways how we can deal with them.
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 11 April 10, 2012
What is this?
12
Algorithmic Fault
13
14
Mechanical Fault
15
Verification?
17
18
Modular Redundancy?
19
20
Patching?
Testing?
21
22
Some Observations
It is impossible to completely test any nontrivial module or any system. Theoretical limitations: Halting problem Practical limitations: Prohibitive in time and cost Testing can only show the presence of bugs, not their absence (Dijkstra).
Testing is done best by independent testers. We often develop a certain mental attitude that the program should behave in a certain way when in fact it does not. Programmers often stick to the data set that makes the program work. "Dont mess up my code!" A program often does not work when tried by somebody else. Don't let this be the end-user.
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 26
25
Component Testing
Unit Testing: Individual subsystem Carried out by developers Goal: Confirm that subsystem is correctly coded and carries out the intended functionality Integration Testing: Groups of subsystems (collection of classes) and eventually the entire system Carried out by developers Goal: Test the interfaces among the subsystems
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 28
Fault Avoidance
Fault Detection
Fault Tolerance
Modular Redundancy
Component Testing
Integration Testing
System Testing
Correctness Debugging
Performance Debugging
27
System Testing
System Testing: The entire system is tested Carried out by developers Goal: Determine if the system meets the requirements (functional and global) Acceptance Testing: Evaluates the system delivered by developers Carried out by the client. May involve executing typical transactions on site on a trial basis Goal: Demonstrate that the system meets customer requirements and is ready to use Implementation (Coding) and testing go hand in hand.
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 29
Unit Testing
Informal: Incremental coding Static Analysis: Hand execution: Reading the source code Walk-Through (informal presentation to others) Code Inspection (formal presentation to others) Automated Tools checking for syntactic and semantic errors departure from coding standards Dynamic Analysis: Black-box testing (Test the input/output behavior) White-box testing (Test the internal logic of the subsystem or object) Data-structure based testing (Data types determine test cases)
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 30
Black-Box Testing
Focus: I/O behavior. If for any given input, we can predict the output, then the module passes the test. Almost always impossible to generate all possible inputs ("test cases") Goal: Reduce number of test cases by equivalence partitioning: Divide input conditions into equivalence classes Choose test cases for each equivalence class. (Example: If an object is supposed to accept a negative number, testing one negative number is enough)
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 31
Input is valid if it is from a discrete set. Select test cases from 2 equivalence classes:
Valid discrete value Invalid discrete value
Another solution to select only a limited amount of test cases: Get knowledge about the inner workings of the unit being tested => white-box testing
April 10, 2012 CS410 Software Engineering Lecture #18: Advanced C++ IV 32
White-Box Testing
Focus: Thoroughness (Coverage). Every statement in the component is executed at least once. Four types of white-box testing: Statement Testing Loop Testing Path Testing Branch Testing
33
35
36