28
Classes Lecture 5 Sections 10.11 - 10.13 Robb T. Koether Hampden-Sydney College Fri, Jan 27, 2017 Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 1 / 28

Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

ClassesLecture 5

Sections 10.11 - 10.13

Robb T. Koether

Hampden-Sydney College

Fri, Jan 27, 2017

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 1 / 28

Page 2: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

1 Classes

2 Class DesignConstructors and DestructorsInspectors and MutatorsOperators and Facilitators

3 Member Access

4 Structs

5 Assignment

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 2 / 28

Page 3: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Outline

1 Classes

2 Class DesignConstructors and DestructorsInspectors and MutatorsOperators and Facilitators

3 Member Access

4 Structs

5 Assignment

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 3 / 28

Page 4: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Classes

Class Constructclass Name{

// Member function prototypes// Declarations of data members

};

The class construct in C++ is an enhancement of the structconstruct in C.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 4 / 28

Page 5: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Classes

The Class Pointclass Point{

public:

// Various member functions

private:int x;int y;

};

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 5 / 28

Page 6: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Class Members

Members may be designated public, private, or protected.Members are private by default.Members may be objects or functions.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 6 / 28

Page 7: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Outline

1 Classes

2 Class DesignConstructors and DestructorsInspectors and MutatorsOperators and Facilitators

3 Member Access

4 Structs

5 Assignment

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 7 / 28

Page 8: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Class Design

Many of the member functions of a class fall into one of thefollowing categories.

ConstructorDestructorInspectorMutatorOperatorFacilitator

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 8 / 28

Page 9: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Outline

1 Classes

2 Class DesignConstructors and DestructorsInspectors and MutatorsOperators and Facilitators

3 Member Access

4 Structs

5 Assignment

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 9 / 28

Page 10: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Constructors and Destructors

A constructor creates a new object in a class.A destructor destroys an object.A class may have many constructors, but only one destructor.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 10 / 28

Page 11: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Constructors and Destructors

Example (Point Class)Point() : x(0), y(0) {}Point(int xval, int yval) : x(xval), y(yval) {}˜Point() {}

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 11 / 28

Page 12: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Outline

1 Classes

2 Class DesignConstructors and DestructorsInspectors and MutatorsOperators and Facilitators

3 Member Access

4 Structs

5 Assignment

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 12 / 28

Page 13: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Inspectors and Mutators

An inspector returns an attribute of an object.A mutator modifies an attribute of an object.Typically, the attributes involved are the data members.Typically, inspectors are const and take no parameters.Typically, mutators take one or more parameters and they returnvoid.Whenever appropriate, the mutators should test the parametersfor validity.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 13 / 28

Page 14: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Inspectors and Mutators

Example (Point Class)int getX() const {return x;}void setX(int xval) {x = xval;}

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 14 / 28

Page 15: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Inspectors and Mutators

Example (Point Class)int x() const {return m_x;}void x(int xval) {m_x = xval;}

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 15 / 28

Page 16: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Outline

1 Classes

2 Class DesignConstructors and DestructorsInspectors and MutatorsOperators and Facilitators

3 Member Access

4 Structs

5 Assignment

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 16 / 28

Page 17: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Operators and Facilitators

An operator is a function that is invoked by a symbol such as + or*.A facilitator is invoked by a non-member operator to perform thefunction of the operator.Nearly every class should have the following operators.

The assignment operator =.The output operator <<.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 17 / 28

Page 18: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Equality and Relational Operators

It is always sensible to define the operators == and !=.It is not always sensible to define the operators <, >, <=, and >=.If there is a sensible meaning of <, then the other three can bedefined as well.If < is undefined, then operations such as sorting are impossible.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 18 / 28

Page 19: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Input and Output Operators

The output operator << can be very useful for debugging.The input and output operators >> and << should always be“compatible.”That is, the input operator should be designed to read the sameformat that the output operator uses.The Point class outputs a point as (2, 3).

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 19 / 28

Page 20: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Input and Output Operators

Example (Point Class)void output(ostream& out) const

{out << ’(’ << x << ", " << y << ’)’;}ostream& operator<<(ostream& out, const Point& p)

{p.output(out); return out;}

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 20 / 28

Page 21: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Outline

1 Classes

2 Class DesignConstructors and DestructorsInspectors and MutatorsOperators and Facilitators

3 Member Access

4 Structs

5 Assignment

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 21 / 28

Page 22: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Member-Access Operators

Member-Access OperatorsPoint p;Point* ptr = &p;cout << p.getX() << ’ ’ << p.getY();cout << ptr->getX() << ’ ’ << ptr->getY();

Use the dot operator . to access members through an object.Use the arrow operator -> to access members through a pointerto an object.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 22 / 28

Page 23: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Outline

1 Classes

2 Class DesignConstructors and DestructorsInspectors and MutatorsOperators and Facilitators

3 Member Access

4 Structs

5 Assignment

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 23 / 28

Page 24: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Structs

Struct Constructstruct Name{

// Declarations of data members};

C and C++ support the struct construct.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 24 / 28

Page 25: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Structs

The Struct Pointstruct Point{

int x;int y;

};

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 25 / 28

Page 26: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Struct Members

In C:Struct members are public.Members may be objects, but not functions.

In C++:Struct members may be designated public, private, orprotected.Members are public by default.Members may be objects or functions.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 26 / 28

Page 27: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Outline

1 Classes

2 Class DesignConstructors and DestructorsInspectors and MutatorsOperators and Facilitators

3 Member Access

4 Structs

5 Assignment

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 27 / 28

Page 28: Classes - Lecture 5 Sections 10.11 - 10people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Constructors and Destructors Inspectors and Mutators Operators and Facilitators 3

Assignment

AssignmentRead Sections 10.11 - 10.13.

Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 28 / 28