C++实现用户自定义类
本例以自定义类 MyString 为例子,可以说,这是说接触C++以来第一次觉得这语言好玩有趣的地方,感觉非常有微妙性!!!
题目:
25 统计字符串个数
作者: Turbo时间限制: 1S章节: OO:类
问题描述 :
实验目的:静态成员、构造函数、析构函数、等于号的使用。
实验内容:
实现一个自己的字符串类MyString,要求包含以下函数:
构造函数:
MyString(unsigned n,char c):将字符c重复n次得到字符串
MyString(char *p):根据传入的字符数组得到字符串
根据需要,还需要设计默认构造函数、拷贝构造函数等其它函数。
其中,MyString类需要提供一个静态方法GetCount,功能为获取到本程序在运行的过程中某时刻有多少个MyString的对象。
本次实验提供了部分代码,请使用以下代码调用MyString类的功能完成输入、处理、输出操作:
void fun1(int n)
{
MyString strArr[n];
MyString *pStr;
cout<<strArr[0].GetCount()<<endl;
}
void fun2(int n)
{
MyString *pStr=new MyString[n];
cout<<MyString::GetCount()<<endl;
}
void fun3(MyString &s1, MyString s2)
{
MyString s3;
s3=s1=s2;
cout<<MyString::GetCount()<<endl;
s3.ShowStr();
cout<<endl;
}
int main( )
{
MyString s1;
MyString s2;
int n;
char charArr[20];
int op;
while (cin >> op)
{
switch (op)
{
case 1:
{
cin>>n;
fun1(n);
break;
}
case 2:
{
cin>>n;
fun2(n);
break;
}
case 3:
{
int m;
char ch;
cin>>m>>ch;
s2=MyString(m,ch);
fun3(s1,s2);
s1.ShowStr();
cout<<endl;
break;
}
case 4:
{
cin >> charArr;
s1=MyString(charArr);
cout<<MyString::GetCount()<<endl;
s1.ShowStr();
cout<<endl;
break;
}
}
}
return 0;
}
输入说明 :
可输入多组测试数据,每组测试数据包含两行:
第一行输入一个操作的种类,
第二行输入所需要的参数:
对于第1个操作和第2个操作,第二行输入一个整数。
对于第3个操作,第二行输入一个整数及一个字符。
对于第4个操作,第二行输入一个字符串。
输出说明 :
具体输出见函数中的输出语句及输出范例。
输入样例
1
3
2
3
1
3
3
10 *
4
abcd
对应输出样例
5
5
8
7
**********
**********
5
abcd
本大侠另外有话要说,既然是用户自定义的类,就少用或者不要用到库函数,有人解答本题进入借助了 库函数里的<String>类,,,呵呵,这么改头换面自己到头来啥都没学到,AC了又有个JB用。。。 我也是查了很多资料,看了些算法书、网站上也找了相应知识点,写成的代码贴上:
// 2017.04.02 T25. 好题目,训练!!
//自定义类和内存自动分配...尽可能的不要借助任何库函数!
//School ~ DongHua University
//Turbo Test System , Test 25 -- Record ~
// Codes created by the rebuilt boy ManTruer
//Student ID : 151340112
//Class : Xin"an 1501
//Date : 2017.04.03
#include <iostream>
#include <cstring>
using namespace std;
class MyString{//使用内存分配new出自定义的类MyString...
private:
char *p_str; //pointer to string ...
int len; //string.size()
static int CountObj; //number of objects
public:
static int GetCountObj();
void ShowStr(){ cout<<p_str; }
MyString();
MyString(int ,char );
MyString(const char * );
MyString( const MyString &);
~MyString();
MyString & operator=(const MyString &);//重载赋值运算符,字符串赋值
MyString & operator=(const char*); //单个字符赋值
char &operator[]( int ); //定义元素索引标识
const char & operator[]( int ) const ;
friend bool operator==(const MyString &, const MyString &);//重载判等运算符
friend bool operator<(const MyString &, const MyString &); //小于号
friend bool operator>(const MyString &st1, const MyString &); //大于号
friend ostream & operator<<(ostream &,const MyString &);//重载输出运算符
friend MyString & operator+(MyString &, MyString &) ;//加法,相连接
};
int MyString::CountObj = 0;
int MyString::GetCountObj(){ return CountObj ; }
MyString::MyString(){
++CountObj; //object +1
// cout<<"加11!"<<endl; //用于追踪程序。。。
len = 0 ;
p_str = new char[ 1 ] ;//为了与 delete对应必须也在此 new...
p_str[ 0 ] = " " ;
}
MyString::MyString(int n,char c){
++CountObj; //object +1
// cout<<"加22!"<<endl; //程序线程跟踪
len = n ;
p_str = new char[ len+1];
for( int i=0; i<len; ++i )
p_str[ i ] = c ;
p_str[ len ] = " " ; //此处不能再用 i 作为索引,因为 i 生命周期已过...
}
MyString::MyString(const char *str ){
++CountObj; //object +1
// cout<<"加33!"<<endl; //程序线程跟踪
len = strlen( str );
p_str = new char[ len+1 ];
strcpy( p_str, str );
}
MyString::MyString(const MyString&other){
++CountObj;
// cout<<"加44!"<<endl; //程序线程跟踪
len = other.len;
p_str = new char[ len+1 ];
strcpy( p_str, other.p_str ) ;
}
MyString::~MyString(){
--CountObj;
delete[] p_str;
// cout<<"减咯~!"<<endl; //程序线程跟踪
}
MyString & MyString::operator=(const MyString &myStr){
if( this->p_str == myStr.p_str )
return *this ;
if( p_str )
delete [] p_str;//必须先清空,先释放当前对象的堆内存
len = myStr.len;
p_str = new char[ len + 1 ] ;
strcpy( p_str , myStr.p_str ) ;
return *this;
}
MyString & MyString::operator=(const char* s ){
delete [] p_str;
len = strlen( s );
p_str = new char[ len+ 1 ] ;
strcpy( p_str, s );
return *this ;
}
char & MyString::operator[](int i ){
return p_str[ i ] ;
}
const char & MyString::operator[](int i ) const {
return p_str[ i ] ;
}
bool operator==(const MyString &str, const MyString &s ) {
return 0 == strcmp( str.p_str, s.p_str) ;
}
bool operator<(const MyString &str, const MyString &s){
return strcmp( str.p_str, s.p_str )<0 ;
}
bool operator>(const MyString &str, const MyString &s){
return strcmp( str.p_str, s.p_str )>0 ;
}
ostream & operator <<(ostream &os, const MyString &myStr){
os<<myStr.p_str;
return os ;
}
MyString & operator+( MyString & left, MyString & right ){
MyString *pStr = new MyString(" ") ;
pStr->p_str = new char[ strlen(left.p_str) + strlen(right.p_str) ] ;
strcpy( pStr->p_str, left.p_str ) ;
strcpy( pStr->p_str, right.p_str );
return *pStr ;
}
void fun1(int n){
MyString strArr[n];
MyString *pStr;
cout<<strArr[0].GetCountObj()<<endl;
}
void fun2(int n){
MyString *pStr=new MyString[n];
cout<<MyString::GetCountObj()<<endl;
}
void fun3(MyString &s1, MyString s2){
MyString s3;
s3=s1=s2;
cout<<MyString::GetCountObj()<<endl;
s3.ShowStr();
cout<<endl;
}
int main( ){
MyString s1;
MyString s2;
int n;
char charArr[20];
int op;
while (cin >> op && op){
switch (op){
case 1:{
cin>>n;
fun1(n);
break;
}
case 2:{
cin>>n;
fun2(n);
break;
}
case 3:{
int m;
char ch;
cin>>m>>ch;
s2=MyString(m,ch);
fun3(s1,s2);
s1.ShowStr();
cout<<endl;
break;
}
case 4:{
cin >> charArr;
s1=MyString(charArr);
cout<<MyString::GetCountObj()<<endl;
s1.ShowStr();
cout<<endl;
break;
}
}
}
return 0;
}
自己顺带练习的 运算符重载 ,待更。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
