Learning objectives
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Learning Objectives PowerPoint PPT Presentation


  • 64 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Learning objectives

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

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

    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

    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

    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

    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

    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

    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

    “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

    “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” 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

    “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

    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


  • Login