Upload
others
View
11
Download
0
Embed Size (px)
Citation preview
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Structs
The Struct Pointstruct Point{
int x;int y;
};
Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 25 / 28
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
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
Assignment
AssignmentRead Sections 10.11 - 10.13.
Robb T. Koether (Hampden-Sydney College) Classes Fri, Jan 27, 2017 28 / 28