CS148 Introduction to Programming II

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
93 4/11/98 CSE 143 Class Constructors [Sections ]
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Learners Support Publications Constructors and Destructors.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Constructors and Destructors
Procedural and Object-Oriented Programming
CISC181 Introduction to Computer Science Dr
Constructor & Destructor
group work #hifiTeam
Lecture 9 Concepts of Programming Languages
CS148 Introduction to Programming II
Basic C++ What’s a declaration? What’s a definition?
Classes and Data Abstraction
Chapter 9 Classes: A Deeper Look, Part 1
Introduction to Classes and Objects
Constructors and destructors
Constructors and Destructors
Classes, Constructors, etc., in C++
CS148 Introduction to Programming II
9-10 Classes: A Deeper Look.
Constructors Member Functions Inheritance
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS410 – Software Engineering Lecture #5: C++ Basics III
Classes: A Deeper Look, Part 1
Chapter 9 Introduction To Classes
Separating Interface from Implementation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
CS148 Introduction to Programming II
Constructors & Destructors
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
CS148 Introduction to Programming II
Classes and Objects Systems Programming.
(4 – 2) Introduction to Classes in C++
SPL – PS3 C++ Classes.
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects
Presentation transcript:

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

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

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

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

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

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