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.

Slides:



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

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Starting Out with C++: Early Objects 5th Edition
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
C++ Review (3) Structs, Classes, Data Abstraction.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Chapter 9 Classes: A Deeper Look, Part I Part II.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Learners Support Publications Constructors and Destructors.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
classes and objects review
Introduction to Classes
Constructor & Destructor
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Chapter 14: More About Classes.
Introduction to Classes
Constructors and Destructors
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Introduction to Classes and Objects
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Classes: A Deeper Look, Part 1
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
Constructors & Destructors
More C++ Classes Systems Programming.
Copy Constructors and Overloaded Assignment
Introduction to Classes and Objects
Presentation transcript:

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 default constructor for you, one that does nothing. A simple instantiation of a class (with no arguments) calls the default constructor: Rectangle r;

Passing Arguments to Constructors 13.8 Passing Arguments to Constructors

Passing Arguments to Constructors To create a constructor that takes arguments: indicate parameters in prototype: Rectangle(double, double); Use parameters in the definition: Rectangle::Rectangle(double w, double len) { width = w; length = len; }

Passing Arguments to Constructors You can pass arguments to the constructor when you create an object: Rectangle r(10, 5);

More About Default Constructors If all of a constructor's parameters have default arguments, then it is a default constructor. For example: Rectangle(double = 0, double = 0); Creating an object and passing no arguments will cause this constructor to execute: Rectangle r;

Classes with No Default Constructor When all of a class's constructors require arguments, then the class has NO default constructor. When this is the case, you must pass the required arguments to the constructor when creating an object.

13.9 Destructors

Destructors Member function automatically called when an object is destroyed Destructor name is ~classname, e.g., ~Rectangle Has no return type; takes no arguments Only one destructor per class, i.e., it cannot be overloaded If constructor allocates dynamic memory, destructor should release it

Contents of InventoryItem.h Version1 (Continued)

Constructors, Destructors, and Dynamically Allocated Objects When an object is dynamically allocated with the new operator, its constructor executes: Rectangle *r = new Rectangle(10, 20); When the object is destroyed, its destructor executes: delete r;

Overloading Constructors 13.10 Overloading Constructors

Overloading Constructors A class can have more than one constructor Overloaded constructors in a class must have different parameter lists: Rectangle(); Rectangle(double); Rectangle(double, double);

From InventoryItem.h (Version 2)

From InventoryItem.h (Version 2)

From InventoryItem.h (Version 2)

Only One Default Constructor and One Destructor Do not provide more than one default constructor for a class: one that takes no arguments and one that has default arguments for all parameters Square(); Square(int = 0); // will not compile Since a destructor takes no arguments, there can only be one destructor for a class

Member Function Overloading Non-constructor member functions can also be overloaded: void setCost(double); void setCost(char *); Must have unique parameter lists as for constructors