payoffer

PayOffers.in

Tuesday, March 25, 2014

Program for database using Static member functions,friend class,this pointer,inline code and dynamic memory allocation in C++

Description :
1) Static member functions :These can be called without any instance(object) of the class .
2)Friend Class :Whenever we create a friend class,all the member function of the friend class also become the friend of other class.
3)This pointer :It is used for pointing to the object for which the meber function is called.
4)Inline function : ‘inline ‘keyword is used to define these functions.Whenever this function is called,the compiler will replace the function call with the actual code.
5)Dynamic Memory allocation :Whenever memory is allocated at runtime,it is called dynamic memory allocation.It is done using ‘new’ operator.

CODE :
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
class data
{
char name[30],dob[15],bg[5],add[30],no[15];
int height,weight;
public:
data()
{
strcpy(name,”");
strcpy(dob,”00-00-0000″);
strcpy(bg,”");
strcpy(add,”");
strcpy(no,”");
height=0;
weight=0;
}
static int count;
friend class personal;
static void dispcount()
{
cout<<”\n\n\nTOTAL NUMBER OF RECORDS CREATED: “<<count;
}
};
class personal
{
char policy_no[10],license_no[10];
public:
personal()
{
strcpy(policy_no,”");
strcpy(license_no,”");
}
friend class data;
void getdata(data *b);
void putdata(data *b);
};

int data::count=0;
void main()
{
personal *a[10];
data *c[10];
int n=0,i,choice;
char ans;
clrscr();
do
{
cout<<”\n\nMENU: “;
cout<<”\n\n\t1.Create\n\n\t2.Display”;
cout<<”\n\nEnter your choice: “;
cin>>choice;
switch(choice)
{
case 1:
do
{
a[n]=new personal;
c[n]=new data;
a[n]->getdata(c[n]);
n++;
data::dispcount();
cout<<”\n\nDo you want to add more records?(y/n): “;
fflush(stdin);
cin>>ans;
}while(ans==’y’ || ans==’Y');
break;
case 2:
for(int i=0;i<n;i++)
a[i]->putdata(c[i]);
data::dispcount();
break;
default:
cout<<”\n\nPlease enter correct choice.”;
break;
}
cout<<”\n\nDo you want to continue?(y/n): “;
fflush(stdin);
cin>>ans;
}while(ans==’y’ || ans==’Y');
delete[]a;
delete[]c;
getch();
}
void personal :: getdata(data *b)
{
cout<<”\n\n\tENTER THE DETAILS”;
cout<<”\n ————————–”;
cout<<”\n\nNAME: “;
gets(b->name);
cout<<”\n\nDATE OF BIRTH(DD-MM-YYYY): “;
gets(b->dob);
cout<<”\n\nADDRESS: “;
gets(b->add);
cout<<”\n\nTELEPHONE NUMBER: “;
gets(b->no);
cout<<”\n\nBLOOD GROUP: “;
gets(b->bg);
cout<<”\n\nHEIGHT: “;
cin>>b->height;
cout<<”\n\nWEIGHT: “;
cin>>b->weight;
cout<<”\n\nDRIVING LICENSE NUMBER: “;
gets(this->license_no);
cout<<”\n\nPOLICY NUMBER: “;
gets(this->policy_no);
b->count++;
}
void personal :: putdata(data *b)
{
cout<<”\n\n\n——————————————–”;
cout<<”\n\t…. PERSONAL DATABASE ….”;
cout<<”\n——————————————–”;
cout<<”\n\nNAME : “<<b->name;
cout<<”\n\nDATE OF BIRTH : “<<b->dob;
cout<<”\n\nADDRESS : “<<b->add;
cout<<”\n\nTELEPHONE NUMBER : “<<b->no;
cout<<”\n\nBLOOD GROUP : “<<b->bg;
cout<<”\n\nHEIGHT : “<<b->height;
cout<<”\n\nWEIGHT : “<<b->weight;
cout<<”\n\nDRIVING LICENSE NO.: “<<this->license_no;
cout<<”\n\nPOLICY NUMBER : “<<this->policy_no;
}

No comments:

Post a Comment