This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson Learning, 2000. Advanced Class Features: -Abstract Data Type (ADT) -Object Oriented Design (OOD) Chapters 11
30
Embed
Advanced Class Features: - Abstract Data Type (ADT) -Object Oriented Design (OOD)
Advanced Class Features: - Abstract Data Type (ADT) -Object Oriented Design (OOD). Chapters 11. Conventional way of adding 2 fractions. Declare fraction, “ fract ” as struct type: struct Fract { int numerator; int denominator; }; Define functions “ add ” and “ print ” : - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson Learning, 2000.
Advanced Class Features:-Abstract Data Type (ADT) -Object Oriented Design (OOD)
Chapters 11
COMP103 - Adv. Class Features 2
Conventional way of adding 2 fractions
Declare fraction, “fract” as struct type:struct Fract { int numerator; int denominator;};
// calling the “add” functionadd(fr1, fr2); print(fr1);cout << "\n";
}
COMP103 - Adv. Class Features 4
Conventional way of adding 2 fractions
Note: users need to know the field names, i.e., fr1.numerator and fr1.denominator how to initialize the fractions, e.g. fr1.numerator=1; where put the target fraction fr1 as the first parameter in add(fr1, fr2)
that the first parameter should be passed by reference,i.e., void add(Fract &f1, Fract f2)
These are implementation details of Fraction. One should not bother the users with these details.
Users should only be concerned of what a “Fraction object” can do but not how it accomplishes its task.
Use Abstract Data Type also referred to as Object Oriented Design
COMP103 - Adv. Class Features 5
Using “ADT” (or “OOD”)
The main program becomes:void main() {// Local Declaration using ADT (class type)// assign values - constructors
Fract f1(1,3), f2(2, 5);cout << "Fraction: ";
// class member function “print”f1.print();cout << " + ";f2.print();cout << " = ";
// class member function “add” f1.add(f2); f1.print();cout << "\n";
}
COMP103 - Adv. Class Features 6
Abstract Data Type (ADT)
When we implement an “object” as an ADT (Abstract Data Type),
We hide the implementation details from the users
This is facilitated by using CLASS type in C++
Data fields should be declared as private
What the object can do is provided as public functions (methods)
Case Study: “Fract” Class Design a simple abstract data type (ADT): Fract
Data (private): numerator, denominator
Operations (things you can do with fractions) initialize [ Constructors ] read print copy increment fraction (add +1) add to another fraction // f1 = f1 + f2 add two fractions // f3 = f1 + f2
char a=‘x’, b=‘y’;int i=10 , j=-10;float q=1.34, p=3.14;// compiler knows which one to call// by examining the parameters’ typesswap(a,b); swap(i,j); swap(q,p);
}
COMP103 - Adv. Class Features 23
Function Overloading Compiler uses only the parameters (not
the return type) to detect overloading
float *average(int *array);
int *average (int *array);
These will not be considered overloaded and
will cause a compiler error!
not overloaded
COMP103 - Adv. Class Features 24
Operator Overloading
Recall:class Fract{
. . .
void increment(void);
void addTo(Fract &fr2);
friend Fract addFr(Fract &f1, Fract &f2);
};
Operation Function Syntax Arithmetic Syntax
Increment:
fr1 = fr1+1
fr1.increment(); ++fr1;
Add to:
fr1 = fr1 + fr2
fr1.addTo(f2); fr1 += fr2;
Add:
fr3 = fr1 + fr2
addFr(f1,f2); f1 + f2;
COMP103 - Adv. Class Features 25
Operator Overloading
C++ allows us to overload the operators for a more natural coding style class Fract
{ private: int numerator; int denominator; public: . . . void operator++(); // increment: ++fr1 }