Classes -- Constructors

class myClass {
    public:
        myClass();
        myClass(const myClass &);
        myClass(const int, const int,
                const string &, const char *);
        myClass & operator=(const myClass &);
    private:
        ...
}

CS151 -- Shawn Stoffer



Classes -- Identifying Constructors


CS151 -- Shawn Stoffer



CS151 -- Shawn Stoffer



Classes -- The Default Constructor

        myClass();

CS151 -- Shawn Stoffer


Classes -- The Copy Constructor

        myClass(const myClass &);

CS151 -- Shawn Stoffer


Classes -- The Copy Constructor

        myClass(const int, const int,
            const string &, const char *);

CS151 -- Shawn Stoffer


Classes -- The Assignment Operator

        myClass & operator=(const myClass &);

CS151 -- Shawn Stoffer


Review -- Const

    myClass(const myClass &);
        myClass(const int, const int,
            const string &, const char *);
    myClass & operator=(const myClass &);
 

Why use const?

  • The parameter is not changing, and further SHOULD NOT change.
  • CS151 -- Shawn Stoffer


    Review -- Reference Parameters

        myClass(const myClass &);
            myClass(const int, const int,
                const string &, const char *);
        myClass & operator=(const myClass &);
     

    Why use reference parameters (myClass &, string &)?

  • The parameter is an object
  • Objects can be quite large
  • Passing by value COPIES the object when the function is called
  • large objects take a long time to copy
  • CS151 -- Shawn Stoffer