// Listing 22.3
#include <iostream>
#include <string.h>
typedef unsigned long pDate;
enum SERVICE { PostMaster, Interchange,
CompuServe, Prodigy, AOL, Internet };
class String
{
public:
// constructors
String();
String(const char *const);
String(const String &);
~String();
// overloaded operators
char & operator[](int offset);
char operator[](int offset) const;
String operator+(const String&);
void operator+=(const String&);
String & operator= (const String &);
friend std::ostream & operator<<
(std::ostream& theStream, String& theString);
// General accessors
int GetLen()const { return itsLen; }
const char * GetString() const { return itsString; }
// static int ConstructorCount;
private:
String (int); // private constructor
char * itsString;
int itsLen;
};
// default constructor creates string of 0 bytes
String::String()
{
itsString = new char[1];
itsString[0] = '\0';
itsLen=0;
// std::cout << "\tDefault string constructor\n";
// ConstructorCount++;
}
// private (helper) constructor, used only by
// class methods for creating a new string of
// required size. Null filled.
String::String(int len)
{
itsString = new char[len+1];
int i;
for ( i = 0; i<=len; i++)
itsString[1] = '\0';
itsLen=len;
// std::cout << "\tString(int) constructor\n";
// ConstructorCount++;
}
// Converts a character array to a String
String::String(const char * const cString)
{
itsLen = strlen(cString);
itsString = new char[itsLen+1];
int i;
for ( i = 0; i<itsLen; i++)
itsString[i] = cString[i];
itsString[itsLen]='\0';
// std::cout << "\tString(char*) constructor\n";
// ConstructorCount++;
}
// copy constructor
String::String (const String & rhs)
{
itsLen=rhs.GetLen();
itsString = new char[itsLen+1];
int i;
for (i = 0; i<itsLen;i++)
itsString[i] = rhs[i];
itsString[itsLen] = '\0';
// std::cout << "\tString(String&) constructor\n";
// ConstructorCount++;
}
// destructor, frees allocated memory
String::~String ()
{
delete [] itsString;
itsLen = 0;
// std::cout << "\tString destructor\n";
}
String& String::operator=(const String & rhs)
{
if (this == &rhs)
return *this;
delete [] itsString;
itsLen=rhs.GetLen();
itsString = new char[itsLen+1];
int i;
for (i = 0; i<itsLen;i++)
itsString[i] = rhs[i];
itsString[itsLen] = '\0';
return *this;
// std::cout << "\tString operator=\n";
}
//non constant offset operator, returns
// reference to character so it can be changed
char & String::operator[](int offset)
{
if (offset > itsLen)
return itsString[itsLen-1];
else
return itsString[offset];
}
// constant offset operator for use
// on const objects (see copy constructor!)
char String::operator[](int offset) const
{
if (offset > itsLen)
return itsString[itsLen-1];
else
return itsString[offset];
}
// creates a new string by adding current
// string to rhs
String String::operator+(const String& rhs)
{
int totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
int i,j;
for (i = 0; i<itsLen; i++)
temp[i] = itsString[i];
for (j = 0; j<rhs.GetLen(); j++, i++)
temp[i] = rhs[j];
temp[totalLen]='\0';
return temp;
}
// changes current string, returns nothing
void String::operator+=(const String& rhs)
{
int rhsLen = rhs.GetLen();
int totalLen = itsLen + rhsLen;
String temp(totalLen);
int i,j;
for ( i = 0; i<itsLen; i++)
temp[i] = itsString[i];
for ( j = 0; j<rhs.GetLen(); j++, i++)
temp[i] = rhs[i-itsLen];
temp[totalLen]='\0';
*this = temp;
}
// int String::ConstructorCount = 0;
std::ostream& operator<<(
std::ostream& theStream,
String& theString)
{
theStream << theString.GetString();
return theStream;
}
class pAddress
{
public:
pAddress(SERVICE theService,
const String& theAddress,
const String& theDisplay):
itsService(theService),
itsAddressString(theAddress),
itsDisplayString(theDisplay)
{}
// pAddress(String, String);
// pAddress();
// pAddress (const pAddress&);
~pAddress(){}
friend std::ostream& operator<<(
std::ostream& theStream, pAddress& theAddress);
String& GetDisplayString()
{ return itsDisplayString; }
private:
SERVICE itsService;
String itsAddressString;
String itsDisplayString;
};
std::ostream& operator<<
( std::ostream& theStream, pAddress& theAddress)
{
theStream << theAddress.GetDisplayString();
return theStream;
}
class PostMasterMessage
{
public:
// PostMasterMessage();
PostMasterMessage(const pAddress& Sender,
const pAddress& Recipient,
const String& Subject,
const pDate& creationDate);
~PostMasterMessage(){}
void Edit(); // invokes editor on this message
pAddress& GetSender() { return itsSender; }
pAddress& GetRecipient() { return itsRecipient; }
String& GetSubject() { return itsSubject; }
// void SetSender(pAddress& );
// other member accessors
// operator methods here, including operator equals
// and conversion routines to turn PostMaster messages
// into messages of other formats.
private:
pAddress itsSender;
pAddress itsRecipient;
String itsSubject;
pDate itsCreationDate;
pDate itsLastModDate;
pDate itsReceiptDate;
pDate itsFirstReadDate;
pDate itsLastReadDate;
};
PostMasterMessage::PostMasterMessage(
const pAddress& Sender,
const pAddress& Recipient,
const String& Subject,
const pDate& creationDate):
itsSender(Sender),
itsRecipient(Recipient),
itsSubject(Subject),
itsCreationDate(creationDate),
itsLastModDate(creationDate),
itsFirstReadDate(0),
itsLastReadDate(0)
{
std::cout << "Post Master Message created. \n";
}
void PostMasterMessage::Edit()
{
std::cout << "PostMasterMessage edit function called\n";
}
int main()
{
pAddress Sender(
PostMaster, "jliberty@PostMaster", "Jesse Liberty");
pAddress Recipient(
PostMaster, "sliberty@PostMaster","Stacey Liberty");
PostMasterMessage PostMasterMessage(
Sender, Recipient, "Saying Hello", 0);
std::cout << "Message review... \n";
std::cout << "From:\t\t"
<< PostMasterMessage.GetSender() << std::endl;
std::cout << "To:\t\t"
<< PostMasterMessage.GetRecipient() << std::endl;
std::cout << "Subject:\t"
<< PostMasterMessage.GetSubject() << std::endl;
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
24小时攻克c++代码
共129个文件
cpp:126个
hpp:3个
需积分: 0 109 下载量 138 浏览量
2007-12-29
17:25:06
上传
评论 1
收藏 75KB ZIP 举报
温馨提示
24小时攻克c++代码
资源详情
资源评论
资源推荐
收起资源包目录
24小时攻克c++代码 (129个子文件)
driverprogram.cpp 7KB
passparmlist.cpp 7KB
parmlist.cpp 7KB
linkedlist.cpp 5KB
debugginglevels.cpp 5KB
usinginvariants.cpp 5KB
derivingadt.cpp 4KB
polyexceptions.cpp 3KB
overloadderive.cpp 3KB
shapeclass.cpp 2KB
implement.cpp 2KB
rect.cpp 2KB
exceptions.cpp 2KB
employeemain.cpp 2KB
adtshapeclass.cpp 2KB
virtualcopy.cpp 2KB
ptrtomember.cpp 2KB
usingtypedef.cpp 2KB
ptrtofunction.cpp 2KB
ptrarrayfunction.cpp 2KB
passingptrfunction.cpp 2KB
simpleevent.cpp 2KB
constructive.cpp 2KB
dynamiccast.cpp 2KB
conanddest.cpp 2KB
usingdefaults.cpp 2KB
passconstptr.cpp 2KB
passreftoobj.cpp 1KB
slicing.cpp 1KB
copyconstructors.cpp 1KB
overloadfunctions.cpp 1KB
arrayptrfunction.cpp 1KB
constptr.cpp 1KB
multivirtual.cpp 1KB
simpleclass.cpp 1KB
assignoperator.cpp 1KB
derivedobject.cpp 1KB
passobjectsbyref.cpp 1KB
preandpostfix.cpp 1KB
breaker.cpp 1KB
baseoverride.cpp 1KB
nestedif.cpp 1KB
postmasterinterface.cpp 1KB
usingthis.cpp 1011B
overloadincrement.cpp 995B
leaky.cpp 979B
prepostfix.cpp 976B
staticfunction.cpp 973B
staticmember.cpp 968B
newarray.cpp 958B
returnwithref.cpp 953B
pointerstore.cpp 941B
datamemberptr.cpp 939B
arrayonheap.cpp 933B
plusoperator.cpp 932B
addfunction.cpp 915B
returnwithptr.cpp 885B
usingdefine.cpp 843B
simpleinherit.cpp 839B
virtmethod.cpp 834B
switcher.cpp 829B
passbyptr.cpp 817B
assignreference.cpp 815B
passbyref.cpp 813B
manyreturns.cpp 812B
pointerheap.cpp 785B
complexwhile.cpp 763B
passbyvalue.cpp 756B
sizer.cpp 751B
returnref.cpp 746B
passbyvar.cpp 737B
defaultparm.cpp 718B
heapcreate.cpp 718B
objarray.cpp 706B
callingbase.cpp 703B
hidingbase.cpp 697B
pointeruse.cpp 678B
declarefunction.cpp 677B
counterconvertint.cpp 670B
cat.cpp 660B
heapaccess.cpp 630B
virtualmethods.cpp 626B
counterconvert2.cpp 609B
addressdemo.cpp 607B
simpleassert.cpp 602B
inliner.cpp 583B
fornested.cpp 535B
forempty.cpp 525B
bracesif.cpp 515B
localvar.cpp 513B
usingparen.cpp 509B
counterconvert.cpp 505B
properbraces.cpp 502B
localvar2.cpp 493B
usevar.cpp 482B
callfunc.cpp 477B
printingvalues.cpp 471B
counter.cpp 466B
ifelse.cpp 458B
createreference.cpp 449B
共 129 条
- 1
- 2
wulonghit
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- unity开发教程.docx
- 代码使用Pygame库实现了一个简单的烟花模拟 核心逻辑包括烟花和粒子类的定义,处理位置、爆炸、尾迹和绘制等操作
- Matlab Simulink 电力电子仿真-Flyback(反激电路)电路分析
- tudou-android-release.apk
- 数据分析教程.docx
- 基于matlab实现用有限元法计算电磁场的Matlab工具 .rar
- 基于matlab实现有限元算法 计算电磁场问题 边界条件包括第一类边界和第二类边界.rar
- 基于matlab实现用于计算不同车重下的电动汽车动力性和经济性.rar
- 基于matlab实现遗传算法求解多车场车辆路径问题 有多组算例可以用.rar
- 浏览器.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0