/
Constructor, Copy Constructor, Destructor Constructor, Copy Constructor, Destructor

Constructor, Copy Constructor, Destructor - PowerPoint Presentation

kittie-lecroy
kittie-lecroy . @kittie-lecroy
Follow
389 views
Uploaded On 2017-12-04

Constructor, Copy Constructor, Destructor - PPT Presentation

Prepared for C I B 126 Pemrograman Berorientasi Objek By Indriani Noor Hapsari ST MT Source Walter Savitch Problem Solving using C https www3ntuedusghomeehchuaprogrammingcppcp3OOPhtmlzz220 ID: 612424

copy constructor destructor object constructor copy object destructor class function delete define type argument members pass return member special

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Constructor, Copy Constructor, Destructo..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Constructor, Copy Constructor, Destructor

Prepared for

C

I

B

126

Pemrograman

Berorientasi

Objek

By

Indriani

Noor

Hapsari

, ST, MT

Source:

Walter Savitch, Problem Solving using C

++.

https://

www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html#zz-2.20

Slide2

Constructor

Description

A

constructor is a special member function in a class whose purpose is usually to initialize the members of an object.A constructor is easy to recognize because:It has the same name as the class.It has no return type.

2Slide3

Constructor

Declaration

A constructor must have the same name as its class.

Constructor cannot return values, even void.Normally, constructors are declared as public.3Slide4

A copy constructor constructs a new object by copying an existing object of the same type. In other words, a copy constructor takes an argument, which is an object of the same class.

If you do not define a copy constructor, the compiler provides a default which copies all the data members of the given object

.

The copy constructor is particularly important. When an object is passed into a function by value, the copy constructor will be used to make a clone copy of the argument.Copy ConstructorSlide5

Example

Copy ConstructorSlide6

Pass-by-value for object means calling the copy constructor. To avoid the overhead of creating a clone copy, it is usually better to

pass-by-reference-to-

const

, which will not have side effect on modifying the caller's object.Copy ConstructorSlide7

A destructor, similar to constructor, is a special function that has the same name as the

classname

, with a prefix ~, e.g., ~Circle(). Destructor is called implicitly when an object is destroyed.

If you do not define a destructor, the compiler provides a default, which does nothing.If your class contains data member which is dynamically allocated (via new or new[] operator), you need to free the storage via delete or delete[]DestructorSlide8

Example

Destructor

delete [] a;Slide9

End of slides.