130 likes | 240 Views
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
E N D
Learning Objectives • Review of the object oriented design goals. • Review of C++ classes • data members • member functions (methods) • constructors • destructors CPSC 231 C++ Review
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
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
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
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
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
SalesPerson class definition cont. private: double totalAnnualSales(); // utility function double sales[ 12 ]; // 12 monthly sales figures }; #endif CPSC 231 C++ Review
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
“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
“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
“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
“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
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