CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
CSE 332: C++ copy control II Copy Control (Part II) Review: copy control consists of 5 distinct operations –A copy constructor initializes an object by.
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II.
Copy Constructors Fall 2008 Dr. David A. Gaitros
ECE122 Feb. 22, Any question on Vehicle sample code?
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.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Introduction to Object Oriented Programming Chapter 10.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
ECE 264 Object-Oriented Software Development
Learners Support Publications Constructors and Destructors.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Programming with ANSI C ++
Haidong Xue Summer 2011, at GSU
Road Map Introduction to object oriented programming. Classes
Constructor & Destructor
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Basic C++ What’s a declaration? What’s a definition?
CMSC 202 Templates.
Constructors and Destructors
Java Programming Language
COP 3330 Object-oriented Programming in C++
Anatomy of a class Part II
Object oriented programming (OOP) Lecture No. 6
Constructors & Destructors
CS 144 Advanced C++ Programming April 30 Class Meeting
Class Circle { Float xc, yc, radius;
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors

CS-1030 Dr. Mark L. Hornick 2 Constructors - review Same name as the class Example Clock(); Called automatically when object is created Job is to initialize class objects Remember, C++ variables aren’t automatically initialized Java variables are initialized to 0, “”, etc C++ constructors have no return type not even void, as used by Java constructors You can use method overloading to supply as many constructors as you wish Each constructor method must have a different signature Unique combination of type & number of parameters: Clock::Clock(int hour); Clock::Clock(int hour, int minute); Clock::Clock(int hour, int minute, int second);

CS-1030 Dr. Mark L. Hornick 3 Constructor rules If you don’t supply any constructors… The compiler automatically supplies a default constructor Default constructor takes no parameters Doesn’t really do anything – does NOT initialize variables As well as a copy constructor… If you supply any kind of constructor The compiler will no longer automatically supply a default constructor But will still supply a copy constructor unless you supply one

CS-1030 Dr. Mark L. Hornick 4 Constructor Example Demo

CS-1030 Dr. Mark L. Hornick 5 Destructor Cleans up an object at the end of lifetime Cleans up the data members prior to their own destruction Closes things (like files), etc. Called automatically Special name Clock::~Clock() Compiler default supplied (if you don’t) Default: destroy each data member i.e. call each data member’s destructor if appropriate Does nothing with primitive data members

CS-1030 Dr. Mark L. Hornick 6 Destructor Example Demo

CS-1030 Dr. Mark L. Hornick 7 Copy Constructor Makes a new copy of an object Used i n some object declarations, when an object is used as a parameter to the constructor of a class of the same type: // create t1 using a 3-parameter // constructor: Clock t1(12, 0, 0); // create t2 as a copy of t1: Clock t2( t1 );

CS-1030 Dr. Mark L. Hornick 8 Copy Constructor Syntax In.h file class Clock { … Clock (const Clock &); …}; In.cpp file Clock::Clock (const Clock& master) { = master. ; … }

CS-1030 Dr. Mark L. Hornick 9 Copy Constructor rules If you don’t supply a copy constructor, the compiler automatically supplies one: Makes a copy of each data member from the “old” object to the “new” one This is referred to as a shallow copy; OK for may cases, but insufficient for others… …care must be used when copying pointers (more later); in such cases, the compiler-supplied copy constructor is probably inadequate C

CS-1030 Dr. Mark L. Hornick 10 Copy Constructor Also called automatically by the compiler: When objects are passed by- value as function parameters When functions return values that are objects

CS-1030 Dr. Mark L. Hornick 11 Copy Constructor Example Demo