/**
* @brief 这里重载了string类,调用了静态库!listring.a
*/
prototype.h - /*
- * prototype.h
- *
- * Created on: Sep 24, 2012
- * Author: hl
- */
- #ifndef _PROTOTYPE_H_
- #define _PROTOTYPE_H_
- #include <string>
- using namespace std;
- class Mail;
- /**
- * @brief 广告模板
- */
- class AdvTemplate
- {
- public:
- AdvTemplate();
- AdvTemplate(string sub, string adv);
- public:
- string getAdvSub(); /// < 获得广告主题
- string getAdvMsg(); /// < 获得广告内容
- private:
- string advSubject; /// < 广告主题
- string advContext; /// < 广告内容
- };
- /**
- * @brief 对象克隆
- */
- class Clone
- {
- public:
- virtual Mail * getClone() = 0;
- };
- /**
- * 邮件类
- */
- class Mail : public Clone
- {
- public:
- Mail(AdvTemplate adv); /// < 获取邮件主题和内容
- public:
- void setRec(string rec); /// < 设置收件人
- void setAppe(string appe); /// < 设置名称
- void setTail(string tai); /// < 设置结尾
- string getSub();
- string getContext();
- string getRec();
- string getAppe();
- string getTail();
- Mail * getClone();
- private:
- string receiver; /// < 接收者
- string appellation; /// < 名称
- string tail; /// < 结尾
- string subject; /// < 主题
- string context; /// < 内容
- };
- #endif /* _PROTOTYPE_H_ */
prototype.cpp- //============================================================================
- // Name : prototype.cpp
- // Author : hl
- // Version :
- // Copyright : Copyright (c) 2012 Tiros
- // Description : Prototype Model in C++, Ansi-style
- //============================================================================
- #include "prototype.h"
- #include "sendmail.h"
- #include "string.h"
- #include <stdlib.h>
- #include <time.h>
- #include <iostream>
- #include <string.h>
- #include <stdio.h>
- using namespace std;
- #define random(x) (rand()%x + 123)
- AdvTemplate::AdvTemplate()
- {
- }
- AdvTemplate::AdvTemplate(string sub, string adv)
- {
- advSubject = sub;
- advContext = adv;
- }
- /**
- * @brief 获得广告内容
- */
- string AdvTemplate::getAdvMsg()
- {
- return this->advContext;
- }
- /**
- * @brief 获得广告主题
- */
- string AdvTemplate::getAdvSub()
- {
- return this->advSubject;
- }
- /**
- * 邮件类
- */
- Mail::Mail(AdvTemplate adv)
- {
- subject = adv.getAdvSub();
- context = adv.getAdvMsg();
- }
- void Mail::setRec(string rec)
- {
- receiver = rec;
- }
- void Mail::setAppe(string appe)
- {
- appellation = appe;
- }
- void Mail::setTail(string tai)
- {
- tail = tai;
- }
- string Mail::getSub()
- {
- return subject;
- }
- string Mail::getContext()
- {
- return context;
- }
- string Mail::getRec()
- {
- return receiver;
- }
- string Mail::getAppe()
- {
- return appellation;
- }
- string Mail::getTail()
- {
- return tail;
- }
- Mail * Mail::getClone()
- {
- Mail * pMail = new Mail(*this);
- return pMail;
- }
- /**
- * @brief 整数转换为字符串
- */
- char *myitoa(int num, char *str, int radix)
- {
- /* 索引表 */
- char index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- unsigned unum;
- int i = 0, j, k;
- if (radix == 10 && num < 0) /* 十进制负数 */
- {
- unum = (unsigned) -num;
- str[i++] = '-';
- }
- else
- unum = (unsigned) num; /* 其他情况 */
- /* 逆序 */
- do
- {
- str[i++] = index[unum % (unsigned) radix];
- unum /= radix;
- } while (unum);
- str[i] = '\0';
- /* 转换 */
- if (str[0] == '-')
- k = 1; /* 十进制负数 */
- else
- k = 0;
- return str;
- }
- int main()
- {
- /// < 创建邮件
- Mail mal(*(new AdvTemplate("幸运大奖", "犬友头像征集活动开始了,欢迎到家踊跃报名!")));
- char buf[128];
- srand(time(0));
- for (int i = 0; i < 5; i++)
- {
- memset(buf, 0, 128);
- myitoa(random(123456), buf, 10);
- /// < 自己封装了一个String函数,这样省去了以上的字符串操作!不过需要將string函数替换!不过我再修改
- /// < 下库,提供一个获取字符串的函数!
- String b = "@qq.com";
- String d = buf;
- String c = d + b;
- char * abc = c.getStr();
- Mail * pMail = mal.getClone(); /// < 得到clone版本,这样再执行多线程操作才不会影响结果!省去了我们自己控制线程pthread_create!
- cout << "address1 ---------- "<< pMail << endl; /// <
- pMail->setRec(abc);
- delete abc;
- pMail->setAppe("xiaodou");
- pMail->setTail("lego公司");
- SendMail::sendMail(*pMail);
- delete pMail; /// < 执行了delete之后,可能每次分配的地址都相同!所以每次你看到address结果都可能相同!
- }
- return 0;
- }
sendmail.h- /*
- * sendmail.h
- *
- * Created on: Sep 24, 2012
- * Author: hl
- */
- #ifndef _SENDMAIL_H_
- #define _SENDMAIL_H_
- #include "prototype.h"
- /**
- * @brief 写信的过程 - 写信的内容、把信放到信封、把信送到邮局、邮局送信
- */
- class SendMail
- {
- public:
- static void sendMail(Mail mal);
- };
- #endif /* _SENDMAIL_H_ */
sendmail.cpp- /*
- * sendmail.cpp
- *
- * Created on: Sep 24, 2012
- * Author: hl
- */
- #include "sendmail.h"
- #include <iostream>
- using namespace std;
- void SendMail::sendMail(Mail mal)
- {
- cout << "姓名 " << mal.getAppe() << "\n内容 " << mal.getContext() << "\n收信人 " << mal.getRec()
- << "\n主题 " << mal.getSub() << "\n结尾 " << mal.getTail() << endl;
- }
string.h- /*
- * string.h
- *
- * Created on: Sep 24, 2012
- * Author: hl
- */
- #ifndef _STRING_H_
- #define _STRING_H_
- /**
- * @brief 字符串操作符重载 - 实现+、
- * 注意:
- * s大小128,针对具体的硬件平台可适当修改!
- * 使用示例:
- * String a = "hello";
- * String b = "kitty";
- * String c;
- * c = a + b + " " + "$" + f + ".com";
- */
- class String
- {
- public:
- String();
- String(char * str);
- public:
- char * getStr();
- void display();
- String operator+(String b);
- private:
- char s[128];
- };
- #endif /* _STRING_H_ */
阅读(450) | 评论(0) | 转发(0) |