1 / 33

Common Errors

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; }.

neil-weaver
Download Presentation

Common Errors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. Common Errors • Mismatching types char getWidth() //WRONG { return width; }

  3. Common Errors • Calling observers without ( ) cout << “Width is “ << rec.getWidth << endl;

  4. 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; }

  5. Introduction to Classes 13.2

  6. The Private Data

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

  8. Inline Member Functions • Member functions can be defined • inline: in class declaration • after the class declaration • Inline appropriate for short function bodies: intgetWidth() const { return width; }

  9. Introduction to Classes • Objects are created from a class • Format: class ClassName { declaration; declaration; };

  10. Accessors: The Get Functions • Accessor: function that retrieves a value from a private member variable. typeOfPrivateDatagetNameofPrivateData() { return nameOfPrivateData; } private: double width; … public: … double getWidth() { return width; }

  11. Rectangle Class class Rectangle { private: double width; double length; public: … double getWidth() { return width; } double getLength() { return length; } }; CLIENT EXAMPLE Rectangle yard; cout << “My yard’s width is “ << yard.getWidth(); • cout << “My yard’s length is “ << yard.getLength();

  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(typeOfPrivateDatafirstLetPrivateData ) { nameOfPrivateData = firstLetPrivateData; } private: double width; … public: … void setWidth(double w) { width = w; }

  13. Rectangle Class Mutators class Rectangle { private: double width; double length; public: … void setWidth(double w) { width = w; } void setLength(double l) { length = l; } }; CLIENT EXAMPLE Rectangle yard; yard.setWidth(37.5); • double len = 30.; • yard.setLength(len);

  14. 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 = “”; }

  15. Default Constructor: Initialize Private Data NameofClass() { nameOfPrivateInt1 = 0; nameOfPrivateString1 = “”; } class Rectangle { private: double width; double length; public: Rectangle() { width = 0.0; length = 0.0; }

  16. 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; }

  17. 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; }

  18. The Client • Rectangle r; • Rectangle s(3., 4.); • r.SetWidth(4.5); • cout << “Length is “ << s.getLength();

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

  20. 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; } };

  21. Class Example Private Members Public Members

  22. 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

  23. Program 13-1 (Continued)

  24. Program 13-1 (Continued)

  25. Program 13-1 (Continued)

  26. 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

  27. Continues...

  28. Contents of Rectangle.ccp Version3 (continued)

  29. 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;

More Related