1 / 19

Object-Oriented Programming Using C++

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.

amandla
Download Presentation

Object-Oriented Programming Using C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming Using C++ CLASS 4

  2. 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

  3. Pg. 411 - Fig. 6.7a Interface #ifndef SALESP_H #define SALESP_H class SalesPerson { public: SalesPerson( ); //constructor void getSalesFromUser( ); // get sales figures from keyboard void 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

  4. Pg. 411- Fig. 6.7b Implementation //SALESP.CPP // member functions for class SalesPerson #include <iostream.h> #include <iomanip.h> #include “salesp.h” // Constructor function initializes array SalesPerson::SalesPerson( ) { for (int i = 0; i < 12; i++) sales [ i ] = 0.0; }

  5. Pg. 412- Fig. 6.7b 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); } }

  6. Pg. 412- Fig. 6.7b 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; }

  7. Pg. 412- Fig. 6.7b //Private utility function to total annual sales double SalesPerson::totalAnnualSales( ) { double total = 0.0; for (int i = 0; i < 12; i++) total += sales [ i ]; return total; }

  8. Pg. 412- Fig. 6.7b // Print the total annual sales void SalesPerson::printAnnualSales( ) { cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) <<endl << “The total annual sales are: $” <<totalAnnualSales( ) << endl; }

  9. Pg. 413- Fig. 6.7c Main Driver // FIG06_07.CPP // Demonstrating a utility function // Compile with SALESP.CPP #include “salesp.h” int main ( ) { SalesPerson s; s.getSalesFromUser( ); s.printAnnualSales( ); return 0; }

  10. Pg. 412- Fig. 6.7c Main Driver // 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; }

  11. Pg. 413- Fig. 6.7c Main Driver // 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; }

  12. Pg. 415- Fig. 6.8a //TIME2.H // Declaration of the time class. // Member functions defined in TIME2.CPP // prevent multiple inclusions of header file #ifndef TIME2_H #define TIME2_H classTime { public: Time(int = 0, int = 0, int = 0); //default constructor void setTime(int, int, int); void printMilitary( ); void printStandard( ); private: int hour; int minute; int second; }; #endif

  13. Pg. 415- Fig. 6.8a //TIME2.H // Declaration of the time class. // Member functions defined in TIME2.CPP // prevent multiple inclusions of header file #ifndef TIME2_H #define TIME2_H classTime { public: Time(int = 0, int = 0, int = 0); //default constructor void setTime(int, int, int); void printMilitary( ); void printStandard( ); private: int hour; int minute; int second; }; #endif

  14. Pg. 415- Fig. 6.8b // 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); }

  15. Pg. 416 - Fig. 6.8c // 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); }

  16. Pg. 396- Fig. 6.10 Set and Get FunctionsOverhead

  17. Pg. 403- Fig. 6.12 MemberwiseOverhead

  18. Q & A

More Related