none
第一次来这个论坛,还不知道怎么用。大家响应有多快? RRS feed

  • 问题

  • 还不知道MSDN这个社区怎么玩啊,先PO一个OJ的题目吧,希望大神快快回复!

    定义一个MyString类如下,该类有构造函数、拷贝构造函数、析构函数、成员函数和静态成员函数,有一个C字符串指针变量,还有一个静态成员变量numberOfObjects。请你完成类的实现。 

    class MyString

    {

    public:

        MyString(const char*);

        MyString(int = 0, char = '\0');  //initialize size_t number of char, terminated with '\0'

        MyString(const MyString&);           //copy constructor

        ~MyString();                                          //destructor

        const char* c_str();                   //return private char*

        static int getNumberOfObjects();

    private:

        char *str;

        static int numberOfObjects;             //count the number of MyString objects

    };

    请提交类的定义及其完整实现。

     

    输入
    输出
    ccccc
    Hello!
    1
    Hint

    请认真理解类的构造函数、重载、静态成员变量及其初始化RAII(资源获取就是初始化)以及newdelete的使用、深拷贝。测试数据中不会发生内存不够的现象。

    主函数形式如下: int main() {        MyString ms1(5, 'c');        cout << ms1.c_str() << endl;        MyString *ptr_ms2 = new MyString("Hello!");     {         MyString ms3(ms1);     }     cout << ptr_ms2->c_str() << endl;     delete ptr_ms2;     cout << MyString::getNumberOfObjects() << endl;         return 0; }

    2014年4月1日 15:27

答案

  • 你好,

    根据你的问题,大概可以这样实现供你参考。

    #include "stdafx.h"
    #include <iostream>
    #include <string.h>
    #include <cstring>
    using namespace std;
    
    class MyString
    {
    public:
    	MyString(const char*);      //constructor
    	MyString(const int &, const char);      
    	MyString(const MyString&);        //copy constructor
    	~MyString();                 //destructor
    	const char* c_str();         //conversion
    	static int getNumberOfObjects();        
    private:
    	char *str=NULL;
    	static int numberOfObjects;
    	
    };
    int MyString::numberOfObjects = 0;
    
    MyString::MyString(const char *c)
    {
    	str = new char[strlen(c) + 1];
    	strcpy(str, c);
    	numberOfObjects++;
    }
    MyString::MyString(const int &i, const char ch) :str(NULL)
    {
    	int j = i + 1;
    	str = new char[j];
    	memset(str, ch, strlen(str));
    	numberOfObjects++;
    	
    }
    MyString::MyString(const MyString &ms)
    {
    	++numberOfObjects;
    	str = new char[strlen(ms.str) + 1];
    }
    MyString::~MyString()
    {
    	--numberOfObjects;
    	delete[]str;
    }
    const char* MyString::c_str()
    {
    	char *c = new char[strlen(str) + 1];
    	strcpy(c, str);
    	return c;
    }
    int MyString::getNumberOfObjects()
    {
    	return numberOfObjects;
    }
    
    
    int main()
    {
    	MyString ms1(5, 'c');
    	cout << ms1.c_str() << endl;
    	MyString *ptr_ms2 = new MyString("Hello!");
    	{
    		MyString ms3(ms1);
    	}
    	cout << ptr_ms2->c_str() << endl;
    	delete ptr_ms2;
    	cout << MyString::getNumberOfObjects() << endl;
    	
    	getchar();
    	return 0;
    }
    May

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年4月2日 8:36

全部回复

  • 还不知道MSDN这个社区怎么玩啊,先PO一个OJ的题目吧,希望大神快快回复!

    定义一个MyString类如下,该类有构造函数、拷贝构造函数、析构函数、成员函数和静态成员函数,有一个C字符串指针变量,还有一个静态成员变量numberOfObjects。请你完成类的实现。 

    class MyString

    {

    public:

        MyString(const char*);

        MyString(int = 0, char = '\0');  //initialize size_t number of char, terminated with '\0'

        MyString(const MyString&);           //copy constructor

        ~MyString();                                          //destructor

        const char* c_str();                   //return private char*

        static int getNumberOfObjects();

    private:

        char *str;

        static int numberOfObjects;             //count the number of MyString objects

    };

    请提交类的定义及其完整实现。

     

    输入
    输出
    ccccc
    Hello!
    1
    Hint

    请认真理解类的构造函数、重载、静态成员变量及其初始化RAII(资源获取就是初始化)以及newdelete的使用、深拷贝。测试数据中不会发生内存不够的现象。

    主函数形式如下: int main() {        MyString ms1(5, 'c');        cout << ms1.c_str() << endl;        MyString *ptr_ms2 = new MyString("Hello!");     {         MyString ms3(ms1);     }     cout << ptr_ms2->c_str() << endl;     delete ptr_ms2;     cout << MyString::getNumberOfObjects() << endl;         return 0; }

    2014年4月1日 16:00
  • 你好,

    根据你的问题,大概可以这样实现供你参考。

    #include "stdafx.h"
    #include <iostream>
    #include <string.h>
    #include <cstring>
    using namespace std;
    
    class MyString
    {
    public:
    	MyString(const char*);      //constructor
    	MyString(const int &, const char);      
    	MyString(const MyString&);        //copy constructor
    	~MyString();                 //destructor
    	const char* c_str();         //conversion
    	static int getNumberOfObjects();        
    private:
    	char *str=NULL;
    	static int numberOfObjects;
    	
    };
    int MyString::numberOfObjects = 0;
    
    MyString::MyString(const char *c)
    {
    	str = new char[strlen(c) + 1];
    	strcpy(str, c);
    	numberOfObjects++;
    }
    MyString::MyString(const int &i, const char ch) :str(NULL)
    {
    	int j = i + 1;
    	str = new char[j];
    	memset(str, ch, strlen(str));
    	numberOfObjects++;
    	
    }
    MyString::MyString(const MyString &ms)
    {
    	++numberOfObjects;
    	str = new char[strlen(ms.str) + 1];
    }
    MyString::~MyString()
    {
    	--numberOfObjects;
    	delete[]str;
    }
    const char* MyString::c_str()
    {
    	char *c = new char[strlen(str) + 1];
    	strcpy(c, str);
    	return c;
    }
    int MyString::getNumberOfObjects()
    {
    	return numberOfObjects;
    }
    
    
    int main()
    {
    	MyString ms1(5, 'c');
    	cout << ms1.c_str() << endl;
    	MyString *ptr_ms2 = new MyString("Hello!");
    	{
    		MyString ms3(ms1);
    	}
    	cout << ptr_ms2->c_str() << endl;
    	delete ptr_ms2;
    	cout << MyString::getNumberOfObjects() << endl;
    	
    	getchar();
    	return 0;
    }
    May

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年4月2日 8:36