帮北航小妹妹做的一道她的C++的作业题.

本文介绍了一个名为UpperCase的C++类,该类继承自MyString,并实现了字符串转换为大写的功能。通过重载操作符和使用友元函数,使得类能够像内置类型一样操作,包括赋值、连接和输入输出。
UpperCase.h
None.gif #include  " stdafx.h "
None.gif#include 
" MyString.h "
None.gif
None.gif
// :public MyString,在声明里指定了继承.
None.gif
class  UpperCase: public  MyString
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    UpperCase( 
const char * str= 0 );
InBlock.gif    UpperCase( 
const string & str);
InBlock.gif    
virtual void copy( const char * str); 
InBlock.gif    
virtual void strCat(const string & str);
InBlock.gif    
virtual void strCat(const char * str);
InBlock.gif
InBlock.gif
InBlock.gif    
//这里是进行操作符重载,以便可以实现类似于str="123"或str+="123"这样的方式,这样使咱们的自定义类可以像普通类型一样进行操作
InBlock.gif
    UpperCase & operator =(const char * str);
InBlock.gif    UpperCase 
& operator +=(const char * str);
InBlock.gif
InBlock.gif    
//这里是使用友元方式进行的重载,我偿试用成员函数的方式重载,但不知道为什么报错.呵,钱能的C++书上有这样写,我在网上找资料也有这样写的.
InBlock.gif
    friend ostream & operator <<(ostream & outstream,UpperCase & str);
InBlock.gif    friend istream 
& operator >>(istream & instream,UpperCase & str);
InBlock.gif
private:
InBlock.gif    
void ToUpper(char * str);
ExpandedBlockEnd.gif}
;
UpperCase.cpp
None.gif #include  " stdafx.h "
None.gif#include 
" UpperCase.h "
None.gif
None.gif
// UpperCase的构造函数,其中:MyString(str)表示调用父类的这个构造函数
None.gif
UpperCase::UpperCase( const   char   * str):MyString(str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
this->ToUpper(this->myStr);
ExpandedBlockEnd.gif}
;
None.gif
None.gifUpperCase::UpperCase(
const  std:: string   & str):MyString(str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
this->ToUpper(this->myStr);
ExpandedBlockEnd.gif}
;
None.gif
// 下面的几个重写的方法我都是用类似于copy的方式,生成一个新的字符指针,然后转换成大写,再调用父类的相关方法
None.gif
void  UpperCase::copy( const   char   * str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
char * temp=new char[strlen(str)+1];
InBlock.gif    strcpy_s(temp,strlen(str)
+1,str);
InBlock.gif    
this->ToUpper(temp);
InBlock.gif    MyString::copy(temp);
InBlock.gif    delete temp;
ExpandedBlockEnd.gif}
;
None.gif
void  UpperCase::strCat( const   char   * str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
char * temp=new char[strlen(str)+1];
InBlock.gif    strcpy_s(temp,strlen(str)
+1,str);
InBlock.gif    
this->ToUpper(temp);
InBlock.gif    MyString::strCat(temp);
InBlock.gif    delete temp;
ExpandedBlockEnd.gif}
;
None.gif
void  UpperCase::strCat( const  std:: string   & str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
char * temp=new char[strlen(str.c_str())+1];
InBlock.gif    strcpy_s(temp,strlen(str.c_str())
+1,str.c_str());
InBlock.gif    
this->ToUpper(temp);
InBlock.gif    MyString::strCat(temp);
InBlock.gif    delete temp;
ExpandedBlockEnd.gif}
;
None.gif
// 我自己加的一个方法,进行大写转换
None.gif
void  UpperCase::ToUpper( char   *  str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
for(size_t i=0;i<strlen(str);i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(str[i]>='a' && str[i]<='z')
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            str[i]
+='A'-'a';
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// 下面这些是重载操作符的实现代码
None.gif
UpperCase  &  UpperCase:: operator   = ( const   char   *  str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
this->copy(str);
InBlock.gif    
return *this;
ExpandedBlockEnd.gif}

None.gifUpperCase 
&  UpperCase:: operator   += ( const   char   *  str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
this->strCat(str);
InBlock.gif    
return (*this);
ExpandedBlockEnd.gif}
;
None.gifostream 
&   operator   << (std::ostream  & outstream,UpperCase  &  str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    str.print();
InBlock.gif    
return outstream;
ExpandedBlockEnd.gif}
;
None.gif
None.gifistream 
&   operator   >> (std::istream  & instream,UpperCase  &  str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    instream
>>str.myStr;
InBlock.gif    
return instream;
ExpandedBlockEnd.gif}
;
基本上就是这样了.我把代码打包了,你可以下载看看,我的编译环境是VC++.Net 2005.

1.下载: 第4次作业的MyString实现
2.下载: UpperCase的实现
3.下载: UpperCase使用成员函数重载<<和>>操作符的实现(目前还有错误,正在找原因)
4.下载: UpperCase使用成员函数重载<<和>>操作符的实现(解决问题)

第3个错误的,其实在UpperCase类里没有问题,关键在于调用的时候.因为使用成员函数重载的<<和>>,这种方式下UpperCase对象是左操作数,调用的时候是这样的:str1<<cout;str1>>cin,而使用友元的方式下是:cout<<str1;cin<<str1,这样的形式.和平常的用法不一样(这个是不是推荐操作符重载使用友元而不是成员函数的原因啊?呵呵)

转载于:https://www.cnblogs.com/fxwdl/archive/2007/04/19/719586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值