Saturday, June 17, 2023

Constructor & Destructor in CPP

 

CHAPTER 8

CONSTRUCTORS AND  DESTRUCTORS

CONSTRUCTOR

          it is a special member function of a class . it initializes the object of its class

·       constructors are also member function of a class

·       they have the names same as class names

·       they are called automatically whenever the object is a created

·       they should be declared in public area

·       they have no return type

TYPES OF CONSTRUCTOR

·       default constructor

·       parameterized constructor

·       copy constructor

·       dynamic constructor

·       constructor over loading

DEFAULT CONSTRUCTOR

SYNTAX

class class _ name {

              private:

                    data members

CONSTRUCTORS AND DESTRUCTORS

public:

     class name() {} //default constructor

};

example

class  student{

private:

      int rlno;

float mark;

public:

student(){}     /*default constructor*/

void get()

{

cout<<"get rlno and mark";

cin>>rlno>>mark;

}

void put()

{

cout<<"rlno="<<rlno;

cout<<"mark="<<mark;

}

};

void main()

{

student s;     /*calling default constructor*/

}

output

_

Parameterized constructor

#include<iostream.h>

#include<conio.h>

class student{

   private:

             int rlno;

             float mark;

public:

       student(){}  /* default constructor*/

       student(int r,float m) /*parameterized constructor*/

 {

       rlno=r;

       mark=m;

  }

  void put()

 {

cout<<"rlno="<<rlno<<endl;

cout<<"mark="<<mark;

}

};

void main()

{

student s;

student k(120,78);

clrscr();

k.put();

getch();

}

output

rlno=120

mark=78

CONSTRUCTOR WITH DEFAULT ARGUMENTS

example

#include<iostream.h>

#include<conio.h>

class student{

 private:

       int rlno;

       float mark;

 public:

       student(){}

       student(int r,float m=78);

       void put()

     {

       cout<<"rlno="<<rlno<<endl;

       cout<<"mark="<<mark<<endl;

   }

};

student::student(int r,float m)

        {

 

          rlno=r;

               mark=m;

             }

void main()

{

student s;

student k(120,78);

clrscr();

k.put();

student t(111);

t.put();

getch();

}

output

rlno=120

 mark=78

rlno=111

 mark=78

 

COPY CONSTRUCTOR

example

#include<iostream.h>

#include<conio.h>

class student{

 private:

      int rlno;

      float mark;

 public:

       student(){}

       student(int r,float m)

      {

         rlno=r;

         mark=m;

         }

 student(student &t)

  {

      rlno=t.rlno;

      mark=t.mark;

   }

   void put()

   {

    cout<<"rlno="<<rlno<<endl;

    cout<<"mark="<<mark<<endl;

   }

};

void main()

{

student s;

student k(120,78);

student R(k);

clrscr();

R.put();

getch();

}

output

rlno=120

mark=78

CONSTRUCTOR OVERLOADING

example

#include<iostream.h>

#include<conio.h>

class student{

 private:

        int rlno;

        float mark;

 public:

        student(){}

        student(int r,float m)

       {

        rlno=r;

        mark=m;

        }

        void put()

        {

         cout<<"Rlno="<<rlno<<endl;

         cout<<"Mark="<<mark<<endl;

         }

};

void main()

{

student s;

student k(120,78);

clrscr();

k.put();

getch();

}

output

Rlno=120

Mark=78

 

DESTRUCTOR

example

#include<iostream.h>

#include<conio.h>

class student{

 private:

    int rlno;

    float mark;

 public

    student(){}

    student(int r,float m)

   {

     rlno=r;

     mark=m;

   }

   void put()

    {

      cout<<"Rlno="<<rlno<<endl;

      cout<<"Mark="<<mark;

     }

   ~student()

     {

      cout<<"destructor called";

   }

};

void main()

{

student k(120,78);

clrscr();

k.put();

getch();

}

output

Rlno=120

Mark=78

 

0 comments:

Post a Comment