求高手帮助一下C++中如何封装函数。。
我写了几个字符串函数想把它们封装一下,但不知道怎么封装,求高手帮助一下。。。
#include<iostream>
using namespace std;
class String {
public:
char a[30];
char b[30];
char *p1;
char *p2;
int number;
int n;
void strcpy();
void strncpy();
void lengh(int number);
void strcat();
void strncat();
void strlwr();
void strupr();
};
void String::strcpy(){
cout<<"strcpy:"<<endl;
cin>>a;
// cin>>b;
p1=a;
p2=b;
while(*p1!='\0'){
*p2=*p1;
p1++;
p2++;
}
*p2='\0';
cout<<b<<endl;
}
void String::strncpy(){
cout<<"strncpy:"<<endl;
cin>>a;
cin>>n;
p1=a;
p2=b;
while(p1!=&a[n])
{
*p2=*p1;
p1++;
p2++;
}
*p2='\0';
cout<<b<<endl<<endl;
}
void String::lengh(int number){
cin>>a;
p1=a;
while(*p1!='\0')
{
number++;
p1++;
}
cout<<number<<endl<<endl;
}
void String::strcat(){
cout<<"strcat:"<<endl;
cin>>a;
cin>>b;
p1=a;
p2=b;
while(*p1!='\0')
{
p1++;
}
while(*p2!='\0')
{
*p1=*p2;
p1++;
p2++;
}
*p1='\0';
cout<<a<<endl<<endl;
}
void String::strncat(){
cout<<"strncat:"<<endl;
cin>>a;
cin>>b;
cin>>n;
p1=a;
p2=b;
while(*p1!='\0')
{
p1++;
}
while(p2!=&b[n])
{
*p1=*p2;
p1++;
p2++;
}
*p1='\0';
cout<<a<<endl<<endl;
}
void String::strlwr(){
cout<<"strlwr:"<<endl;
cin>>a;
p1=a;
while(*p1!='\0')
{
while(*p1>64 && *p1<91)
{
*p1=*p1+32;
p1++;
}
*p1=*p1;
p1++;
}
cout<<a<<endl<<endl;
}
void String::strupr(){
cout<<"strupr:"<<endl;
cin>>a;
p1=a;
while(*p1!='\0')
{
while(*p1>96 && *p1<123)
{
*p1=*p1-32;
p1++;
}
*p1=*p1;
p1++;
}
cout<<a<<endl<<endl;
}
int main()
{
String str;
str.strcpy();
str.strncpy();
str.lengh(0);
str.strcat();
str.strncat();
str.strlwr();
str.strupr();
return 0;
}