Chapter 9- Constructors and Destructors II PUC, MDRPUC, Hassan 1| Page Chapter-9 CONSTRUCTORS AND DESTRUCTORS Introduction: It is sometimes convenient if an object can initialize itself when it is first created, without the need to make a separate call to member functions. Automatic initialization is carried out using special member functions called constructors. Constructors: A Constructor is a special member function that is called automatically when an object is created. The purpose of a constructor is to mainly initialize the member variables of a class. The general syntax of a the constructor in C++ is: In the above example class declaration, class Sum has a member function Sum( ) with the same name of the class and which provides initial value to its data member s. Characteristics of Constructor: The name of the constructor is the same as the name of the class. Example: In the above class, name of the class is Sum and the function name is Sum. A Constructor, even though it is a function, has no return type, i.e. it is neither a value-returning function nor a void function. Example: Sum ( ) function has no return type and not even void. The constructor should be declared in the public section. Constructors are executed automatically i.e. they are never invoked. They are executed when a class object is created. Important 5 Marks
13
Embed
Chapter-9 CONSTRUCTORS AND DESTRUCTORS When object is passed to a function using pass by value, copy constructor is automatically ... Program to find factorial of a number using copy
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Chapter 9- Constructors and Destructors II PUC, MDRPUC, Hassan
1 | P a g e
Chapter-9
CONSTRUCTORS AND DESTRUCTORS
Introduction:
It is sometimes convenient if an object can initialize itself when it is first created, without the need
to make a separate call to member functions.
Automatic initialization is carried out using special member functions called constructors.
Constructors:
A Constructor is a special member function that is called automatically when an object is
created.
The purpose of a constructor is to mainly initialize the member variables of a class.
The general syntax of a the constructor in C++ is:
In the above example class declaration, class Sum has a member function Sum( ) with the same
name of the class and which provides initial value to its data member s.
Characteristics of Constructor:
The name of the constructor is the same as the name of the class.
Example: In the above class, name of the class is Sum and the function name is Sum.
A Constructor, even though it is a function, has no return type, i.e. it is neither a value-returning
function nor a void function.
Example: Sum ( ) function has no return type and not even void.
The constructor should be declared in the public section.
Constructors are executed automatically i.e. they are never invoked. They are executed when a
class object is created.
Important5 Marks
Chapter 9- Constructors and Destructors II PUC, MDRPUC, Hassan
2 | P a g e
A class can have more than one constructor. However all constructor of a class should have the
same name.
It is not possible to refer to the address of the constructors.
The constructors make implicit calls to the operator new and delete when memory allocation is
required.
Example: Program to demonstrate how constructor is automatically executed at the time of
Chapter 9- Constructors and Destructors II PUC, MDRPUC, Hassan
12 | P a g e
}void main( ){
num a;a.display( );
}
CHAPTER 9 – Constructors and Destructors BLUE PRINT
VSA (1 marks) SA (2 marks) LA (3 Marks) Essay (5 Marks) Total
- 01 Question - 01 Question 02 Question
- Question No 14 - Question No 33 07 Marks
Important Questions
5 Marks Question:
1. What is Constructor? Give the rules for writing a constructor function.
2. What is default constructor? Write a program to illustrate it.
3. Explain parameterized constructor with syntax and example.
4. Mention the different methods by which constructor are invoked. Explain anyone
with an illustrative example.
5. Explain the features of copy constructor.
6. Explain destructor with syntax and example.
7. Write a C++ program to find the sum of the series 1+X+X2+….+Xn using
constructor:
#include<iostream.h>#include<conio.h>class copy{
private:int x, n;
public:int compute;copy(int xx, int nn){
x = xx;n = nn;
}};
OUTPUT:
In Constructor:
Value of X = 100
In Destructor
Chapter 9- Constructors and Destructors II PUC, MDRPUC, Hassan
13 | P a g e
int copy : : compute( ){
int nextterm;int sum =1;nextterm = x;
for (int i=1; i<=n; i++){
sum = sum + nextterm;nextterm = nextterm * x;
}return sum;
}void main( ){
int n, x ;clrscr( );cout<<”Enter the X and N values”<<endl;cin>>x >> n;copy obj ( x, n);copy cpy = obj;cout<<”Object 1: Sum of the series:” <<obj.compute( )<<endl;cout<<”Object 2: Sum of the series:” <<cpy.compute( )<<endl;getch();