PDA

View Full Version : سوال: تبدیل یک تابع (ساختار) به کلاس



Bvk_programmer
چهارشنبه 03 خرداد 1391, 11:44 صبح
سلام دوستان میخواستم این برنامه رو که با تابع نوشته شده به کلاس تبدیل کنم . ممنون میشم اگر کسی میدونه کد رو تبدیل کنه .

با تشکر

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
int x=1;
struct tell{
char name[20];
char family[20];
int phone;
} list[100];
void add()
{if (x>=100)
cout<<"sory. the fhone book is complete"<<endl;
else
cout<<"input first name :";
cin>>list[x].name;
cout<<"input last name :";
cin>>list[x].family;
cout<<"input phone number :";
cin>>list[x].phone;

x++;}
void search()
{int i,num,p;
char text[20];
cout<<"search by name(1) or tell number(2):";
cin>>p;
if(p==1)
{
cout<<"name:";
cin>>text;
for(i=1;i<=x;i++)
{
if (strcmpi(text,list[i].name)==0)
{
cout<<list[i].name<<endl;
cout<<list[i].family<<endl;
cout<<list[i].phone<<endl;
}
else cout<<"this name not here"<<endl;
}
}
else
if (p==2)
{
cout<<"tell number:";
cin>>num;
for(i=1;i<=x;i++)
{
if (num==list[i].phone)
{
cout<<list[i].name<<endl;
cout<<list[i].family<<endl;
cout<<list[i].phone<<endl;
}
else cout<<"this number not here"<<endl;
}
}
else cout<<"sorry"<<endl;
}
void list_()
{
int i;
for(i=1;i<x;i++)
{cout<<"----------------"<<endl;
cout<<"|"<<list[i].name<<"|";
cout<<"|"<<list[i].family<<"|";
cout<<"|"<<list[i].phone<<"|"<<endl;
cout<<"----------------"<<endl;
}
}
void delet()
{
int a,i,j;
cout<<"enter a fhone number for delete:";
cin>>a;
for(i=1;i<=x;i++)
{if (a==list[i].phone)
{for (j=i;j<=x;j++)
{strcpy(list[j+1].name,list[j].name);
strcpy(list[j+1].family,list[j].family);
list[j+1].phone=list[j].phone;
}
}
else
cout<<"this number not here"<<endl;}}
int main()
{
int c,t,i,y;
long f;
while(1)
{
cout<<"1-Add"<<endl;
cout<<"2-Search"<<endl;
cout<<"3-List"<<endl;
cout<<"4-Delete"<<endl;
cout<<"5-Exit"<<endl<<"insert a number then press enter:";


cin>>c;
switch (c){
case 1:
add();
break;
case 2 :
search();
break;
case 3 :
list_();
break;
case 4 :
delet();
break;
case 5 :
return 0;
break;
}
}
getch();
return 0;
}

ASGGSA
چهارشنبه 03 خرداد 1391, 13:34 عصر
سلام.
ببینید این کار رو باید خود شما انجام بدید اگه با کلاس آشنا هستید که کاری نداره اما اگه آشنا نیستید بهتره یه کتاب بخرید. حالا می تونید توابع خودتون را بصورت زیر
تعریف کنید. اگر می خواهید توابع خود بصورت سراسری استفاده بشه در زیر public تعریف و اگر بصورت خصوصی باشد در زیر private تعریف کنید.

class CClass
{
public:
function1();
function2();
function3();
private:
function4();
function5();
};