Computer Notes - Constructor

0

No comments posted yet

Comments

Slide 23

Never return a handle to a data member You are never sure that function accessing the reference will not change the value of the variable

Slide 24

Never return a handle to a data member You are never sure that function accessing the reference will not change the value of the variable

Slide 25

Never return a handle to a data member You are never sure that function accessing the reference will not change the value of the variable

Slide 1

Constructor http://ecomputernotes.com

Slide 2

Review Member functions implementation Constructors Constructors overloading Copy constructors http://ecomputernotes.com

Slide 3

Copy Constructor Copy constructor are used when: Initializing an object at the time of creation When an object is passed by value to a function http://ecomputernotes.com

Slide 4

Example void func1(Student student){ … } int main(){ Student studentA(“Ahmad”); Student studentB = studentA; func1(studentA); } http://ecomputernotes.com

Slide 5

Copy Constructor (contd.) Student::Student( const Student &obj){ rollNo = obj.rollNo; name = obj.name; GPA = obj.GPA; } http://ecomputernotes.com

Slide 6

Shallow Copy When we initialize one object with another then the compiler copies state of one object to the other This kind of copying is called shallow copying http://ecomputernotes.com

Slide 7

Example Student studentA(“Ahmad”); Student studentB = studentA; http://ecomputernotes.com

Slide 8

Example int main(){ Student studentA(“Ahmad”,1); { Student studentB = studentA; http://ecomputernotes.com

Slide 9

Example int main(){ Student studentA(“Ahmad”,1); { Student studentB = studentA; } } http://ecomputernotes.com

Slide 10

Copy Constructor (contd.) Student::Student( const Student & obj){ int len = strlen(obj.name); name = new char[len+1] strcpy(name, obj.name); … /*copy rest of the data members*/ } http://ecomputernotes.com

Slide 11

Example int main(){ Student studentA(“Ahmad”,1); { Student studentB = studentA;

Slide 12

Example int main(){ Student studentA(“Ahmad”,1); { Student studentB = studentA; } } http://ecomputernotes.com

Slide 13

Copy Constructor (contd.) Copy constructor is normally used to perform deep copy If we do not make a copy constructor then the compiler performs shallow copy http://ecomputernotes.com

Slide 14

Destructor Destructor is used to free memory that is allocated through dynamic allocation Destructor is used to perform house keeping operations http://ecomputernotes.com

Slide 15

Destructor (contd.) Destructor is a function with the same name as that of class, but preceded with a tilde ‘~’ http://ecomputernotes.com

Slide 16

Example class Student { … public: ~Student(){ if(name){ delete []name; } } } http://ecomputernotes.com

Slide 17

Overloading Destructors cannot be overloaded http://ecomputernotes.com

Slide 18

Sequence of Calls Constructors and destructors are called automatically Constructors are called in the sequence in which object is declared Destructors are called in reverse order http://ecomputernotes.com

Slide 19

Example Student::Student(char * aName){ … cout << aName << “Cons\n”; } Student::~Student(){ cout << name << “Dest\n”; } }; http://ecomputernotes.com

Slide 20

Example int main() { Student studentB(“Ali”); Student studentA(“Ahmad”); return 0; } http://ecomputernotes.com

Slide 21

Example Output: Ali Cons Ahmad Cons Ahmad Dest Ali Dest http://ecomputernotes.com

Slide 22

Accessor Functions Usually the data member are defined in private part of a class – information hiding Accessor functions are functions that are used to access these private data members Accessor functions also useful in reducing error

Slide 23

Example – Accessing Data Member class Student{ … int rollNo; public: void setRollNo(int aRollNo){ rollNo = aRollNo; } }; http://ecomputernotes.com

Slide 24

Example – Avoiding Error void Student::setRollNo(int aRollNo){ if(aRollNo < 0){ rollNo = 0; } else { rollNo = aRollNo; } } http://ecomputernotes.com

Slide 25

Example - Getter class Student{ … int rollNo; public: int getRollNo(){ return rollNo; } }; http://ecomputernotes.com

Slide 26

this Pointer class Student{ int rollNo; char *name; float GPA; public: int getRollNo(); void setRollNo(int aRollNo); … }; http://ecomputernotes.com

Slide 27

this Pointer The compiler reserves space for the functions defined in the class Space for data is not allocated (since no object is yet created) Function Space getRollNo(), … http://ecomputernotes.com

Slide 28

this Pointer Student s1, s2, s3;

Slide 29

this Pointer Function space is common for every variable Whenever a new object is created: Memory is reserved for variables only Previously defined functions are used over and over again http://ecomputernotes.com

Slide 30

this Pointer Memory layout for objects created: How does the functions know on which object to act?

Slide 31

this Pointer Address of each object is passed to the calling function This address is deferenced by the functions and hence they act on correct objects The variable containing the “self-address” is called this pointer

Slide 32

Passing this Pointer Whenever a function is called the this pointer is passed as a parameter to that function Function with n parameters is actually called with n+1 parameters http://ecomputernotes.com

Slide 33

Example void Student::setName(char *) is internally represented as void Student::setName(char *, const Student *) http://ecomputernotes.com

Slide 34

Declaration of this DataType * const this; http://ecomputernotes.com

Slide 35

Compiler Generated Code Student::Student(){ rollNo = 0; } Student::Student(){ this->rollNo = 0; } http://ecomputernotes.com

Summary: http://ecomputernotes.com - Computer Notes - Constructor in Object oriented Programming what is Constructor Explain about it in detail .explain it with example

Tags: computer notes - constructor

URL:
More by this User
Most Viewed