Get Email Subscription

Enter your email address:

Delivered by FeedBurner

Wednesday, April 21, 2010

This program demonstrates the use of constructor and destructor. (C++)


This program demonstrates the use of constructor and destructor
 
This is a very common interview question so please understand this concept very well.
 
 
#include< iostream.h>
#include< conio.h>
#include< stdio.h>
class student
{
private:
char *name;
char *sub;
int marks;
public:
student()//constructor( same name as class)
{
cout< < "Inside the constructor"< < endl;
// allocate the space in the memory.
name=new char[20];
sub=new char[20];
}
void input();
void output();
~student()//destructor(same name with tilde)
{
cout< < endl< < "Inside the distructor"< < endl;
//frees the allocated space
delete name;
delete sub;
}
};
main()
{
clrscr();
student st;
st.input();
st.output();
}
void student::input()
{
cout< < "Enter the name:: ";
gets(name);
cout< < endl< < "Enter the Subject:: ";
gets(sub);
cout< < endl< < "Enter the marks:: ";
cin>>marks;
}
void student::output()
{
cout< < "Name:: "< < name< < endl;
cout< < "Subject:: "< < sub< < endl;
cout< < "Marks:: "< < marks;
}

Related Posts by Categories



0 comments:

About This Blog

Lorem Ipsum

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP