Download presentation
Presentation is loading. Please wait.
Published by Roxanne Blair Modified over 5 years ago
1
Constructors and destructors
Dr. Bhargavi Dept of CS CHRIST
2
introduction Can we initialize the variables of objects when they are created? Similarly, when a variable of built-in type goes out of scope, can compiler automatically destroys the variable? Yes. Using: Constructors, for automatic initialization of objects. Destructors, for destroying objects when no more required.
3
Constructors Definition: special function which is invoked without explicit call whose task is to initialize objects of its class. When invoked? When object is created. Why called constructors? Bcoz it constructs values for data members. Explicit call statement is not required to call a constructor. Use? Convenient when number of obj is large.
4
Types of constructors Default constructor Parameterized constructor
Overloaded multiple constructor Constructors with default arguments Copy constructors Dynamic constructors Types of constructors Default constructor Parameterized constructor Overloaded multiple constructor Constructors with default arguments Copy constructors Dynamic constructors
5
Characteristics of constructors
They should be declared in public section. Invoked automatically at object creation. Have no return type. Thus, cannot return values. Cannot be inherited. Though, derived class can call base class constructors. May have default arguments. Cannot be virtual. We cannot refer to address of constructors An obj with constructor cannot be used as union Makes implicit call to ‘new’ and ‘delete’ operators for dynamic memory allocation
6
Default constructors If no constructor is defined, compiler calls default constructor without body. Default constructors are constructors without explicit definition and without arguments. Eg. A::A() Eg. classConstructorDefaulVal.cpp Eg. classConstructor.cpp Eg. privateConstructor.cpp
7
Parameterized constructors
Practically we cannot initialize all the objects with same values. What if I want to initialize with different values to different objects? Is it possible? Yes. How? Parameterized constructors can take arguments that can be passed with individual values for each object. Definition: Constructors that can take arguments are called parameterized constructors.
8
Parameterized constructors
Pl note that parameterized constructors require argument passing. Constructors can also be defined as inline. Constructor can accept a reference to its own class as parameter. We also call it copy constructor. Eg.6_1_ExampleOfParameterizedConstruncto r.cpp Eg. classParameterisedConstructor.cpp
9
overloading constructors
Can we copy the values of first object into second object using constructor? Yes. Lets see how. See the example given below. Eg. 6_2_OverloadedConstructors.cpp Eg. contructor_Overloading.cpp
10
Constructors with default arguments
Is it possible to define constructors with default arguments? Yes. How? Eg. complex C(float real, float imag=0); complex C(5.0); complex C(2.0,3.0); Eg. 6_2_OverloadedConstructors.cpp, we have already done. Eg. classConstructorDefaulVal.cpp, we have already done. Note: Its imp to distinguish between default constructors A::A() and default argument constructor A::A(int=0). They cannot be implemented together to avoid ambiguity.
11
Dynamic initialization of objects
Can class objects be initialized dynamically? Means? During runtime provide the initialization values to objects. Is it possible? Yes. Advantage: Multiple formats are available for initialization using overloaded constructors. Provides flexibility of using different formats at runtime. Eg. 6_3_DynamicInitializationOfObject.cpp
12
Copy constructor We already did this during constructor overloading.
Use? Initialize objects with values of another existing object. Eg. Integer i2 = i1; Integer i2(i1); Following statement is not copy constructor but, its legal and assignment operator overloading which we will see next chapter. i2 = i1; Copy constructor takes a reference to an obj of same class as itself as an argument. We cannot pass argument by value to copy constructor. When no copy constructor is defined, compiler supplies its own copy constructor. Eg. 6_4_CopyConstructor.cpp Eg. copyConstructorDestructor.cpp
13
Dynamic constructors Constructor can also allocate memory while creating objects. Use? Assign right amount of memory as per the requirement. Thus, optimum utilization of memory. How to do that? Using new operator. Eg. dynamicConstructor.cpp Eg. 6_5_ConstructorsWithNew.cpp
14
Constructing two dimensional array
Eg. 6_6_ConstructingMatrixObjects.cpp
15
Const objects Can we have objects as const? Yes. Y?
To avoid accidental modification of obj values. Eg. const matrix X(m,n); //const obj X We cannot modify m and n. Constant obj can call only const functions. High security implementation.
16
destructors Use? To destroy objects created by constructors.
Preceded by tilde(~) sign. Eg. ~integer(){}; Never takes any argument. Never returns any value. Invoked implicitly by compiler during exit. Used to clean up storage not accessible. It is a part of good programming practice. New – delete, same, constructor – destructor. Objects get destroyed in reverse order of creation. Eg. 6_7_ImplmentationOfDestructors.cpp Eg.6_8_MemoryAllocationToAnObjectUsingDestrucor.c pp
17
END OF CHAPTER 6 THANK YOU
Objects and Classes Part II
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Chapter 12: Adding Functionality to Your Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
Chapter 10 Introduction to Classes
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.
玻璃钢生产厂家句容玻璃钢雕塑景观设计玻璃钢花盆行业浦江人物玻璃钢雕塑定制青岛玻璃钢雕塑公司晋城玻璃钢浮雕镂空雕塑厂家中卫玻璃钢雕塑施工欧式人物玻璃钢雕塑厂家哪家好四川商业商场美陈生产企业海南玻璃钢雕塑公司诚迈玻璃钢仿真钢琴海边景观雕塑山东抽象玻璃钢雕塑批发玻璃钢藕雕塑图片宁夏玻璃钢仿真雕塑朱达诚的玻璃钢李小龙雕塑佛山玻璃钢仿铜人物雕塑栖霞新年商场美陈狗玻璃钢雕塑设计厂家成品玻璃钢花盆厂家直销玻璃钢海鸥雕塑品质玻璃钢卡通雕塑销售宁波玻璃钢牛仔雕塑日照玻璃钢机器人雕塑定做亳州小品系列玻璃钢雕塑价格朝阳区玻璃钢雕塑工程电话公园玻璃钢雕塑设计厂家江西景区玻璃钢雕塑定制嘉兴玻璃钢雕塑厂浙江省商场美陈合肥城市玻璃钢雕塑销售电话商场气球美陈报价模板香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警汪小菲曝离婚始末遭遇山火的松茸之乡雅江山火三名扑火人员牺牲系谣言何赛飞追着代拍打萧美琴窜访捷克 外交部回应卫健委通报少年有偿捐血浆16次猝死手机成瘾是影响睡眠质量重要因素高校汽车撞人致3死16伤 司机系学生315晚会后胖东来又人满为患了小米汽车超级工厂正式揭幕中国拥有亿元资产的家庭达13.3万户周杰伦一审败诉网易男孩8年未见母亲被告知被遗忘许家印被限制高消费饲养员用铁锨驱打大熊猫被辞退男子被猫抓伤后确诊“猫抓病”特朗普无法缴纳4.54亿美元罚金倪萍分享减重40斤方法联合利华开始重组张家界的山上“长”满了韩国人?张立群任西安交通大学校长杨倩无缘巴黎奥运“重生之我在北大当嫡校长”黑马情侣提车了专访95后高颜值猪保姆考生莫言也上北大硕士复试名单了网友洛杉矶偶遇贾玲专家建议不必谈骨泥色变沉迷短剧的人就像掉进了杀猪盘奥巴马现身唐宁街 黑色着装引猜测七年后宇文玥被薅头发捞上岸事业单位女子向同事水杯投不明物质凯特王妃现身!外出购物视频曝光河南驻马店通报西平中学跳楼事件王树国卸任西安交大校长 师生送别恒大被罚41.75亿到底怎么缴男子被流浪猫绊倒 投喂者赔24万房客欠租失踪 房东直发愁西双版纳热带植物园回应蜉蝣大爆发钱人豪晒法院裁定实锤抄袭外国人感慨凌晨的中国很安全胖东来员工每周单休无小长假白宫:哈马斯三号人物被杀测试车高速逃费 小米:已补缴老人退休金被冒领16年 金额超20万