Engineering Problem Solving With C An Object Based Approach - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Engineering Problem Solving With C An Object Based Approach

Description:

rational(int n, int d); void input (istream & istr ) const; ... double x, y; // The anchor point of the object. int orientation; //orientation ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 30
Provided by: marci64
Category:

less

Transcript and Presenter's Notes

Title: Engineering Problem Solving With C An Object Based Approach


1
Engineering Problem Solving With CAn Object
Based Approach
  • Chapter 8
  • Introduction to Classes

2
Classes
  • The building blocks of object oriented
    programming
  • Include function members and data members
  • A well designed class is as easy to use as a
    pre-defined data type

3
Designing a Class
  • Desired operations
  • Member functions or methods
  • constructors
  • accessor functions
  • overloaded operators
  • (functions that do the work)
  • Required data members

4
Writing a Class Definition
  • A class definition has two parts
  • Class declaration
  • defines name of class, data members, and
    prototypes for member functions
  • Class implementation
  • implements the member functions

5
Class Declaration
  • Name of the class is specified using the key word
    class
  • Body of the class declaration includes
  • declaration statements for the data members
  • function prototypes
  • Keywords public, private and protected specify
    the accessibility of the class members

6
Example - Date Class Declaration
  • include ltiostreamgt
  • using namespace std
  • class Date
  • //class body
  • public
  • void input(istream) // I/O methods
  • void print(ostream) const
  • void setDate(int mo, int d, int y)
  • int get_day() const // access methods
  • int get_month() const
  • int get_year() const
  • private
  • int day, month, year

7
Date Class
  • Name of the class is Date
  • Three private data members
  • Six public function members
  • Data members can only be accessed by the member
    functions

8
Class Implementation
  • Includes
  • Function definitions for all function members
  • Scope Resolution Operator () specifies a
    function as a member of a class

9
Implementation of Date Class
  • include Date.h
  • void Dateinput(istream in)
  • in gtgt month gtgt day gtgt year
  • void Dateprint(ostream out) const
  • out ltlt month ltlt/ ltltday ltlt / ltlt year
  • void Dateset_date(int m, int d, int y)
  • month m
  • day d
  • year y

10
Accessor Functions
  • Required to access private data members
  • Complete set should be provided
  • Our Date class requires 3 accessor functions

int get_day() const int get_month() const int
get_year() const
  • const should be placed at the end of any
    function that is not intended to modify the
    calling object.

11
Initializing Objects
  • Constructor functions are used to initialize
    objects at the time they are declared
  • Called automatically
  • Same name as the class name
  • No return value, not even void

12
Constructors for the Date Class
  • Default Constructor
  • constructor with no parameters
  • Prototype
  • Date()
  • Constructor with Parameters
  • Prototype
  • Date(int m, int d, int y)

13
Default Constructor
  • Definition
  • DateDate() month(1), day(1), year(2002)
  • or
  • DateDate()
  • month 1
  • day 1
  • year 2000
  • First definition is preferred.

14
Constructors With Parameters
  • Definition
  • DateDate(int m, int d, int y)month(m), day(d),
    year(y)
  • or
  • DateDate(int m, int d, int y)
  • month m
  • day d
  • year y

15
Using Programmer Defined Classes
  • Separate Compilation
  • Class declaration is saved in a file named
    classname.h
  • Class implementation is saved in a file named
    classname.cpp (or whatever extension your
    compiler expects)
  • .h file is included in user program and
    implementation file
  • User program is linked to implementation file

16
Using the Date Class
  • include Date.h
  • int main()
  • Date today(4,6,2002), birthday
  • //member functions are called using the dot
    operator
  • birthday.input(cin)
  • today.print(cout)
  • birthday.print(cout)
  • if(today.get_day() birthday.get_day()
  • today.get_month() birthday.get_month()
    )
  • cout ltlt happy birthday!

17
Practice!
  • Given this definition of a class for a rational
    number implement the default constructor,
    parameterized constructor, and the input
    function.
  • class rational
  • private
  • int num, denom
  • public
  • rational( ) // initialize to 1/1
  • rational(int n, int d)
  • void input (istream istr ) const
  • void output (ostream ostr) // write as
    num/denom
  • bool improper() const // true if num gt denom

18
Answer to practice!
include "rational.h"
rationalrational( )num(1),denom(1)
rationalrational(int n, int d)num(n),denom(d)

void rationalinput (istream istr )
istr gtgt num gtgt denom
19
Operators
  • Assignment operator is defined for objects of the
    same type
  • Date d1, d2
  • d1.input(cin)
  • d2 d1
  • Other operators are not predefined, but can be
    overloaded in the class definition(see chapter
    10)
  • arithmetic, relational, logical, input and output

20
Helper Functions
  • Member functions
  • Called by other member functions
  • Usually specified as private

21
Example
  • Design a class to implement a unit vector. A
    vector object will be represented by an anchor
    point (the base of the arrow), and an
    orientation(always between 0 and 360 degrees). 
    There are 2 manipulations that you can perform on
    a vector without changing its length.
  • translate (slide) the vector in the plane,
    changing its position but not its orientation.
  • rotate a vector, changing its orientation

22
Translation - change position but not orientation
23
Rotation - change orientation around anchor point
24
Class Declaration
class UnitVector public
UnitVector() //
contstructors UnitVector(double init_x,
double init_y, double init_orientation)
void rotate(double d_orient)     //
rotate the vector void
translate(double dx, double dy) // translate
the // vector. private
//helper function     void
fix_orientation()   // Calculate a legal
orientation      double x,
y                   // The anchor point of the
object.      int orientation
//orientation
25
Class Implementation
//Constructor functions UnitVectorUnitVector(dou
ble initial_x, double initial_y, double
initial_orientation) x(initial_x),
y(initial_y), orientation(initial_orientation
) fix_orientation() UnitVectorUnitVector(
) x(0), y(0), orientation 0
26
Member Function Definitions
// rotate the calling vector void
UnitVectorrotate(double d_orient)       orient
ation d_orient fix_orientation() //
translate the calling vector void
UnitVectortranslate(double dx, double dy) x
dx y dy
27
Helper Function Definition
//This function adjusts the orientation value
//Original orientation may be lt 0 or gt 360)
//Adjusted orientation is 0ltorientation lt 360.
void UnitVectorfix_orientation()  
if(orientation lt 0) orientation 360
orientation360 else orientation
orientation360
28
Passing Objects To Functions
  • Objects may be passed either "by value" or "by
    reference"
  • Example

bool UnitVectorShareBasePoint(UnitVector
v)const ( if (x v.x y v.y) return
true else return false
29
Practice!
  • Which of the statements on the right is valid
    within a main program which contains an include
    for this header file.
  • class rational
  • private
  • int num, denom
  • int LCD( )
  • public
  • rational( )
  • rational(int n, int d)
  • void input (istream istr )
  • void output (ostream ostr) const
  • void reduce ( )
  • rational A
  • rational B(5, 9)
  • input(A)
  • B.output(cout)
  • int divB.LCD()
  • A.denom 3
  • B.reduce

?
?
?
?
Write a Comment
User Comments (0)
About PowerShow.com