Upload
neil-weaver
View
25
Download
1
Tags:
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
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Common Errors
• Mismatching types
char getWidth() //WRONG { return width; }
13-2
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Common Errors
• Calling observers without ( )
cout << “Width is “ << rec.getWidth << endl;
13-3
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction to Classes
13.2
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Private Data
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Rectangle Class
class Rectangle{ private: double width; double length; public: …};
13-7
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; }
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;
};
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; }
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();
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; }
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);
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 = “”; }
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; }
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; }
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; }
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-19
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
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-22
Class Example
Private Members
Public Members
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-24
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-25
Program 13-1 (Continued)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-26
Program 13-1 (Continued)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-27
Program 13-1 (Continued)
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-29
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-30
Continues...
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-31
Contents of Rectangle.ccp Version3 (continued)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13-32
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;