1 / 13

Learning Objectives

Learning Objectives. Review of the object oriented design goals. Review of C++ classes data members member functions (methods) constructors destructors. C++ Classes. Classes enable programmers to model objects that have attributes (data members) and

chelsi
Download Presentation

Learning Objectives

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. Learning Objectives • Review of the object oriented design goals. • Review of C++ classes • data members • member functions (methods) • constructors • destructors CPSC 231 C++ Review

  2. C++ Classes • Classes enable programmers to model objects that have • attributes (data members) and • behaviors (member functions or methods) • Once a class has been defined, the class name can be used to declare objects of that class. CPSC 231 C++ Review

  3. A Simple C++ Class #include <iostream.h> // ------------ a Box class class Box { private: int height, width, depth; // private data members public: Box(int, int, int); // constructor function ~Box(); // destructor function int volume(); // member function (computev olume) }; CPSC 231 C++ Review

  4. Class Box ---------- the constructor function Box::Box(int ht=0, int wd=0, int dp=0) { height = ht; width = wd; depth = dp; } // ---------- the destructor function Box::~Box() { // does nothing } // CPSC 231 C++ Review

  5. Class Box cont. // -------- member function to compute the Box's volume int Box::volume() { return height * width * depth; } // -------- an application to use the box int main() { Box b, thisbox(7,8,9); // declare a Box cout <<b.volume()<<' '<<thisbox.volume(); // compute & display return 0; } CPSC 231 C++ Review

  6. Example of a SalesPerson Class // salesp.h p 384 of D&D // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public: SalesPerson(); // constructor void getSalesFromUser(); // get sales figures from keyboard void setSales( int, double ); // user supplies data void printAnnualSales(); CPSC 231 C++ Review

  7. SalesPerson class definition cont. private: double totalAnnualSales(); // utility function double sales[ 12 ]; // 12 monthly sales figures }; #endif CPSC 231 C++ Review

  8. Member Functions for Class SalesPerson // 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; } CPSC 231 C++ Review

  9. “get” function getSalesFromUser // Function to get 12 sales figures from the user // at the keyboard 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 ); } } CPSC 231 C++ Review

  10. “set” function setSales // Function to set one of the 12 monthly sales figures. // Note that the month value must be from 0 to 11. void SalesPerson::setSales( int month, double amount ) { if ( month >= 0 && month < 12 && amount > 0 ) sales[ month ] = amount; else cout << "Invalid month or sales figure" << endl; } CPSC 231 C++ Review

  11. “print” function printAnnualSales // Print the total annual sales void SalesPerson::printAnnualSales() { cout << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << "\nThe total annual sales are: $" << totalAnnualSales() << endl; } CPSC 231 C++ Review

  12. “utility” function totalAnnualSales // 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; } CPSC 231 C++ Review

  13. Sample driver // Compile with salesp.cpp #include <iostream.h> #include "salesp.h" int main() { SalesPerson s; // create SalesPerson object s s.getSalesFromUser(); // note simple sequential code s.printAnnualSales(); // no control structures in main return 0; } CPSC 231 C++ Review

More Related