33
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Common Errors When a function receives a value, it must have a placeholder. void setWidth(double) //WRONG { width = w; } ALWAYS INCLUDE FORMAL PARAMETERS! void setWidth(double w) 13-1

Common Errors

Embed Size (px)

DESCRIPTION

Common Errors. When a function receives a value, it must have a placeholder. void setWidth (double) //WRONG { width = w; } ALWAYS INCLUDE FORMAL PARAMETERS! void setWidth (double w). Common Errors. Mismatching types char getWidth () //WRONG { return width; }. - PowerPoint PPT Presentation

Citation preview

Page 1: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Common Errors

• When a function receives a value, it must have a placeholder.

void setWidth(double) //WRONG { width = w; }

ALWAYS INCLUDE FORMAL PARAMETERS!void setWidth(double w)

13-1

Page 2: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Common Errors

• Mismatching types

char getWidth() //WRONG { return width; }

13-2

Page 3: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Common Errors

• Calling observers without ( )

cout << “Width is “ << rec.getWidth << endl;

13-3

Page 4: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Common Errors

• Declaring objects when writing constructors

class Rectangle{ private: double width; double length; public: Rectangle myRect(double w, double l) { width = w; length = l; }

13-4

Page 5: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Introduction to Classes

13.2

Page 6: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Private Data

Page 7: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Rectangle Class

class Rectangle{ private: double width; double length; public: …};

13-7

Page 8: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-8

Inline Member Functions

• Member functions can be defined– inline: in class declaration– after the class declaration

• Inline appropriate for short function bodies:int getWidth() const { return width; }

Page 9: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-9

Introduction to Classes

• Objects are created from a class• Format:

class ClassName{

declaration;declaration;

};

Page 10: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-10

Accessors: The Get Functions

• Accessor: function that retrieves a value from a private member variable.

typeOfPrivateData getNameofPrivateData(){ return nameOfPrivateData; }

private:

double width;…

public: … double getWidth() { return width; }

Page 11: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Rectangle Class

class Rectangle{ private: double width; double length; public: … double getWidth() { return width; } double getLength() { return length; }

};

13-11

CLIENT EXAMPLE

Rectangle yard;

cout << “My yard’s width is “ << yard.getWidth();cout << “My yard’s length is “ << yard.getLength();

Page 12: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-12

Mutators: The Set Functions

• Mutator: a member function that stores a value in a private member variable, or changes its value in some way

void setNameofPrivateData(typeOfPrivateData firstLetPrivateData ){ nameOfPrivateData = firstLetPrivateData; }

private:

double width;…

public: … void setWidth(double w) { width = w; }

Page 13: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Rectangle Class Mutators

class Rectangle{ private: double width; double length; public: … void setWidth(double w) { width = w; } void setLength(double l) { length = l; }};

13-13

CLIENT EXAMPLE

Rectangle yard;

yard.setWidth(37.5);double len = 30.;yard.setLength(len);

Page 14: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Default Constructor: Initialize Private Data

• Member function that is automatically called when an object is created; Purpose is to initialize or “construct” an object; Nothing in parentheses

NameofClass(){ nameOfPrivateInt1 = 0; nameOfPrivateString1 = “”; }

Page 15: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CLIENT EXAMPLE

Rectangle yard;//yard.width is 0.0//yard.length is 0.0

Default Constructor: Initialize Private Data

NameofClass(){ nameOfPrivateInt1 = 0; nameOfPrivateString1 = “”; }

class Rectangle{ private: double width; double length; public: Rectangle() { width = 0.0; length = 0.0; }

Page 16: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Constructor with Parameters

• Member function that is automatically called when an object is created; Purpose is to initialize or “construct” an object

NameofClass(typeOfPrivate1 Private1FirstLetter, typeOfPrivate2 Private2FirstLetter, … )

{ nameOfPrivate1 = Private1FirstLetter; nameOfPrivate2 = Private2FirstLetter; }

Page 17: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CLIENT EXAMPLE

Rectangle table(3.,4.);//yard.width is 3.0//yard.length is 4.0

Constructor with Parameters

NameofClass(typeOfPrivate1 Private1FirstLetter, typeOfPrivate2 Private2FirstLetter, … ){ nameOfPrivate1 = Private1FirstLetter; nameOfPrivate2 = Private2FirstLetter; }

class Rectangle{ private: double width; double length; public: Rectangle(double w, double l) { width = w; length = l; }

Page 18: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Client

• Rectangle r;• Rectangle s(3., 4.);

• r.SetWidth(4.5);• cout << “Length is “ << s.getLength();

13-18

Page 19: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-19

Page 20: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Rectangle Class

class Rectangle{ private: double width; double length; public: … void setWidth(double w) { width = w; } void setLength(double l) { length = l; }};

13-20

Page 21: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Rectangle Class

class Rectangle{ private: double width; double length; public: Rectangle(double len, double wid); // Constructor { length = len; width = wid; } void setWidth(double wid) { width = wid; } void setLength(double len) { length = len; }

double getWidth() const { return width; } double getLength() const { return length; }

double getArea() const { return width * length; }};

13-21

Page 22: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-22

Class Example

Private Members

Public Members

Page 23: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-23

Defining an Instance of a Class

• An object is an instance of a class• Defined like structure variables:

Rectangle r;• Access members using dot operator:

r.setWidth(5.2);cout << r.getWidth();

• Compiler error if attempt to access private member using dot operator

Page 24: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-24

Page 25: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-25

Program 13-1 (Continued)

Page 26: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-26

Program 13-1 (Continued)

Page 27: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-27

Program 13-1 (Continued)

Page 28: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-28

Constructors

• Member function that is automatically called when an object is created

• Purpose is to construct an object

• Constructor function name is class name

• Has no return type

Page 29: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-29

Page 30: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-30

Continues...

Page 31: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-31

Contents of Rectangle.ccp Version3 (continued)

Page 32: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-32

Page 33: Common Errors

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-33

Default Constructors

• A default constructor is a constructor that takes no arguments.

• If you write a class with no constructor at all, C++ will write a default constructor for you, one that does nothing.

• A simple instantiation of a class (with no arguments) calls the default constructor:

Rectangle r;