Weitere ähnliche Inhalte
Ähnlich wie Aae oop xp_07
Ähnlich wie Aae oop xp_07 (20)
Kürzlich hochgeladen (20)
Aae oop xp_07
- 1. Constructors and Destructors
Objectives
In this lesson, you will learn to:
Identify the need for constructors
Declare constructors
Identify the need for destructors
Declare destructors
Use scope resolution operator
Use constructors with parameters
Invoke member functions by using:
The call by value method
©NIIT OOPS/Lesson 7/Slide 1 of 25
- 3. Constructors and Destructors
Constructors
Are used to initialize the member variables of the
class when the objects of the class are created
Must have the same name as that of class name
Cannot return any value, not even a void type
Class can have more than one constructors defined in
it (known as overloaded constructors)
Default constructors accept no parameters and are
automatically invoked by the compiler
©NIIT OOPS/Lesson 7/Slide 3 of 25
- 5. Constructors and Destructors
Declaration of Constructors
Example:
class Calculator
{
private:
int number1, number2, tot;
public:
...
Calculator()
{
number1 = number2 = tot = 0;
cout Constructor invoked endl;
}
};
©NIIT OOPS/Lesson 7/Slide 5 of 25
- 6. Constructors and Destructors
Destructors
Are used to de-initialize the objects when they are
destroyed
Are used to clear memory space occupied by a data
member when an object goes out of scope
Must have the same name as that of the class,
preceded by a ~ (For example: ~Calculator())
Are automatically invoked
Can also be explicitly invoked when required
Cannot be overloaded
©NIIT OOPS/Lesson 7/Slide 6 of 25
- 7. Constructors and Destructors
Need for Destructors
To de-initialize the objects when they are destroyed
To clear memory space occupied by a data member
when an object goes out of scope
©NIIT OOPS/Lesson 7/Slide 7 of 25
- 8. Constructors and Destructors
Declaration of Destructors
Example:
/* This Code Shows The Use Of Destructor
In The Calculator Class */
class Calculator
{
private:
int number1, number2, tot;
public:
...
~Calculator() //Body Of The
//Destructor
{
number1 = number2 = tot = 0;
}
};
©NIIT OOPS/Lesson 7/Slide 8 of 25
- 9. Constructors and Destructors
Just a Minute…
Given is a code snippet for the main() function of a
program. How would you ensure that the following tasks
are accomplished when the program is executed:
1. The member variables are initialized with zero
2. On exit or termination of the program the following
message is displayed : “Bye Folks!!! Have a Nice
Time”
#include iostream
int main()
{
Number num1;num1.disp();
}
Hint: The Number class has only one member
variable
©NIIT
myNum. OOPS/Lesson 7/Slide 9 of 25
- 10. Constructors and Destructors
Scope Resolution Operator (::)
Is used to define member functions outside the class
definition therefore making the class definition more
readable
Example:
class calculator
{
public:
void input();
};
void calculator::input ()
{…}
©NIIT OOPS/Lesson 7/Slide 10 of 25
- 11. Constructors and Destructors
Constructors with Parameters
Allow member variables of the class to be initialized
with user supplied values from main() function also
called parameters
Example
class calculate
{
private:
int num1,num2,total;
public:
calculate(int, int);
};
©NIIT OOPS/Lesson 7/Slide 11 of 25
- 12. Constructors and Destructors
Constructors with Parameters (Contd.)
calculate::calculate(int x, int y)
{
num1=x;num2=y;
total=0;
}
int main()
{//Accept values in two variable var1
//var2
calculate c1(var1,var2);
}
©NIIT OOPS/Lesson 7/Slide 12 of 25
- 14. Constructors and Destructors
Call by Value
Is useful when the function does not need to modify
the values of the original variables
Does not affect the values of the variables in caller
functions
©NIIT OOPS/Lesson 7/Slide 14 of 25
- 15. Constructors and Destructors
Problem Statement 5.P.1
Predict the Output:
#include iostream
void square(int);
class functionCall
{
int number;
public:
functionCall();
};
functionCall::functionCall()
{
number=10;square(number);
}
©NIIT OOPS/Lesson 7/Slide 15 of 25
- 16. Constructors and Destructors
Problem Statement 5.P.1 (Contd.)
void square(int num)
{
coutnumendl;
num *= num; //This Expression Is
//Resolved As num = num * num
cout num endl;
}
int main()
{
functionCall f1;
return 0;
}
©NIIT OOPS/Lesson 7/Slide 16 of 25
- 18. Constructors and Destructors
Call by Reference (Contd.)
Using an alias
The same variable can be referenced by more than
one name by using the or the alias operator
The change in the value of the variable by the
called or the calling program is reflected in all the
affected functions
©NIIT OOPS/Lesson 7/Slide 18 of 25
- 19. Constructors and Destructors
Problem Statement 5.P.2
Predict the Output:
#include iostream
void square(int );
class functionCall
{
int number;
public:
functionCall();
};
functionCall::functionCall()
{
number=10;square(number);
}
©NIIT OOPS/Lesson 7/Slide 19 of 25
- 20. Constructors and Destructors
Problem Statement 5.P.2 (Contd.)
void square(int num)
{
coutnumendl;
num *= num; //This Expression Is
//Resolved As num = num * num
cout num endl;
}
int main()
{
functionCall f1;
return 0;
}
©NIIT OOPS/Lesson 7/Slide 20 of 25
- 21. Constructors and Destructors
Call by Reference using Pointers
To define and declare a pointer variable:
Using pointers
Involves a pointer variable storing the memory
address of any variable
Is advantageous since it allows direct access to
individual bytes in the memory and output devices
Example:
char var = 'G';
char *ptr ; //Pointer Declaration
ptr = var; //Stores the address of
//the variable
©NIIT OOPS/Lesson 7/Slide 21 of 25
- 22. Constructors and Destructors
Call by Reference using Pointers (Contd.)
Using pointers
Allows dynamic allocation and release of memory
that is program can obtain memory while it is
running by using new and release by using delete
operator
Syntax:
variable = new type;
The type of variable mentioned on the left hand and
the type mentioned on the right side of the new
operator should match
delete variable
©NIIT OOPS/Lesson 7/Slide 22 of 25
- 23. Constructors and Destructors
Summary
In this lesson, you learned that:
Constructors are member functions of any class and
are invoked the moment an instance of the class to
which they belong is created
A constructor function has the same name as its class
A destructor function is invoked when any instance of
a class ceases to exist
A destructor function has the same name as its class
but prefixed with a ~ (tilde)
The member functions and the constructors of a class
can also be defined outside the boundary of the class,
using the scope resolution operator (::)
©NIIT OOPS/Lesson 7/Slide 23 of 25
- 24. Constructors and Destructors
Summary (Contd.)
The data that the function must receive when called
from another function is/are called the parameters of a
function
In C++ programs, functions that have parameters are
invoked in one of the following ways:
A call by value
A call by reference
You can give two names to a variable by using the
alias operator
A reference provides an alias, or an alternate name
for the variable
©NIIT OOPS/Lesson 7/Slide 24 of 25
- 25. Constructors and Destructors
Summary (Contd.)
A pointer is a variable that stores the memory address
of another variable
Dynamic allocation is the means by which a program
can obtain memory while it is running
The capability of obtaining memory as the need arises
is provided by the new operator
The delete operator is used to release memory
©NIIT OOPS/Lesson 7/Slide 25 of 25