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

Lesson 2 - Les Nombres
0 Zéro 1 un 11 onze 21 vingt-et-un 31 trente-et-un 41 quarante-et-un 2 deux 12 douze 22 vingt-deux 32 trente-deux 42 quarante-deux 3 trois 13 treize 23...

MATHEMATICS of CBSE BOARD
Certainly! The class 12 mathematics curriculum typically includes topics like calculus, algebra, geometry, and statistics. Here's a basic structural plan: 1. **Calculus:** - Differential Calculus ...

Exam Preparation
As we know, exam time is most difficult for the student whether they study correctly or not. This time is also required mental preparation which comes through more than ten times revision and then memories...

How To Avoid Sleep While Studying?
I will give you the top 7 methods to avoid sleep while you are studying: 1) Be Interested in what you are Studying! Yes to prevent sleep you must make sure that you are Interested in what you are reading! So...
A

Abraham P.

1 0
0

Smart work
To score good in your board examinations , there is always need of hard work but along with this students also require smart work, this helps students in laerning and understanding capability in a short...
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