Presentation is loading. Please wait.

Presentation is loading. Please wait.

Anatomy of a class Part II

Similar presentations


Presentation on theme: "Anatomy of a class Part II"— Presentation transcript:

1 Anatomy of a class Part II
CSCE 121 J. Michael Moore Based on Slides created by Carlos Soto.

2 Constructor Special method / member function Initializes an object
Recall passing filename to file streams when they were declared / defined Recall RAII Primary way to support passing needed info into object to set it up correctly

3 Constructors class Student { string name; int id; public: Student (string name, int id); }; No return type. Not even void.

4 Preferred!!! Constructors Member Initialization List.
class Student { string name; int id; public: Student (string name, int id): name (name), id (id) {} }; No confusion on which is which. Preferred!!!

5 Needed for anything prior to C++ 11.
Constructors class Student { string name; int id; public: Student (string name, int id) { this->name = name; this->id = id; } }; Needed for anything prior to C++ 11. Not preferred.

6 Constructors class Student { // ... public: Student (string name, int id); }; int main() { Student joe("Joe Smith", ); }

7 Can move constructor definition outside of class definition.
Constructors Can move constructor definition outside of class definition. class Student { string name; int id; public: Student (string name, int id); }; Student::Student (string name, int id): name(name), id(id) {}

8 Overloading constructors
class Student { string name; int id; public: Student (string name, int id) : name(name), id(id) {} Student (string name) : name(name), id(0) {} };

9 No arguments / parameters.
Default constructor class Student { string name; int id; public: Student (string name, int id) : name(name), id(id) {} Student (string name) : name(name), id(0) {} Student () : name(""), id(0) {} }; No arguments / parameters.

10 Using a default constructor
class Student { // ... public: Student (string name, int id); Student (); }; int main () { Student joe("Joe Smith", ); Student amy; vector<Student> undergraduates(1000); }

11 Notes on the Default constructor
If there are no constructors defined, compiler automatically creates a default constructor. This is what happened when we declared a class before we learned about constructors. Calls default constructor on all class data types. If you define other constructors, but no default constructor, and the compiler needs one, then compiler error. You should go ahead and create your own default constructor. You know better than the compiler what default values each data member needs.

12 Using a class class Student { // ... public: Student (string name, int id); string getName(); }; int main() { Student joe("Joe Smith", ); string name = joe.getName(); }

13 class Student { // ... int id; void generateID(); public: Student (string name) : name(name) { generateID(); } string getID() { return id; }; int main() { Student joe("Joe Smith"); string id = joe.getID(); }

14 These constructors can be defined in two ways.
class Student { // ... public: Student (); Student (string name, int id); }; int main() { Student joe("Joe Smith", ); } These constructors can be defined in two ways.

15 Initialization via default constructor:
// 1st Pre C++11 Student::Student(string name, int id) { this->name = name; // assignment this->id = id; // assignment } Values set twice. Initialization via default constructor: name initialized to empty string with string’s default constructor Assignment in constructor with parameters: name is set to value passed as parameter id is set to value passed as a parameter Default constructor for string called before body of this constructor is executed.

16 Initialization in constructor with parameters
// 2nd C++ 11 and later Student::Student(string name, int id) : name(name), id(id) {} Values set once. Initialization in constructor with parameters name is initialized with parameter that is passed into string’s constructor. id is initialized with parameter


Download ppt "Anatomy of a class Part II"

Similar presentations


Ads by Google