SlideServe
Menu
Préférences
© SlideServe · v2026

Common Errors

neil-weaver avatar
33 diapositives · 516 Views · Nov 09, 2014

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

Common Errors - slide 1
01 / 33Common Errors
1 / 17

Télécharger la présentation

Common Errors
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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;