Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS148 Introduction to Programming II

Similar presentations


Presentation on theme: "CS148 Introduction to Programming II"— Presentation transcript:

1 CS148 Introduction to Programming II
Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 15: 3/21/2003 Lecture 15: 3/21/2003 CS148 Spring 2003

2 Outline C++ Classes Constructors Destructors
Specification and implementation files Lecture 15: 3/21/2003 CS148 Spring 2003

3 Class Constructors 1/2 A class constructor guarantees the initialization of a class object Implicitly invoked whenever a class object is created (never invoked using the dot notation) Has the same name as the class itself A parameterless constructor is the default constructor TimeType startTime; //default constructor is invoked TimeType startTime (10,30,0) //parameterized constructor is invoked class TimeType { public: TimeType(); //default constructor TimeType (int, int, int); //parameterized constructor void Increment(); void Write(); private: }; Lecture 15: 3/21/2003 CS148 Spring 2003

4 Class Constructors 2/2 Rules for using constructors
A constructor cannot return a function value (function declared without a return value type) When a class provides several constructors, and a class object is declared, the compiler chooses the appropriate constructor according to the supplied arguments list When a class object is declared without an argument list, the effect depends on what constructors the class provides If the class has no constructors at all, memory is allocated for the class object, but the private data members are in an uninitialized state If the class does have consructors The default constructor is invoked if there is one If no default constructor can be found, a syntax error occurs Lecture 15: 3/21/2003 CS148 Spring 2003

5 Class destructors Implicitly invoked when a class object is destroyed
A class object is destroyed when the scope it was declared in is exited Same name as a constructor, except first character is a tilde (~) Used to performed clean up, e.g., deallocation of dynamic memory allocated within a class class TimeType { public: TimeType(); //default constructor TimeType (int, int, int); //parameterized constructor void Increment(); void Write(); ~TimeType(); //Destructor private: }; Lecture 15: 3/21/2003 CS148 Spring 2003

6 Specification/Implementation files
An ADT consists of two parts: a specification and an implementation The specification describes the behavior of the data type without reference to its implementation Programming Style Specification file: a header file (.h) containing only the class declaration Implementation file: a source file (.cpp) containing functions definitions for the class member functions. Needs to include the specification file. Client code (.cpp). Uses the class public members. Needs to include the specification file. Lecture 15: 3/21/2003 CS148 Spring 2003


Download ppt "CS148 Introduction to Programming II"

Similar presentations


Ads by Google