UrbanPro
true

Take Class 12 Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

C++ Constructor and Destructor

S
Sujatha K.
12/06/2018 0 0

Constructor and Destructor

What is the need of a constructor and it can be explained with an example:

//   Turbo C compiler

#inlcude

#include

#include

class Stud

{

int Rno;

char Name[10];

public:

void Input()

{

cout<<"Enter Roll number:";

cin>>Rno;

cout<<"Enter name :";

gets(name);

 }

void Output()

{

cout<<"\nEntered Roll number:"<<Rno;

cout<<"\nEntered name :";

puts(name);

 }

};

void main()

{

Stud S;

S.Output();

}

/* When Program is compiled the output will be a garbage value and now let us see how constructor is useful in this situation.  The above will now include a constructor and destructor

*/

#inlcude

#include

#include

#include

class Stud

{

int Rno;

char Name[10];

public:

Stud() // constructor

{

Rno=5;

strcpy(Name,"Rahul");

}

void Input()

{

cout<<"Enter Roll number:";

cin>>Rno;

cout<<"Enter name :";

gets(name);

 }

void Output()

{

cout<<"\nEntered Roll number:"<<Rno;

cout<<"\nEntered name :";

puts(name);

 }

~Stud() //destructor

{cout<<"Object destructed";}

};

void main()

{  //line 1

Stud S; //line 2

S.Output();

}//last line

/* Now When program is compiled  at line 2 constructor is automaticaly called and initializes the value for its datamember we will get the output as 

Entered Roll number: 5

Entered name : Rahul

and when the program ends i.e last line  destructor will be called automatically */

/*If any doubt regarding the above code can be cleared by asking questions and in the next session I will explain all types of constructor*/

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You



How to Solve Numericals in Physics?
Are you scared of numerical? Do you think you don't understand them? If yes, then read ahead. See, it might be any exam, but numerical won't leave your back. So let's talk about common problems and their...

How To Prepare For CBSE Class 12 Board Exam ?
How to prepare for the CBSE Class 12 Board Exam? In this lesson, I will teach you how to prepare for the CBSE Class 12 Board Exam. Your class 12 board exam is probably the most important exam in your...
A

Abraham Paul

3 0
0

Directions Cosines and Directions Ratios (3D GEOMETRY)
Direction Cosines are the Cosines Of the Angles of the angles between and the line and the coordinate Axis. If we have a vector (a,b,c) in space then two a=x2-x1,b=y2-y1 and c =z2-z1 are called the direction...
X

Looking for Class 12 Tuition Classes?

The best tutors for Class 12 Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take Class 12 Tuition with the Best Tutors

The best Tutors for Class 12 Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more