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


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

Kingdom protista
The Kingdom Protista consists of eukaryotic protists. Members of this very diverse kingdom are typically unicelluar and less complex in structure than other eukaryotes. In a superficial sense, these organisms...

Logarithm with JEE Example
JEE 2011 Question on Logarithm Let (x0,y0) be the solution of the following equations: (2x)ln2 = (3y)ln3 3lnx = 2lnyThen x0 is(A)1 6(B)1 3(C)1 2(D) 6Answer and Comments: (C). We can treat the data as...

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