2. DATA TYPES A DATA TYPE IS A FORMAL DESCRIPTION OF: 1. THE DOMAIN THAT AN OBJECT OF THAT TYPE CAN HAVE. 2. THE BASIC SET OF OPERATIONS THAT CAN BE APPLIED TO VALUES OF THAT TYPE. 3. DATA REPRESENTATION.
3. THE FORMAL DESCRIPTION OF int AND float VALUES AND THE ALLOWABLE OPERATIONS ON SUCH DATA TYPE COME FROM MATHEMATICS. THE OPERATIONS ARE: + , - , * , AND / . THE RANGE OF VALUES CAN BE LISTED AS INT_MIN TO INT_MAX .
8. A SIMULATION OF THE TYPE Logical : typedef int Logical; const Logical TRUE = 1; const Logical FALSE = 0; : Logical positive; : positive = TRUE; // initialize flag
9. MORE ON DATA ABSTRACTION ABSTRACT DATA TYPES PROVIDE FOR THE ABILITY TO MODEL THE DOMAIN AND OPERATIONS OF DATA WITHOUT ANY CONCERN ABOUT IMPLEMENTATION.
10. THE CASE OF THE "COUNTER" A COUNTER IS A SIMPLE INTEGER VARIABLE. ITS VALUE IS CHANGED AND ACCESSED DEPENDING ON SOME OTHER TASK.
11. COMMON OPERATIONS PERFORMED (ON THE INTEGER VARIABLE) ARE: 1. CLEAR (START AT SOME INITIAL VALUE) 2. ALTER THE VALUE TO KEEP TRACK OF TASK (INCREMENT) 3. ALTER THE VALUE TO KEEP TRACK OF TASK (DECREMENT) 4. EXAMINE OR PRINT THE CURRENT VALUE (ACCESS)
12. EXAMPLE: PROBLEM : GRADUATION CERTIFICATION REFERS TO EVALUATING EARNED CREDITS TO ASSURE THAT STUDENTS HAVE SATISFIED CURRICULUM REQUIREMENTS. SOME STUDENTS DO, SOME DO NOT. DEVELOP AND IMPLEMENT A SYSTEM THAT KEEPS TRACK OF THOSE STUDENTS WHO ARE GRADUATING AND THOSE THAT ARE DELAYED BASED ON NUMBER OF CREDITS EARNED.
13.
14. 2. SPECIFICATION (ONLY REGARDING COUNTER) a) OPERATIONS void Initialize(int); void Increment(); void Decrement(); int Access_Value(); b) DATA int value;
17. RELATIONSHIP BETWEEN DATA AND OPERATIONS: SEPARATE ENTITIES PROCESS A DATA STRUCTURES CONTROL STRUCTURES PROCESS D PROCESS C PROCESS B
18. ALTERNATIVE REPRESENTATION THE CLASS , A SET OF OBJECTS, ALLOWS FOR THE REPRESENTATION OF DATA AND OPERATIONS INTO A COHESIVE UNIT.
19. RELATIONSHIP BETWEEN DATA AND OPERATIONS: A COHERENT UNIT CONTROL STRUCTURES AND DATA STRUCTURES PROCESS A PROCESS D PROCESS C PROCESS B DATA
20. THE C++ CLASS A MECHANISM TO MODEL OBJECTS WITH MULTIPLE ATTRIBUTES. THE C++ CLASS ALLOWS FOR THE DEFINITION A NEW TYPE OF DATA ACCORDING TO THE NEEDS OF THE PROBLEM, AND TO DEFINE OPERATIONS (METHODS) TO ACT UPON THE TYPE (ABSTRACT DATA TYPING).
21. INFORMATION HIDING THE ENCAPSULATION OF IMPLEMENTATION DETAILS. PRIVATE CODE AND DATA CANNOT BE ACCESSED DIRECTLY.
22. CLASS SYNTAX class <class_name> { public: <public declaration list> private: <private declaration list> };
23. class <class_name> { public: <public data structures> // optional <constructors> // optional <function prototypes> // optional private: // optional <private data structures> // optional <function prototypes> // optional };
24.
25. SYNTAX OF DEFINING MEMBER FUNCTIONS <return_type> <class_name> :: <function_name> ( <parameter_list> ) { <function member body> }
26.
27.
28.
29.
30.
31. IMPLEMENTATION: THE Counter CLASS // File: Counter.h #include <limits.h> // contains definitions of // INT_MAX and INT_MIN class Counter { public: // member functions void Initialize(int init_value); // initialize counter void Increment(); // increment counter void Decrement(); // decrement counter int Access_Value(); // return current counter // value private: // data members int value; };
33. void Counter::Decrement() // decrement counter { if (value > INT_MIN) value--; else cerr << "Counter underflow. Decrement ignored." << endl; } // Decrement() int Counter::Access_Value() // return current counter value { return value; } // Access_Value()
34. #include <iostream.h> #include "Counter.h" void main() { // Minimum credits for graduation const int GRAD_LEVEL = 130; // local objects of class Counter Counter c_graduated; Counter c_delayed; // local data int class_size, number_of_credits; int i; // get the graduating class size cout << "Enter the class size: "; cin >> class_size; IMPLEMENTATION (USING THE Counter CLASS)
35. // initialize counter values c_graduated.Initialize(0); c_delayed.Initialize(class_size); // use of Increment() and Decrement() for (i = 0; i < class_size; i++) { cout << "Please enter the number of validated credits: "; cin >> number_of_credits; if (number_of_credits >= GRAD_LEVEL) { c_graduated.Increment(); c_delayed.Decrement(); } } // print results using Access_value() cout << "The number of students graduating this semester: " << c_graduated.Access_Value() << endl; cout << "The number of students delayed this semester: " << c_delayed.Access_Value() << endl; return; }
36.
37.
38.
39.
40. ANOTHER EXAMPLE: PROBLEM : USING FLOATING POINT NOTATION TO REPRESENT FRACTIONS MAY PRODUCE APPROXIMATE RESULTS. FOR EXAMPLE, THE RATIONAL NUMBER 1/3 IS 0.33333. REGARDLESS OF THE NUMBER OF ACCURATE DIGITS, IT WOULD BE IMPOSSIBLE TO PRECISELY REPRESENT SUCH A NUMBER. DESIGN, DEVELOP AND IMPLEMENT A SYSTEM TO REPRESENT FRACTIONS ACCURATELY. AS AN EXAMPLE, WE WILL IMPLEMENT THE ADDITION OPERATION ONLY.
41.
42. 2. CLASS SPECIFICATION A) PUBLIC CONSTRUCT: Fraction(int n, int d = 1); Fraction(); void Get(); void Show(); double Evaluate(); B) PRIVATE int numerator; int denominator; C) FRIEND friend Fraction operator+ (Fraction f1, Fraction f2);
45. class Fraction { // operator overload, so we can do fractional arithmetic // using a familiar operator, the "+" sign friend Fraction operator+ (Fraction f1, Fraction f2); public: : private: : }; FRIEND FUNCTIONS AND OPERATOR OVERLOADING
46. class Fraction { : public: Fraction(int n, int d = 1); // constructor Fraction(); // another constructor : private: : }; CONSTRUCTORS
47. // File: Fraction.h // The class declaration for fractions is included in this // header file. We represent a fraction using a pair of integers: // the numerator (top part) and denominator (bottom part). // This class definition includes more than the rational // numbers, since the number infinity is permitted (a fraction // with a zero denominator--except 0/0)
48. class Fraction { // operator overload, so we can do fractional arithmetic // using a familiar operator , the "+" sign friend Fraction operator+ (Fraction f1, Fraction f2); public: Fraction(int n, int d = 1); // constructor // Set numerator = n, denominator = d // if no second argument, default to 1 // another constructor: Fraction(); // Set numerator = 0, denominator = 1 void Get(); // Get a fraction from keyboard void Show(); // Display a fraction on screen double Evaluate(); // Return the decimal value of a fraction private: int numerator; // top part int denominator; // bottom part };
49. // File: Fraction.cpp // The class definition for fractions #include <iostream.h> #include "Fraction.h" Fraction operator+ (Fraction f1, Fraction f2) // Override of operator "+" for fraction addition { Fraction r; // the return value of f1 + f2 // compute numerator r.numerator = (f1.numerator * f2.denominator) + (f2.numerator * f1.denominator); // compute denominator r.denominator = f1.denominator * f2.denominator; return r; // return the result }
50. Fraction::Fraction(int n, int d) // A Fraction constructor which allows the numerator and // denominator to be specified { numerator = n; denominator = d; }
52. void Fraction::Get() // Get a fraction from standard input, in the form // "numerator/denominator" { char div_sign; // used to consume the '/' character during input cin >> numerator >> div_sign >> denominator; }
53. void Fraction::Show() // Display a fraction, in the form "numerator/denominator" { cout << numerator << '/' << denominator; }
54. double Fraction::Evaluate() // Calculates and returns the decimal value of a fraction { double n = numerator; // convert numerator to float double d = denominator; // convert denominator to float return (n / d); // return float representation }
55. // File: TestFraction.cpp // Test the Fraction class #include <iostream.h> // for output #include "Fraction.h" // for Fraction declarations void main() { // Fraction constructors Fraction f1(3, 2), f2(4), f3, f4; // Display the fractions cout << endl << "The fraction f1 is "; f1.Show(); cout << endl << "The fraction f2 is "; f2.Show(); cout << endl << "The fraction f3 is "; f3.Show(); // Get and display a fraction cout << endl << "Enter a fraction of your own: "; f3.Get(); cout << endl << "You entered "; f3.Show();
56. // Add two Fractions using the overloaded operator f4 = f1 + f3; // Display the fractions and result cout << endl << endl << "The sum of "; f1.Show(); cout << " and "; f3.Show(); cout << " is "; f4.Show(); // Find and display the floating-point value of the Fraction cout << endl << "The value of this fraction is " << f4.Evaluate() << endl; }
Hinweis der Redaktion
Member access specifiers are always followed by a colon (:) and can appear multiple times in any order.
CONSTRUCTOR: defining, call, overloading, no return type.