0% found this document useful (0 votes)
54 views25 pages

Online Class 4-Constructors and Destructors

Uploaded by

Soutik Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views25 pages

Online Class 4-Constructors and Destructors

Uploaded by

Soutik Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

CONSTRUCTORS ,DE

STRUCTORS
CONSTRUCTOR
 … is a special member function whose task is
to initialize the objects of its class .
 By the way ,Why is initialization necessary ?

 A constructor should have the same name as


the class .

class integer
{
int m,n;
public:
integer () ; // constructor
…..
…..
};
integer:: integer() // constructor defined
{
m=0;
n=0;
}
WHEN AN OBJECT IS CREATED
e.g. Integer in1 ;
Here the object is created and also initialized
No function calling is required .
Any other member function would have to be
‘invoked’
{If some calculation functions are called
before the setvalue function is called ..then
unexpected values will be the result)
DIFFERENCE BETWEEN CONSTRUCTOR AND OTHER
MEMBER FUNCTIONS:
 1. The Constructor has the same name as the
class name.

2. The Constructor is called when an object of
the class is created.

3. A Constructor does not have a return type.

4. When a constructor is not specified, the
compiler generates a default constructor
which does nothing.
 . There are 3 types of constructors:
 Default Constructor
 Parameterized Constructor
 Copy constructor
CONSTRUCTORS
 ….can have arguments
 They are then called parameterized
constructors
class ABC
{
int i;
float j;
public :
ABC (int a ,float b)
{
i=a; j=b;
}
Creating objects
ABC ob1( 3,5.89) ; //implicit call
or
ABC ob1=ABC ( 3,5.89); //explicit call
DEFAULT CONSTRUCTOR
 A default constructor is a constructor which
can be called with no arguments (either
defined with an empty parameter list, or
with default arguments provided for every
parameter).
 Syntax

ClassName ( ) ;
ClassName :: ClassName ( ) body
 If no default constructor is defined, then the
compiler supplies a default constructor.

 What does such a constructor do ?


?????????????
WHY CAN’T WE INITIALISE
DATA MEMBERS IN THE MAIN ..
Int main()
Student_Perf ={0,0,0} ;

??
 It may be illegal if the data members are
private (cannot be used directly in main)

 The solution then is to have an initialisation


function as a member function.

 Anything wrong with this idea??


 The programmer has to call this function
‘explicitly’

 The best solution is for the compiler to do


it ,but the programmer should have a ‘role’
in it .
 The constructor is called automatically
whenever an object is created .
THE CRECTANGLE CLASS WITH A
CONSTRUCTOR
QUESTION
 Define a class ‘Student_perf’ with data
members cgpa and marks1 and marks2.
 Make a constructor .

i) Define it ‘inline’
ii) Define it outside the class definition
THIS IS FOR A CIRCLE
 class Circle
 {
 private:
 double radius;
 public:
 Circle() { radius = 0.0;}
 Circle(int r);
 void setRadius(double r){radius = r;}
 double getDiameter(){ return radius
*2;}
 double getArea();
 double getCircumference();
QUESTION
 Create a class for Cylinder .
 The data members are radius ,and height
 There should be member functions for
finding the area and surface area
 Use constructors.
POINTS REGARDING
CONSTRUCTORS
 No return type
 Obeys access rules
COPY CONSTRUCTOR
 Find out and study –what it is ,where and
why it is used etc etc…
DESTRUCTORS
DESTRUCTOR
 Has the format
 ~class name ()
 Destructors and constructors invoke ‘new ‘
and delete.
#include <iostream>
using namespace std;
class Line
{
private:
double length;
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor
declaration
};

You might also like