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...

Margin of Safety?
What is the Margin of Safety? Suppose a class 11th student need 33 marks to pass a paper. If a student gets exactly 33 marks, then he will be a pass. These 33 marks are called Break-Even-Point Marks. ...

What are Reserves & Provisions? Lets understand in detail-
Provisions:There are some expenses and losses which are specific to incur, but their amount cannot be known with certainty as they are not yet incurred. It is necessary to make provision for such certain...
A

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