类的学习

wariob 2009-07-31 12:39:19
#include <iostream.h>
#include "String.h"
int main()
{
int aCnt =0, eCnt = 0, iCnt=0,uCnt=0,
theCnt=0, itCnt=0,wdCnt=0,notVowel=0;
//为了使用operator==(const char*)
//我们并不定义The("the"),TI("It")
String buf,the("the"),it("it");
//调用operator>>(istream&,String&)
while( cin >> buf ){
++wdCnt;

//调用operator<<(ostream&,const String&)
cout<<buf<< ' ';
if ( wdCnt%12==0 )
cout <<endl;
//调用String::operator==(const String&) and
// String::operator==(const char*);
if ( buf ==the || buf == "The" )
++theCnt;
else
if (buf == the || buf == "The" )
++itCnt;

//调用String::size()
for( int ix =0; ix < buf.size(); ++ix )
{
//调用String::operator[](int)
switch( buf[ix] )
{
case 'a': case 'A':
++aCnt;
break;
case 'e': case 'E':
++eCnt;
break;
case 'i': case 'I':
++iCnt;
break;
case 'o': case 'O':
++oCnt;
break;
case 'u': case 'U';
++uCnt;
break;
default:
++notVowel;
break;
}
}
}
//调用operator<<( ostream&, const String& )
cout << "\n\n"
<<"Words read: " << wdCnt <<"\n\n"
<<"the/The: " <<theCnt <<'\n'
<<"it/It: " <<itCnt <<"\n\n"
<<"non-vowels read: " << notVowel << "\n\n"
<<"a: "<<aCnt<<'\n'
<<"e: "<<eCnt<<'\n'
<<"i: "<<iCnt<<'\n'
<<"o: "<<oCnt<<'\n'
<<"u: "<<uCnt<<endl;
}


...全文
115 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2009-07-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lijian22500 的回复:]
没看懂什么意思
[/Quote]
me too
yangyunzhao 2009-07-31
  • 打赏
  • 举报
回复
压根没有看明白,太多了,格式又乱,无从看起
lijian22500 2009-07-31
  • 打赏
  • 举报
回复
没看懂什么意思
mstlq 2009-07-31
  • 打赏
  • 举报
回复
纯代码帖?
starcat 2009-07-31
  • 打赏
  • 举报
回复
帮顶。

我刚进入“类”的学习中,果然头大。
wariob 2009-07-31
  • 打赏
  • 举报
回复
#include <iostream>
class String;

istream& operator>>(istream&,String&);
ostream& operator<<( ostream&, const String& );

class String{
public:
//一组重载的构造函数
//提供自动初始化功能
//String str1; //String()
//String str2( "literal "); // String ( const char* );
//String str3( str2); // String ( const String&);
String();
String( const char*);
String( const String&);

//析构函数:自动析构
~String();

//一组重载的赋值操作符
//str1 = str2
//str3 = "a string literal"
String& operator=( const Stirng&);
String& operator=( const char*);

//一组冲裁的等于操作符
//str1 == str2;
//str3 == "a string literal";
bool operator==( const String& );
bool operator==( const char*);

//重载的下标操作符
//str1[ 0 ] = str2 [ 0 ];
char& operator[]( int );

//成员访问函数
int size() { return_size; }
char* c_str(){ return_stirng;}

private:
int _size;
char *_string;
};
taodm 2009-07-31
  • 打赏
  • 举报
回复
楼主学到了啥没看出来,但是用了本垃圾教材和垃圾的VC6到是看出来了。
珍惜生命啊,远离这2者。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧