330 likes | 480 Views
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; }.
E N D
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 • Calling observers without ( ) cout << “Width is “ << rec.getWidth << endl;
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; }
Rectangle Class class Rectangle { private: double width; double length; public: … };
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; }
Introduction to Classes • Objects are created from a class • Format: class ClassName { declaration; declaration; };
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; }
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();
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; }
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);
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 = “”; }
Default Constructor: Initialize Private Data NameofClass() { nameOfPrivateInt1 = 0; nameOfPrivateString1 = “”; } class Rectangle { private: double width; double length; public: Rectangle() { width = 0.0; length = 0.0; }
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; }
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; }
The Client • Rectangle r; • Rectangle s(3., 4.); • r.SetWidth(4.5); • cout << “Length is “ << s.getLength();
Rectangle Class class Rectangle { private: double width; double length; public: … void setWidth(double w) { width = w; } void setLength(double l) { length = l; } };
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; } };
Class Example Private Members Public Members
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
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
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;