- 论坛徽章:
- 0
|
-
- CPhone类定义
- #include <string>
- #include <windows.h>
- #pragma comment(lib, "Ws2_32.lib ")
- #define wchar_t WCHAR
- class CPhone
- {
- public:
- //构造函数列表
- CPhone();
- CPhone(const WCHAR *pBrand, const unsigned int price, const char *phoneNumber, const bool isSup3G);
- CPhone(const CPhone& phone);
-
- //析构函数
- ~CPhone();
-
- //重载复制操作符
- CPhone& operator =(const CPhone& phone);
-
- //类操作函数,基本上都是复制构造函数
- WCHAR* getPhoneBrand(const CPhone& phone); //获得电话的品牌
- int getPhonePrice(const CPhone& phone); //获得电话的价格
- char* getPhoneNumber(const CPhone& phone); //获得电话号码
- bool isSupport3G(const CPhone& phone); //查询是否支持3G
- char* changePhoneNumber(char *newPhoneNumber);//更改电话号码,该函数调用后,必须显式释放申请的内存
- void displayPhoneBrand(void); //显示电话品牌
- void displayPhonePrice(void); //显示电话价格
- void displayPhoneNumber(void);//显示电话号码
-
- private:
- WCHAR *m_pBrand; //电话品牌
- unsigned int m_price; //电话价格
- char *m_phoneNumber; //电话号码
- bool m_isSupport3G; //是否支持3G
- };
复制代码- CPhone类成员的实现
- //#include <stdio.h>
- #include <iostream>
- #include "stdafx.h"
- #include <wininet.h>
- #include <assert.h>
- #include "CPhone.h"
- CPhone::CPhone() : m_pBrand(NULL), m_price(0), m_phoneNumber(NULL), m_isSupport3G(FALSE) {}
- CPhone::CPhone(const WCHAR *pBrand, const unsigned int price, const char *phoneNumber, const bool isSup3G)
- {
- assert(NULL!=pBrand);
- assert(NULL!=phoneNumber);
- this->m_pBrand = new WCHAR(wcslen(pBrand)+1);
- this->m_phoneNumber = new char(strlen(phoneNumber)+1);
-
- if ((NULL!=this->m_pBrand) && (NULL!=this->m_phoneNumber))
- {
- wcscpy(this->m_pBrand, pBrand);
- this->m_price = price;
- strcpy(this->m_phoneNumber, phoneNumber);
- this->m_isSupport3G = isSup3G;
- }
- }
- CPhone::CPhone(const CPhone& phone)
- {
- assert(NULL!=phone.m_pBrand);
- assert(NULL!=phone.m_phoneNumber);
- this->m_pBrand = new WCHAR(wcslen(phone.m_pBrand)+1);
- this->m_phoneNumber = new char(strlen(phone.m_phoneNumber)+1);
- if ((NULL!=this->m_pBrand) && (NULL!=this->m_phoneNumber))
- {
- wcscpy(this->m_pBrand, phone.m_pBrand);
- this->m_price = phone.m_price;
- strcpy(this->m_phoneNumber, phone.m_phoneNumber);
- this->m_isSupport3G = phone.m_isSupport3G;
- }
- }
- CPhone& CPhone::operator =(const CPhone& phone)
- {
- if (this == &phone)
- {
- return *this;
- }
-
- delete this->m_pBrand;
- delete this->m_phoneNumber;
- assert(phone.m_pBrand);
- assert(phone.m_phoneNumber);
- this->m_pBrand = new WCHAR(wcslen(phone.m_pBrand)+1);
- this->m_phoneNumber = new char(strlen(phone.m_phoneNumber)+1);
-
- if ((NULL==this->m_pBrand) || (NULL==this->m_phoneNumber))
- {
- exit(1);
- }
-
- wcscpy(this->m_pBrand, phone.m_pBrand);
- this->m_price = phone.m_price;
- strcpy(this->m_phoneNumber, phone.m_phoneNumber);
- this->m_isSupport3G = phone.m_isSupport3G;
-
- return *this;
- }
- CPhone::~CPhone()
- {
- delete m_pBrand;
- delete m_phoneNumber;
- m_pBrand = NULL;
- m_phoneNumber = NULL;
- }
- /***************************
- 函数目的:返回CPhone对象的品牌
- 参数:CPhone对象的const引用
- 返回值:指向Cphone对象品牌字符串的首指针
- ***************************/
- WCHAR* CPhone::getPhoneBrand(const CPhone& phone)
- {
- if (NULL==phone.m_pBrand)
- {
- return NULL;
- }
-
- //函数返回电话品牌字符数组的头指针,调用时可以用数组保存该字符串然后输出
- return phone.m_pBrand;
- }
-
- int CPhone::getPhonePrice(const CPhone& phone)
- {
- if (0==phone.m_price)
- {
- return 0;
- }
- return phone.m_price;
- }
- char* CPhone::getPhoneNumber(const CPhone& phone)
- {
- if (NULL==phone.m_phoneNumber)
- {
- return NULL;
- }
-
- //该函数使用方法与getPhoneBrand()一致
- return phone.m_phoneNumber;
- }
- bool CPhone::isSupport3G(const CPhone& phone)
- {
- return phone.m_isSupport3G;
- }
- char* CPhone::changePhoneNumber(char *newPhoneNumber)
- {
- assert(NULL!=newPhoneNumber);
- if (strlen(this->m_phoneNumber) < strlen(newPhoneNumber))
- {
- std::cout<<"Error";
- return NULL;
- }
- strcpy(this->m_phoneNumber, newPhoneNumber);
- return this->m_phoneNumber;
- }
- void CPhone::displayPhoneBrand(void)
- {
- if (NULL==this->m_pBrand)
- {
- return ;
- }
- setlocale(LC_ALL, "");
- //setlocale(LC_ALL, NULL);
- //WCHAR *p = this->m_pBrand;
- while (*this->m_pBrand!='\0')
- {
- //wprintf(L"%S\n ", *this->m_pBrand);
- std::wcout<<*this->m_pBrand;
- this->m_pBrand++;
- }
- }
- void CPhone::displayPhonePrice(void)
- {
- if (0==this->m_price)
- {
- return ;
- }
- std::cout<<this->m_price;
- }
- void CPhone::displayPhoneNumber(void)
- {
- if (NULL==this->m_phoneNumber)
- {
- return ;
- }
-
- //char *p = this->m_phoneNumber;
- while (*this->m_phoneNumber!='\0')
- {
- std::cout<<*this->m_phoneNumber;
- this->m_phoneNumber++;
- }
- }
复制代码- main函数
- #include "CPhone.h"
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- //setlocale(LC_ALL, "");
- WCHAR arry[] = L"诺基亚";
- CPhone nokia(arry, 1500, "13030204020", FALSE);
- nokia.displayPhoneBrand();
- cout<<endl;
- nokia.displayPhoneNumber();
- cout<<endl;
- nokia.displayPhonePrice();
- getchar();
- return 0;
- }
复制代码 为什么nokia.displayPhoneBrand()输出的结果是358342252220122 |
|