能力值:
( LV3,RANK:20 )
|
-
-
2 楼
#include<iostream.h>
#include<stdio.h>
#include <string.h>
class Count
{
public:
Count();
void StartCount(char *str,char *p);
void ShowCount();
~Count(){};
private:
char *itsStr,*itsP;
};
Count::Count()
{
}
void Count::StartCount(char *str,char *p)
{
itsStr=str;
itsP=p;
}
void Count::ShowCount()
{
int i,count=0;
for(i=0;itsStr[i]!='\0';i++)
{
if(itsStr[i]==*itsP)
{
count++;
}
}
cout<<"字母a的个数: "<<count<<endl;
}
int main()
{
Count mycount;
char str[1024];
char *p=new char;
cout<<"请输入一串英文字符: "<<endl;
gets(str);
cout<<"请输入要统计的字母: "<<endl;
scanf("%c",p);
mycount.StartCount(str,p);
mycount.ShowCount();
delete p;
return 0;
}
总结:
(1)在类里面定义的函数,在实现其函数作用的时候,必须在前面加一个类名,比如:
void Count::ShowCount()
{
int i,count=0;
for(i=0;itsStr[i]!='\0';i++)
{
if(itsStr[i]==*itsP)
{
count++;
}
}
cout<<"字母a的个数: "<<count<<endl;
}
(2)在类里面申明构造函数时,在类外面也要实现构造函数,即使没有功能
Count::Count()
{
}
(3)在动态申请内存空间是,用*p=new int,那么在后面还要删除这个内存空间,也就是释放他:
char *p=new char;
.....
.....
delete p;
|
|
|