1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b,...
-
Upload
douglas-greene -
Category
Documents
-
view
235 -
download
0
Transcript of 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b,...
![Page 1: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/1.jpg)
1
Overview of C++
CS3304 - Data Structure
![Page 2: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/2.jpg)
2
Value parameters• Value parameters
int abc (int a, int b , int c) // a, b, and c are the
{ // formal parameters
a = a * 2;
return a+b+c;
}
call:
z = abc(2, x, y) //2, x, y are the actual parameters
![Page 3: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/3.jpg)
3
Template Functionsfloat abc (float a, float b, float c) {
a = 2 * a;
return a+b+c;
}
template <class T>
T abc(T a, T b, T c) {
a = 2 * a;
return a+b+c;
}
![Page 4: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/4.jpg)
4
Reference Parametersfloat abc(int& a, int& b, int& c) {
a = 2 * a;
return a+b+c;
}
template <class T>
T abc (T& a, T& b, T& c) {
a = 2 * a;
return a+b+c;
}
![Page 5: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/5.jpg)
5
Const Reference Parameters• Formal parameters are not changed, avoid copying
template < class T>
T abc (const T& a, const T& b, cont T& c) {
return a+b+c;
}
template <class Ta, class Tb, class Tc>
Ta abc(const Ta& a, const Tb& b, const Tc& c) {
return a+b+c
}
![Page 6: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/6.jpg)
6
Return Values• Value return
T X(int i, T& z) // a copy of the value is returned
• reference return
T& X(int i, T& z) //a reference is returned
• const reference return
const T& X(int i, T& z) // a reference to a const
// object is returned
![Page 7: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/7.jpg)
7
Recursive functions
When a function calls itself (directly or indirectly).
ex: factorial, exponentiation, and Fibonacci numbers
n! = 1 * 2 * 3 * ... *n, for n > 0
and that 0! = 1
a recursive function has two parts:
base part
recursive part
![Page 8: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/8.jpg)
8
ex: Factorial
n! = 1 if n = 0 (base)n! = n(n-1)! if n > 0 (recursive)
![Page 9: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/9.jpg)
9
Definition• Recursion is a problem solving tool that allows you to solve a
problem p by solving another problem p' that is similar in nature to p but smaller.
• Each successive recursive call should bring you closer to a situation in which the answer is known.
• A case for which the answer is known (and can be expressed without recursion) is called a base case.
• Each recursive algorithm must have at least one base case, as well as the general (recursive) case
![Page 10: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/10.jpg)
10
Factorial Nfactorial n n! = n * (n - 1) * (n - 2) * ... * 2 * 1;
int function fact (int n)
{
if (n == 0) // base case
return (1);
else // recursive case
return (n * fact (n - 1));
}
![Page 11: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/11.jpg)
11
Steps
fact(4) -> 4 * fact(3) -> 3 * fact(2) -> 2 * fact(1) -> 1 * fact(0)
1 * 1
2 * 1 <-----------|
3 * 2 <-----------|
4 * 6 <------------|
24 <-------|
![Page 12: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/12.jpg)
12
iterative solution to factorial
int function fact_it (int n )
{
int p;
p = 1;
for (int i = 1; i <= n; i++)
p = p * i;
return (p);
}
![Page 13: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/13.jpg)
13
Disadvantages and Advantages of recursion
Disadvantages:
each function call creates an activation record in memory & requires processing time to set up records & to reset values after return from function call
advantages:
some problems may be easier to set up & program
![Page 14: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/14.jpg)
14
Iterative function for SUM
Template <class T>
T sum(T a[], int n) {
//return sum of numbers a[0: n-1]
T tsum = 0;
for (int i = 0; i< n; i++)
tsum += a[i]
return tsum;
}
![Page 15: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/15.jpg)
15
Recursive function for SUM
Template < calss T>
T Rsum(T a[], int n) {
// return sum of numbers a [0 : n-1]
if (n > 0)
return Rsum(a, n-1) + a[n-1]
return 0; // base case
}
![Page 16: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/16.jpg)
16
Recursive function for the Fibonacci numbers
• F(0) = 0
• F(1) = 1
• F(n) = F(n-1) + F(n-2)
int fibonacci(int n) {
if (n==0) return 0;
else if (n==1) return 1;
else if (n > 1)
return (fibonacci(n-1) + fibonacci(n-2));
}
![Page 17: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/17.jpg)
17
Dynamic Memory Allocation• Operator new
int *y;
y = new int;
*y = 10;
float *x = new float [n]; // array
• Operator delete
delete y;
delete [] x; // one-dimensional array
![Page 18: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/18.jpg)
18
The operator new
int *y = new int;
*y = 10
or
int *y = new int (10)
or
int *y;
y = new int (10)
![Page 19: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/19.jpg)
19
One-Dimensional Arrays
• Float *x = new float [n]
• addressing the elements:
– x[10], x[n-1]
![Page 20: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/20.jpg)
20
Exception Handling• Using #Include <except.h>
• using try and catch
float *x;
try {x = new float [n];}
catch (xalloc) {// enter only when new fails
cerr << “out of Memory” << endl;
exit (1); }– xalloc exception is thrown by new when unable
to allocate memory memory
![Page 21: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/21.jpg)
21
Two-dimensional ArraysChar c[7][5] // static allocation
• Dynamic allocation:
char (*c) [5] // number of columns=5
try {c = new char [n][5];} // n is a variable
catch (xalloc) { // enter only when new fails
cerr << “out of Memory” << endl;
exit (1); }
• Value of n may be determined dynamically
![Page 22: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/22.jpg)
22
Two-dimensional Arrays• What if number of columns is not known at compile time?
• Need to construct array dynamically
• View 2-d array as a 1-d array of rows
• Each row is created using new
• Pointers to each row are saved in another
1-d array.
X
![Page 23: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/23.jpg)
23
Pointer to Pointerstemplate <class T>
bool make2darray(T ** &x, int rows, int cols) {
try {
x = new T * [rows] // array of pointers
// create memory for each row
for (int i = 0, i <rows; i++)
x[i] = new int [cols]
return true
}
catch (xalloc) {return false;}
}
![Page 24: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/24.jpg)
24
Classes
• Example: define a class currency
• $2.35
• -$64.32
• Operations:
– set data values
– determine components
– add two objects
– increment the value
– output
![Page 25: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/25.jpg)
25
Classesenum sign (plus, minus);class Currency {
private:
sign sgn;
unsigned long dollars;
unsigned int cents; public:
Currency(sign s = plus, unsigned long d = 0,unsigned int c = 0);
~Currency() {}bool Set(sign s, unsigned long d,
unsigned int c);bool Set (float a);
![Page 26: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/26.jpg)
26
Classes - cont.sign Sign() const {return sgn;}unsigned long Dollars() const {return dollars;}unsigned int Cents () cosnt {return cents;}Currency Add (const Currency& x) const;Currency& Increment (const Currency& x);void Output () const;
}
![Page 27: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/27.jpg)
27
Classes - cont.
void main () {
Currency f;
currency g(plus, 2, 45), h(minus, 10);
Currency *m = new Currency (plus, 8, 12);
g.Set(minus, 33, 0);
h.Set(78.33);
}
![Page 28: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/28.jpg)
28
Member Functions//Class constructor
Currency::Currency(sign s, unsigned long d,
unsigned int c)
{
if (c > 99) {
cerr << “Cents should be < 100” << endl;
exit (1);}
sgn = s; dollars = d; cents = c;
}
![Page 29: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/29.jpg)
29
Member Functions - Cont.
Bool Currency::Set(sign s, unsigned long d,unsigned int c)
{if (c > 99 ) return false;sgn = s; dollars = d; cents = c;return true;
}
![Page 30: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/30.jpg)
30
Member function -contBool Currency::Set (float a) {
if (a < 0) {sgn = minus; a = -a;}
else sgn = plus;
dollars = a;
cents = ( a + 0.005 – dollars) * 100;
return true;
}
//00.5 is needed to take care of computer errors when representing float numbers
![Page 31: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/31.jpg)
31
Add
![Page 32: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/32.jpg)
32
Member function -contCurrency& Currency::Increment(const Currency& x)
{
*this = Add(x);
return *this;
}
• this points to the invoking object
• *this is the invoking object
![Page 33: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/33.jpg)
33
Operator Overloading• C++ built-in operators can be overloaded
• Overloaded operator obeys the precedence, associativity, and number of operands dictated by the built-in operator
• When an operator is overloaded as a member function, the object associated with the operator is the left-most operand
example: X + Y <=> X.operator + (Y)
=> left-most operand must be an object of the class of which the operator is a member.
If that is not the case, instead of using a member operator use a friend operator.
![Page 34: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/34.jpg)
34
Operator Overloadingenum sign (plus, minus);class Currency {private:
sign sgn; unsigned long dollars; unsigned int cents;
public:Currency(sign s = plus, unsigned long d = 0,
unsigned int c = 0);~Currency() {}bool Set(sign s, unsigned long d,
unsigned int c);bool Set (float a);
![Page 35: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/35.jpg)
35
Operator Overloadingsign Sign() const {return sgn;}unsigned long Dollars() const {return dollars;}unsigned int Cents () cosnt {return cents;}Currency operator+ (const Currency& x) const;Currency& Increment (const Currency& x);void Output () const;
}
![Page 36: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/36.jpg)
36
Operator OverloadingCurrency Currency::operator+ (const Currency& x) const {
Currency y;
y.amount = amount + x.amount;
return y;
}
Example:
Currency A(2, 50), B(minus, 7, 35), C;
C = A + B;
![Page 37: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/37.jpg)
37
Operator Overloadingvoid Currency :: output() const
{
if (sgn == minus) cout << ‘-’;
cout << ‘$’ << dollars << ‘.’;
if (cents < 10) cout << “0”;
cout << cents;
}
• Example: Currency X(plus, 3, 50);
X.output();
![Page 38: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/38.jpg)
38
Operator Overloading
• Better overload the stream insertion operator <<:
cout << X;
• Note However,
cout.operator << X // erroneous
• the object from which the operator is invoked is not an object of type Currency.
==> operator << can NOT be a member of the class.
• Can define operator outside the class
==> no access to private members
• Define it as a friend member.
![Page 39: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/39.jpg)
39
New Currency Classclass Currency {
friend ostream& operator<< (ostream&, const Currency&);
public:Currency()….Unsigned long Dollars() const{ if (amount < 0) return (-amount) / 100; else return amount / 100;}….
Private:long amount;
}
![Page 40: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/40.jpg)
40
Member function -contostream& operator<< (ostream& out, const Currency& X)
{
if (X.sgn == minus) out << ‘-’;
out << ‘$’ << x.dollars << ‘.’;
if (X.cents < 10) out << “0”;
out << X.cents;
return out;
}
![Page 41: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/41.jpg)
41
Friend Functions/Operators• A friend function is a function defined outside of the class scope.
Note: there is no scope resolution
• The keyword “friend” is used in the class specification
• A friend function of a class has access to the private members of the class
• Inside the function definition, the dot operator is required to access the class data members.
![Page 42: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/42.jpg)
42
Classes with Dynamic Data Members
• Class constructor(s)
• Class destructor
• Class copy-constructor
• Class deep copy function member
![Page 43: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/43.jpg)
43
Classes with Dynamic Data Members• The following member functions are needed
Class constructor(s) initializationallocate space for dynamic dataInvoked automatically when an object is created
Class destructor free space allocated for dynamic dataInvoked automatically when an object goes out of scope
Class copy-constructorneeded to do deep copying. Invoked automatically in the
following situationsInitialization at declarationAn object parameter is passed by valueAn object is the function return value
Class deep copy function member: Must be invoked explicitly
![Page 44: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/44.jpg)
44
The Date Class Exampleclass Date {
public:
Date(int, int, int, char* ); // Constructor
Date(void ); // default Constructor
Date(const Date& ) // Copy-constructor
~Date( void); // Destructor
void DeepCopy (Date OtherDate); // Deep copy
….
void get(int&, int&, int&);
private:
int month, day, year;
char* msg; //pointer to char string
};
![Page 45: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/45.jpg)
45
Class ConstructorsDate::Date(void ) //default constructor
{
day = 1; month = 1; year = 1;
msg = NULL; //msg points to NULL
}
Date::Date(int d, int m, int y, char* msgstr)
{
day = d; month = m; year = y;
//Allocate memory for a new string of char
msg = new char[strlen(msgstr) + 1];
//copy msgstr into msg
strcpy(msg, msgstr);
}
![Page 46: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/46.jpg)
46
Class Constructors class Date {
public:
Date(int =1, int =1, int =1, char* =NULL );
//Constructor with default parameters
Date(const Date& ) // Copy-constructor
~Date(void ); // Destructor
void DeepCopy (Date OtherDate); //Deep copy
void get(int&, int&, int&);
private:
int month, day, year;
char* msg; //pointer to char string
};
![Page 47: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/47.jpg)
47
Class Constructors
Example: Date X, Y(10, 5, 1995);
Date Z(9, 2, 2002, "Labor Day");
Date A[100];
• When declaring an array of objects, the default constructor is used to initialize each object in the array.
• If no default constructor (and no default parameters), the array objects will NOT be initialized.
![Page 48: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/48.jpg)
48
Needed to do deep copying. Invoked automatically in the following situations:
Initialization at declaration
An object parameter is passed by value
An object is a function return value
Copy Constructor
![Page 49: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/49.jpg)
49
Copy Constructorclass ClassName {
public:
………
ClassName(const ClassName& SomeObject);private:
………
};
A copy constructor is needed only if some data members are dynamic data
The name of the constructor is the same as the name of the class
The parameter is a reference to an object of the class type.
![Page 50: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/50.jpg)
50
Date Copy-Constructor
//Implementation of Date copy-constructor
Date::Date(const Date& OtherDate)
{
// copy static data members
month = OtherDate.month; day = OtherDate.day;
year = OtherDate.year;
//copy dynamic data member
msg = new char[strlen(OtherDate.msg)+1];
strcpy(msg, OtherDate.msg);
}
Example: Date X(9, 2, 2002, "Labor Day");
Date Y= X; // Initialization at declaration
![Page 51: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/51.jpg)
51
Class Destructor
Date::~Date(void)
{
// De-allocate data pointed to by msg.
delete [ ] msg;
}
• Invoked automatically when an object gets out of scope
• Needed when a class uses dynamic data members to de-allocate memory
![Page 52: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/52.jpg)
52
Deep vs. Shallow Copying of Dynamic Data Members
• What happens when the following function is executed?
#include "date.h"
int main( void)
{
Date D1(10,10,2002, “Al Birth Day”);
Date D2(2, 5, 1999, “Jane Birth Day”);
D1 = D2; // Assignment Requires copying
// D2 into D1
return 0;
}
![Page 53: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/53.jpg)
53
Deep vs. Shallow Copying of Dynamic Data Members
void Date::DeepCopy(const Date& X)
{
// Copy static data members
day = X.day; month = X.month; year = X.year;
//Copy dynamic data members
delete [] msg; // deallocate the original string
msg = new char[strlen(X.msg) + 1]; // allocate space for new string
strcpy(msg, X.msg); // copy the X.msg in msg
}
Example: Date D1(10,10,1772, “Al Birth Day”);
Date D2(2, 5, 1999, “Jane Birth Day ”);
D1.DeepCopy( D2); // Assignment
![Page 54: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/54.jpg)
54
Overloading the = operator with deep copy
class Date {
public:
Date(int=1, int=1, int=1, char* = 0 ); //Constructor
Date(const Date& ) // Copy-constructor
~Date( ); // Destructor
void operator = (Date OtherDate); // deep copy assignment
void get(int&, int&, int&);
private:
int month, day; year;
char* msg; //pointer to char string
};
![Page 55: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/55.jpg)
55
Overloading the = operator with deep copy
void Date::operator = (Date X)
{
// Copy static data members
day = X.day; month = X.month; year = X.year;
//Copy dynamic data members
delete [] msg; // deallocate the original string
msg = new char[strlen(X.msg) + 1]; // space for new string
strcpy(msg, X.msg); // copy the X.msg in msg
}
![Page 56: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/56.jpg)
56
Example: Date D1(10,10,1772, “Al Birth”);
Date D2(2, 5, 1999, “Jane Birth”);
D1 = D2;
![Page 57: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/57.jpg)
57
More on C++• preprocessor
– to ensure that compilation is done only once
#ifndef Preprocessor_Identifier
#define Preprocessor_Identifier
class definition
#endif
![Page 58: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/58.jpg)
58
Testing
To expose the presence of errors
• test data
• test set
• designing test data
– black box method
– white box method
![Page 59: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/59.jpg)
59
White box methods
• statement coverage
• decision coverage
• clause coverage
• execution path coverage
![Page 60: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/60.jpg)
60
Debugging suggestions
• logical reasoning
• program trace
• make sure the correction does not introduce new errors
• use incremental testing and debugging
![Page 61: 1 Overview of C++ CS3304 - Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.](https://reader033.fdocuments.net/reader033/viewer/2022051516/56649f555503460f94c78f1c/html5/thumbnails/61.jpg)
61
End of Chapter 1