6.Problem statement
Develop an object oriented program in C++ to create a database of student information system containing the following information: Name, Roll number, Class, division, Date of Birth, Blood group, Contact address, telephone number, driving license no. etc Construct the database with suitable member functions for initializing and destroying the data viz constructor, default constructor, Copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete.
Program
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
class personal;
static int count;
class person
{
char *name;
int *rollno,*classno;
char *bloodgroup,*div;
public:person()
{
name=new char[20];
rollno=new int;
classno=new int;
bloodgroup = new char[3];
div=new char;
}
~person()
{
cout<<"Destructor called ";
}
friend class personal;
};
class personal
{
char *address;
long int *telephone;
char *dob;
public:personal()
{
address=new char[50];
telephone = new long int;
dob= new char[10];
}
~personal()
{
cout<<"Destructor called ";
}
void getdata(person &);
void putdata(person &);
int search (person *obj[]);
};
void personal::getdata(person &obj)
{
cout<<"--------ENTER INFORMATION----------"<<endl;
cin.ignore();
cout<<"Enter name :";
cin.getline(obj.name,20);
cout<<"Enter roll no :";
cin>>*obj.rollno;
cout<<"Enter class :";
cin>>*obj.classno;
cout<<"Enter div :";
cin>>*obj.div;
cin.ignore();
cout<<"Enter blood group :";
cin.getline(obj.bloodgroup,3);
cout<<"Enter address :";
cin.getline(this->address,50);
cout<<"Enter telephone no :";
cin>>*this->telephone;
cin.ignore();
cout<<"Enter date of birth :";
cin.getline(this->dob,10);
}
void personal::putdata(person &obj)
{
cout<<"---------STUDENT INFORMAION-----------"<<endl;
cout<<"Name is :"<<(char *)obj.name<<endl;
cout<<"Roll no is :"<<*obj.rollno<<endl;
cout<<"Class is :"<<*obj.classno<<endl;
cout<<"Division is :"<<*obj.div<<endl;
cout<<"Blood group is :"<<(char *)obj.bloodgroup<<endl;
cout<<"Date of birth is :"<<(char *)this->dob<<endl;
cout<<"Address is :"<<(char *)this->address<<endl;
cout<<"Telephone number is :"<<*this->telephone<<endl;
}
int personal::search(person *obj[])
{
char temp[30];
cout<<"Enter name :";
cin.ignore();
cin.getline(temp,30);
for(int i=0;i<count;i++)
{
if(strcmp(temp,(char *)obj[i]->name)==0)
{
return i;
}
}
return -1;
}
int main()
{
personal *obj1[10];
person *obj2[10];
count = 0;
char c='y';
int choice,p;
while(c=='y')
{
cout<<"Enter choice \n[1]Add data\n[2]Display data\n[3]Search\n[4]Delete\n";
cin>>choice;
switch(choice)
{
case 1:obj1[count]=new personal;
obj2[count]=new person;
obj1[count]->getdata(*obj2[count]);
count++;
break;
case 2:for(int i=0;i<count;i++)
{
obj1[i]->putdata(*obj2[i]);
cout<<endl;
}
break;
case 3:p=obj1[0]->search(obj2);
if(p==-1)
{
cout<<"not found"<<endl;
}
else
{
cout<<"found"<<endl;
obj1[p]->putdata(*obj2[p]);
}
break;
case 4:p=obj1[0]->search(obj2);
if(p==-1)
{
cout<<"not found"<<endl;
}
else
{
cout<<"Found "<<endl;
//obj1[p]->putdata(*obj2[p]);
cout<<"Preparing to delete"<<endl;
*obj1[p]=*obj1[count-1];
*obj2[p]=*obj2[count-1];
obj1[p]->putdata(*obj2[p]);
// cout<<endl<<obj1[count-1]<<endl;
delete obj1[count-1];
delete obj2[count-1];
count--;
}
break;
default:cout<<"Invalid choice ";
break;
}
cout<<"Do you want to continue :";
cin>>c;
}
return 0;
}
Develop an object oriented program in C++ to create a database of student information system containing the following information: Name, Roll number, Class, division, Date of Birth, Blood group, Contact address, telephone number, driving license no. etc Construct the database with suitable member functions for initializing and destroying the data viz constructor, default constructor, Copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete.
Program
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
class personal;
static int count;
class person
{
char *name;
int *rollno,*classno;
char *bloodgroup,*div;
public:person()
{
name=new char[20];
rollno=new int;
classno=new int;
bloodgroup = new char[3];
div=new char;
}
~person()
{
cout<<"Destructor called ";
}
friend class personal;
};
class personal
{
char *address;
long int *telephone;
char *dob;
public:personal()
{
address=new char[50];
telephone = new long int;
dob= new char[10];
}
~personal()
{
cout<<"Destructor called ";
}
void getdata(person &);
void putdata(person &);
int search (person *obj[]);
};
void personal::getdata(person &obj)
{
cout<<"--------ENTER INFORMATION----------"<<endl;
cin.ignore();
cout<<"Enter name :";
cin.getline(obj.name,20);
cout<<"Enter roll no :";
cin>>*obj.rollno;
cout<<"Enter class :";
cin>>*obj.classno;
cout<<"Enter div :";
cin>>*obj.div;
cin.ignore();
cout<<"Enter blood group :";
cin.getline(obj.bloodgroup,3);
cout<<"Enter address :";
cin.getline(this->address,50);
cout<<"Enter telephone no :";
cin>>*this->telephone;
cin.ignore();
cout<<"Enter date of birth :";
cin.getline(this->dob,10);
}
void personal::putdata(person &obj)
{
cout<<"---------STUDENT INFORMAION-----------"<<endl;
cout<<"Name is :"<<(char *)obj.name<<endl;
cout<<"Roll no is :"<<*obj.rollno<<endl;
cout<<"Class is :"<<*obj.classno<<endl;
cout<<"Division is :"<<*obj.div<<endl;
cout<<"Blood group is :"<<(char *)obj.bloodgroup<<endl;
cout<<"Date of birth is :"<<(char *)this->dob<<endl;
cout<<"Address is :"<<(char *)this->address<<endl;
cout<<"Telephone number is :"<<*this->telephone<<endl;
}
int personal::search(person *obj[])
{
char temp[30];
cout<<"Enter name :";
cin.ignore();
cin.getline(temp,30);
for(int i=0;i<count;i++)
{
if(strcmp(temp,(char *)obj[i]->name)==0)
{
return i;
}
}
return -1;
}
int main()
{
personal *obj1[10];
person *obj2[10];
count = 0;
char c='y';
int choice,p;
while(c=='y')
{
cout<<"Enter choice \n[1]Add data\n[2]Display data\n[3]Search\n[4]Delete\n";
cin>>choice;
switch(choice)
{
case 1:obj1[count]=new personal;
obj2[count]=new person;
obj1[count]->getdata(*obj2[count]);
count++;
break;
case 2:for(int i=0;i<count;i++)
{
obj1[i]->putdata(*obj2[i]);
cout<<endl;
}
break;
case 3:p=obj1[0]->search(obj2);
if(p==-1)
{
cout<<"not found"<<endl;
}
else
{
cout<<"found"<<endl;
obj1[p]->putdata(*obj2[p]);
}
break;
case 4:p=obj1[0]->search(obj2);
if(p==-1)
{
cout<<"not found"<<endl;
}
else
{
cout<<"Found "<<endl;
//obj1[p]->putdata(*obj2[p]);
cout<<"Preparing to delete"<<endl;
*obj1[p]=*obj1[count-1];
*obj2[p]=*obj2[count-1];
obj1[p]->putdata(*obj2[p]);
// cout<<endl<<obj1[count-1]<<endl;
delete obj1[count-1];
delete obj2[count-1];
count--;
}
break;
default:cout<<"Invalid choice ";
break;
}
cout<<"Do you want to continue :";
cin>>c;
}
return 0;
}
Comments
Post a Comment