object-oriented programming using c++
DESCRIPTION
Object-Oriented Programming Using C++. CLASS 4. Objectives. 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. Pg. 411 - Fig. 6.7a. - PowerPoint PPT PresentationTRANSCRIPT
1IDLOOPC1998.
Object-Oriented Programming
Using C++
CLASS 4
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
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
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
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
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
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
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
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;
}
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;
}
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;
}
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
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
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
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
16IDLOOPC1998.
Set and Get FunctionsOverhead
Pg. 396- Fig. 6.10
17IDLOOPC1998.
MemberwiseOverhead
Pg. 403- Fig. 6.12
18IDLOOPC1998.
Q & A
19IDLOOPC1998.