CPP05 – Write a CPP program to create Student class with appropriate constructor and destructor

By | October 29, 2018

Write a CPP program to create Student class with appropriate constructor and destructor. Also show what happen when you try to attempt to access private data members from outside of the class.

CODE:-

#include<iostream.h>
#include<string.h>
#include<conio.h>
class student
{
int roll;
char name[25];
char addr[100];
int std;
char div;
long cont;
public:
void get_data(int,char*,char*,int,char,long);
void display(void);
student();
~student();
};
student::student(void)
{
cout<<“object is created “<<endl;
}
student::~student(void)
{
cout<<“object is destroyed!!!”<<endl;
}
void student::get_data(int rol,char *nm,char *ad,int st,char dv,long ct)
{
roll=rol;
strcpy(name,nm);
strcpy(addr,ad);
std=st;
div=dv;
cont=ct;
}
void student::display(void)
{
cout<<endl;
cout<<“Roll : “<<roll<<endl;
cout<<“Name : “<<name<<endl;
cout<<“Address : “<<addr<<endl;
cout<<“Std : “<<std<<endl;
cout<<“Div : “<<div<<endl;
cout<<“Contact : “<<cont<<endl;
cout<<endl;
}

void main()
{
clrscr();
student stud;
stud.get_data(5,”Gangadhar”,”Suman_Apt,Malad_West”,9,’A’,28845562);
stud.display();
//cout<<“Roll No. = “<<stud.roll<<endl; //accessing with object
//cout<<“Roll No. = “<<student::roll<<endl; //accessing with class name
getch();
}

#include<iostream.h>
#include<string.h>
#include<conio.h>
class student
{
int roll;
char name[25];
char addr[100];
int std;
char div;
long cont;
public:
void get_data(int,char*,char*,int,char,long);
void display(void);
student();
~student();
};
student::student(void)
{
cout<<"object is created "<<endl;
}
student::~student(void)
{
cout<<"object is destroyed!!!"<<endl;
}
void student::get_data(int rol,char *nm,char *ad,int st,char dv,long ct)
{
roll=rol;
strcpy(name,nm);
strcpy(addr,ad);
std=st;
div=dv;
cont=ct;
}
void student::display(void)
{
cout<<endl;
cout<<"Roll : "<<roll<<endl;
cout<<"Name : "<<name<<endl;
cout<<"Address : "<<addr<<endl;
cout<<"Std : "<<std<<endl;
cout<<"Div : "<<div<<endl;
cout<<"Contact : "<<cont<<endl;
cout<<endl;
}
void main()
{
clrscr();
student stud;
stud.get_data(5,"Gangadhar","Suman_Apt,Malad_West",9,'A',28845562);
stud.display();
//cout<<"Roll No. = "<<stud.roll<<endl; //accessing with object
//cout<<"Roll No. = "<<student::roll<<endl; //accessing with class name
getch();
}

SCREEN SHOTS:-

OUTPUT:-

Attempt Access:-