CS 202 Fall 2013  >  Notes for Thursday, September 12, 2013

CS 202 Fall 2013
Notes for Thursday, September 12, 2013

Classes I Day 2 [13.6–13.10]

Inline Functions

C++ has a rule that nothing can have more than one definition. An exception to this rule is inline functions. Member functions defined inside a class definition are inline; this is why it is okay to include the same header in multiple source files. Member functions defined outside a class definition must follow the one-definition rule; this is why we do not put such definitions in a header file, which might be included in multiple source files.

Constructors

A constructor is a class member function that is called when an object of that class is created. Write a constructor as a member function whose name is the same as the name of the class. A constructor has no return value and is never const. Typically, it initializes the object’s data members.

[C++]

class Foo {
public:
    ...
    Foo()
    {
        _a = 1;
        _b = 2;
    }

private:
    int _a;
    int _b;
};

We do not need to call a constructor explicitly.

[C++]

Foo x;
// x._a is now 1, and x._b is now 2

A constructor like that above, which takes no arguments, i s called a default constructor.

Note that even const objects are constructed, and the constructor is allowed to set the values of member variables. When the constructor is done, the member variables are no longer modifiable.

Constructors with Arguments

Add arguments to a constructor just like any other function. Pass arguments by putting them in parentheses after the name of a variable in its declaration.

[C++]

class Foo {
public:
    ...
    Foo(int a, int b)
    {
        _a = a;
        _b = b;
    }
    ...
};

Foo y(37, 6);
// y._a is now 37, and y._b is now 6

Overloading Constructors

We can have two functions with the same name, as long as they do not have the same parameter types. This is called overloading the function. We can do the same thing with constructors; thus a class can have multiple constructors.

Note that C++ writes a default constructor for you if you declare no constructors. The one written for you initializes each data member using its default constructor.

The Destructor

The destructor of a class is a member function that is called when an object of the class is destroyed. Destruction happens as indicated in the following table.

Kind of Object When Object is Destroyed
Automatic
normal local vars
When object goes out of scope
Static
global vars & static local vars
When the program ends
Dynamic
created with new—more on this later
When a pointer to the object is deleted

Write the destructor as a member function whose name is the same as the name of the class with a tilde (“~”) before it. The destructor has no return value and is never const.

[C++]

class Foo {
public:
    ...
    // A rather dumb destructor
    ~Foo()
    {
        cout << "Bye!" << endl;
    }
    ...
};

A do-nothing destructor will be written for you if you do not declare the destructor. When to write the destructor:

A good example of a destructor is that of classes ofstream and ifstream. Open files need to be closed. This can be done by calling member function close, if you wish. However, both of these classes have a destructor that closes the file, if it is still open at that point. This is why we are not required to call the close function in C++ file I/O.

Example Class

We incorporated some of the above ideas into class Clock from last time.

See clock.h and clock.cpp for the code for class Clock. See clock_main2.cpp for a new C++ program that uses class Clock. (The old program, clock_main.cpp, still works, too.)


CS 202 Fall 2013: Notes for Thursday, September 12, 2013 / Updated: 12 Sep 2013 / Glenn G. Chappell