080Constructors - Constructors Inline Functions and Static...

This preview shows page 1 - 12 out of 32 pages.

Constructors, Inline Functions, and Static Data Members Jackie Kuehn CS 140
Image of page 1

Subscribe to view the full document.

Goals Constructors Definitions Calling Destructors More Tools const parameter modifier Inline functions Static member data
Image of page 2
Constructor Introduction Constructor is the initialization or “birth” of an object. CTOR The constructor is called when the object is created. Destructor is called when an object goes out of scope or is de-allocated. It is the “death” of an object. Both the constructor and the destructor get automatically called.
Image of page 3

Subscribe to view the full document.

What is a Constructor? Constructor is a function that initializes the data members. Without a constructor, you have “very bad code”. Uninitialized data will produce an output that is garbage. Constructor’s force data to be initialized properly. Constructor is used to construct objects of a class.
Image of page 4
Declaring and Initializing Variables When variables of native types are declared there is no question about how much memory to allocate Initialization is the process of storing a value in newly declared variables
Image of page 5

Subscribe to view the full document.

Initializing Simple Variables int counter = 0; char* owner = NULL; char name[] = C++ ; double coef[3] = { 0, 1.2, 3.0 };
Image of page 6
Initializing Class Objects Classes are complex (derived) types They may have many components Example: Employee class has name, address, id, etc. Declaration of a variable that is a class (instantiating an object) requires the construction of an object by setting aside the required blocks of memory
Image of page 7

Subscribe to view the full document.

Constructor Definitions Constructors defined like any member function Except: 1. Must have same name as class 2. Cannot return a value; not even void!
Image of page 8
Constructor Definition Example Class definition with constructor: class DayOfYear { public: //Constructor initializes month and day DayOfYear(int monthValue, int dayValue) ; void input(); void output(); private: int month; int day; }
Image of page 9

Subscribe to view the full document.

Constructor Notes Notice name of constructor: DayOfYear Same name as class itself! Constructor declaration has no return-type Not even void! Constructor in public section It’s called when objects are declared If private, could never declare objects!
Image of page 10
Calling Constructors Declare objects: DayOfYear date1(7, 4), date2(5, 5); Objects are created here Constructor is called Values in parens passed as arguments to constructor Member variables month, day initialized: date1.month 7 date2.month 5 date1.dat 4 date2.day 5
Image of page 11

Subscribe to view the full document.

Image of page 12

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern