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

12th class
Revision is the most important thing in your preparations so never go short of it. I am a Physics faculty so telling you about few points 1 special focus on derivations 2 remember every important formula...

Steps of Execution of a Python Program
When we compile a Python program, the python compiler converts the Python source code into another code called byte code. Byte code is a fixed set of instructions that represent different types of operations....

Nothing changes overnight except our perception
Each student has different capabilities and method of how to deal with them also varies. It's not the subject that usually bothers, but it's the way how we deal with them that makes them comfortable or...

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

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