Transcript
Page 1: Object-Oriented  Programming  Using C++

1IDLOOPC1998.

Object-Oriented Programming

Using C++

CLASS 4

Page 2: Object-Oriented  Programming  Using C++

2IDLOOPC1998.

Ob

ject

ives

• Understand the purpose of constructors and destructors

• Use default arguments with constructors

• Understand how objects are assigned to one another

• Set up an interface separate from an implementation

Page 3: Object-Oriented  Programming  Using C++

3IDLOOPC1998.

Interface#ifndef SALESP_H#define SALESP_Hclass SalesPerson {public:

SalesPerson( ); //constructorvoid getSalesFromUser( ); // get sales figures

from keyboardvoid setSales(int,double); //User supplies

one month’s//sales figures.

void printAnnualSales( );private:

double sales[12]; //12 monthly sales figures

double totalAnnualSales(); //utility function};#endif

Pg. 411 - Fig. 6.7a

Page 4: Object-Oriented  Programming  Using C++

4IDLOOPC1998.

Implementation//SALESP.CPP// member functions for class SalesPerson#include <iostream.h>#include <iomanip.h>#include “salesp.h”// Constructor function initializes arraySalesPerson::SalesPerson( ){ for (int i = 0; i < 12; i++)

sales [ i ] = 0.0;}

Pg. 411- Fig. 6.7b

Page 5: Object-Oriented  Programming  Using C++

5IDLOOPC1998.

Implementation

void SalesPerson::getSalesFromUser( ){

double salesFigure;

for ( int i = 0; i < 12; i++ ) {cout << “Enter sales amount for month” << i + 1 << “: ”;cin >> salesFigure ;

setSales(i, salesFigure);}

}

Pg. 412- Fig. 6.7b

Page 6: Object-Oriented  Programming  Using C++

6IDLOOPC1998.

Implementation

//Function to set one of the 12 mo. sales figs.//Note that the month value must be from 0 to 11.

void SalesPerson::setSales(int month, double amount){ if (month >= 0 && month <12 && amt > 0)

sales[month-1] = amountt;else cout << “Invalid month for sales fig” << endl;

}

Pg. 412- Fig. 6.7b

Page 7: Object-Oriented  Programming  Using C++

7IDLOOPC1998.

//Private utility function to total annual salesdouble SalesPerson::totalAnnualSales( ){ double total = 0.0;

for (int i = 0; i < 12; i++) total += sales [ i ];

return total;}

Pg. 412- Fig. 6.7b

Page 8: Object-Oriented  Programming  Using C++

8IDLOOPC1998.

// Print the total annual salesvoid SalesPerson::printAnnualSales( ){

cout << setprecision(2)<< setiosflags(ios::fixed | ios::showpoint)<<endl << “The total annual sales are: $”<<totalAnnualSales( ) << endl;

}

Pg. 412- Fig. 6.7b

Page 9: Object-Oriented  Programming  Using C++

9IDLOOPC1998.

Main Driver

Pg. 413- Fig. 6.7c

// FIG06_07.CPP// Demonstrating a utility function// Compile with SALESP.CPP

#include “salesp.h”int main ( ){

SalesPerson s;

s.getSalesFromUser( );s.printAnnualSales( );return 0;

}

Page 10: Object-Oriented  Programming  Using C++

10IDLOOPC1998.

Main Driver

Pg. 412- Fig. 6.7c

// FIG06_07.CPP// Demonstrating a utility function// Compile with SALESP.CPP#include “salesp.h”main( )int main ( ){

SalesPerson s;

s.getSalesFromUser( );s.printAnnualSales( );return 0;

}

Page 11: Object-Oriented  Programming  Using C++

11IDLOOPC1998.

Main Driver

Pg. 413- Fig. 6.7c

// FIG06_07.CPP// Demonstrating a utility function// Compile with SALESP.CPP

#include “salesp.h”main( )int main ( ){

SalesPerson s;

s.getSalesFromUser( );s.printAnnualSales( );return 0;

}

Page 12: Object-Oriented  Programming  Using C++

12IDLOOPC1998.

//TIME2.H// Declaration of the time class.// Member functions defined in TIME2.CPP// prevent multiple inclusions of header file#ifndef TIME2_H#define TIME2_HclassTime {public:

Time(int = 0, int = 0, int = 0); //default constructorvoid setTime(int, int, int);void printMilitary( );void printStandard( );

private:int hour;int minute;int second; };

#endif

Pg. 415- Fig. 6.8a

Page 13: Object-Oriented  Programming  Using C++

13IDLOOPC1998.

//TIME2.H// Declaration of the time class.// Member functions defined in TIME2.CPP// prevent multiple inclusions of header file#ifndef TIME2_H#define TIME2_HclassTime {public:

Time(int = 0, int = 0, int = 0); //default constructorvoid setTime(int, int, int);void printMilitary( );void printStandard( );

private:int hour;int minute;int second; };

#endif

Pg. 415- Fig. 6.8a

Page 14: Object-Oriented  Programming  Using C++

14IDLOOPC1998.

// TIME2.CPP// member function definitions for Time class.

#include <iostream.h>#include “time2.h”

// Constructor function to initialize private data.// Default values are 0 (see class definition).Time::Time(int hr, int min, int sec)

{ setTime(hr, min, sec); }

Pg. 415- Fig. 6.8b

Page 15: Object-Oriented  Programming  Using C++

15IDLOOPC1998.

// FIG6_8.CPP

#include <iostream.h>#include “time2.h”

main( ){

Time t1, t2(2), t3(21, 34), t4(12, 25, 42), t5(27, 74, 99);

}

Pg. 416 - Fig. 6.8c

Page 16: Object-Oriented  Programming  Using C++

16IDLOOPC1998.

Set and Get FunctionsOverhead

Pg. 396- Fig. 6.10

Page 17: Object-Oriented  Programming  Using C++

17IDLOOPC1998.

MemberwiseOverhead

Pg. 403- Fig. 6.12

Page 18: Object-Oriented  Programming  Using C++

18IDLOOPC1998.

Q & A

Page 19: Object-Oriented  Programming  Using C++

19IDLOOPC1998.


Recommended