第三十六讲 浅出class(2)

      学习C语言的时候我们一般用char来表示字符串,但是总觉得不便,如今,我们学习了C++,又开始接触到C++的class,我们倒不如来尝试一下写一个字符串类型——My_String,我们今天先给出原型和实现,明天再来作详解。
——————————————–
//My_String.h
#pragma once
#include <iostream>using std::ostream;

using std::istream;


namespace My_Code{
class My_String{
public:
My_String();

My_String(const char* str);

My_String(const My_String&

);

~My_String();

int length()const
{

return m_len;

}
My_String&

operator=(const My_String&

);

My_String&

operator=(const char*);

char&

operator[](int i);

const char&

operator[](int i) const;

friend bool operator<(const My_String&

str1,const My_String&

str2);

friend bool operator>(const My_String&

str1,const My_String&

str2);

friend bool operator==(const My_String&

str1,const My_String&

str2);

friend ostream&

operator<<(ostream &

os,const My_String &

str);

friend istream&

operator>>(istream &

is,My_String &

str);

private:
char* m_str;

int m_len;

};

}
—————————————————–
接下来我们来实现这个My_String
——————————————————-
//My_String.cpp
#include “My_String.h”
#include <cstring>using std::cin;

using std::cout;


namespace My_Code{
My_String::My_String()
{
m_len = 4;

m_str = new char[1];

m_str[0] = ‘�’;

}
        My_String::My_String(const char* str)
{
m_len = std::strlen(str);

m_str = new char[m_len+1];

strcpy_s(m_str,m_len+1,str);

}
         My_String::My_String(const My_String&

str)
{
m_len = str.m_len;

m_str = new char[m_len+1];

strcpy_s(m_str,m_len+1,str.m_str);

}
         My_String::~My_String()
{
delete [] m_str;

}
          My_String&

My_String::operator=(const My_String &

str)
{
if(this == &

str)

return *this;

delete [] m_str;

m_len = str.m_len;

m_str = new char[m_len+1];

strcpy_s(m_str,m_len+1,str.m_str);

return *this;

}
          My_String&

My_String::operator=(const char *s)
{
delete [] m_str;

m_len = std::strlen(s);

m_str = new char[m_len+1];

strcpy_s(m_str,m_len+1,s);

return *this;

}
          char&

My_String::operator[](int i)
{

return m_str[i];

}
          const char&

My_String::operator[](int i) const
{

return m_str[i];

}
         bool operator<(const My_String &

str1,const My_String &

str2)
{

return (std::strcmp(str1.m_str,str2.m_str)<0);

}
          bool operator>(const My_String &

str1,const My_String &

str2)
{

return str2<str1;

//调用上面的operator<
}
          bool operator==(const My_String &

str1,const My_String &

str2)
{

return (std::strcmp(str1.m_str,str2.m_str)==0);

}
         ostream&

operator<<(ostream &

os,const My_String &

str)
{
os<<str.m_str;

return os;

}
        istream&

operator>>(istream &

is,My_String &

str)
{
char temp[80];

is.get(temp,80);

if(is)
str = temp;

//调用operator=
while(is &

&

is.get() != ‘n’)
continue;

return is;

}
}
——————————————————
下面,我们用一个程序来测试一下我们的My_String类:
——————————————
//TestMy_String.cpp
 using namespace My_Code;

using std::cout;

using std::cin;

using std::endl;

const
int
num = 5;

My_String name;

cout<<“Hi,What’s your name?n”;

cin>>name;

cout<<name<<“,please enter up to “<<num<<” short sayings<空行则退出>”<<endl;

My_String sayings[num];

char temp[80];

int i;

for(i=0;

i<5;

i++)
{
cout<<i+1<<“:”;

cin.get(temp,80);

while(cin &

&

cin.get() != ‘n’)
continue;

if(!cin || temp[0] == ‘�’)
break;

else
sayings[i] = temp;

//operator=
}
int total = i;

if(total >0)
{
cout<<“here are your sayings:n”;

for(i=0;

i<total;

i++)
{
cout<<sayings[i][0]<<“:”<<sayings[i]<<endl;

}
int shortest = 0;

int first = 0;

for(i=1;

i<total;

i++)
{
if(sayings[i].length() <sayings[shortest].length())
shortest = i;

if(sayings[i]<sayings[first])
first = i;

}
cout<<“shotest sayings:n”<<sayings[shortest]<<endl;

cout<<“First alphabetically:n”<<sayings[first]<<endl;

}
——————————————–
第三十六讲 浅出class(2)

发表评论