第1题:请写出下面这个类的方法代码
#include<iostream>
#include<string>
using namespace std;
#pragma warning (disable:4996)
//第1题:
class String
{
public:
String( char *pstr)
{
cout << "String::String()" << endl;
if (pstr != NULL)
{
_pstr = new char[strlen(pstr) + 1];
strcpy(_pstr, pstr);
}
else
{
_pstr = new char[1]();
}
}
~String() //析构函数
{
cout << "String::~String()" << endl;
delete[] _pstr;
_pstr = NULL;
}
String(const String &src)
{
cout << "String::String(const String &src)" << endl;
_pstr = new char[strlen(src._pstr) + 1];
strcpy(_pstr, src._pstr);
}
void Show()
{
cout << _pstr << endl;
}
void operator=(const String &src)
{
if (this != &src)
{
delete _pstr;
_pstr = new char[strlen(src._pstr) + 1];
strcpy(_pstr, src._pstr);
}
}
private:
char *_pstr;
};
第2题:请完成下面这个类的方法代码,请实现带头结点的单链表
class Link
{
public:
Link() //构造函数
{
_phead = new Node();
}
~Link() //析构函数
{
Node* pCur = _phead;
Node* pNext = pCur;
while (pCur != NULL)
{
pNext = pCur->_pnext; //pNext指向剩下的链表
delete pCur; //释放当前节点
pCur = pNext;
}
_phead = NULL;
}
void InsertHead(int val)
{
Node* pnewnode = new Node(val);
pnewnode->_pnext = _phead->_pnext;
_phead->_pnext = pnewnode;
}
void InsertTail(int val)
{
Node* pCur = _phead;
for (; pCur->_pnext != NULL; pCur = pCur->_pnext)
{
}
Node* pnewnode = new Node(val); //指针域已经置空
pCur->_pnext = pnewnode;
}
void DeleteNode(int val) //删除值为val的结点
{
Node* pCur = _phead;
while (pCur->_pnext != NULL)
{
if (pCur->_pnext->_data == val)
{
Node * p = pCur->_pnext;
pCur->_pnext = p->_pnext;
delete p;
}
if (pCur->_pnext->_data != val)
{
pCur = pCur->_pnext;
}
}
}
void ShowLink()
{
for (Node* pCur = _phead->_pnext; pCur != NULL; pCur = pCur->_pnext)
{
cout << pCur->_data << " ";
}
cout << endl;
}
private:
class Node
{
Node(int data = 0) :_data(data), _pnext(NULL) {}
int _data;//数据域
Node* _pnext;//指针域
friend class Link;
};
Node* _phead; //头指针
};
第3题:请给出下面对象创建过程中涉及的方法打印
class Test
{
public:
Test(int a = 5, int b = 5) :ma(a), mb(b)
{
cout << "Test(int, int)" << endl;
}
~Test()
{
cout << "~Test()" << endl;
}
Test(const Test &src) :ma(src.ma), mb(src.mb)
{
cout << "Test(const Test&)" << endl;
}
void operator=(const Test &src)
{
ma = src.ma; mb = src.mb; cout << "operator=" << endl;
}
private:
int ma;
int mb;
};
Test t1(10, 10);
int main()
{
Test t2(20, 20);
Test t3 = t2;
static Test t4 = Test(30, 30);
t2 = Test(40, 40);
t2 = (Test)(50, 50);
t2 = 60;
Test *p1 = new Test(70, 70);
Test *p2 = new Test[2];
Test *p3 = &Test(80, 80);
Test &p4 = Test(90, 90);
delete p1;
delete[]p2;
return 0;
}
Test t5(100, 100);
序号 | 输出结果 | 对应指令 | 生成对象性质 | 生存周期 | 解释说明 |
1 | Test(int, int) | Test t1(10, 10); | 全局对象t1 | 程序结束前 | t1的构造函数 |
2 | Test(int, int) | Test t5(100, 100); | 全局对象t5 | 程序结束前 | t5的构造函数 |
3 | Test(int, int) | Test t2(20, 20); | 局部对象t2 | 对象所在函数结束后 | t2的构造函数 |
4 | Test(const Test&) | Test t3 = t2; | 局部对象t3 | 对象所在函数结束后 | t3=t2时的拷贝构造函数 |
5 | Test(int, int) | staticTest t4 = Test(30, 30); | 静态局部对象t4 | 程序结束前 | t4的构造函数 |
6 | Test(int, int) | t2 =Test(40, 40); | 临时对象1 | 当前语句结束 | 临时对象1的构造函数 |
operator= | 赋值运算符重载 | ||||
~Test() | 临时对象1的析构函数 | ||||
7 | Test(int, int) | t2 = (Test)(50, 50); | 临时对象2 | 当前语句结束 | 临时对象2的构造函数 |
operator= | 赋值运算符重载 | ||||
~Test() | 临时对象2的析构函数 | ||||
8 | Test(int, int) | t2 = 60; | 临时对象3(隐式) | 当前语句结束 | 临时对象3的构造函数 |
operator= | 赋值运算符重载 | ||||
~Test() | 临时对象3的析构函数 | ||||
9 | Test(int, int) | Test *p1 = newTest(70, 70); | p1指向的局部对象(堆) | 对象所在函数结束前(手动) | p1指向对象的构造函数 |
10 | Test(int, int) | Test *p2 = newTest[2]; | p2指向的局部对象数组(堆) | 对象所在函数结束前(手动) | p2指向对象数组[0]的构造函数 |
11 | Test(int, int) | 对象所在函数结束前(手动) | p2指向对象数组[1]的构造函数 | ||
12 | Test(int, int)
| Test *p3 = &Test(80, 80); | p3指向的临时对象(栈) | 当前语句结束 | p3指向对象的构造函数 |
~Test()
| p3指向对象的析构函数 | ||||
13 | Test(int, int)
| Test &p4 = Test(90, 90); | p4引用的临时对象(栈) | 对象所在函数结束后 | p4引用的临时对象(栈)构造函数 |
| ~Test()
| delete p1; |
|
| p1指向对象的构造函数 |
| ~Test()
| delete[]p2; |
|
| p2指向对象数组[0]的析构函数 |
| ~Test()
|
|
| p2指向对象数组[1]的析构函数 | |
| ~Test()
|
|
|
| p4引用的临时对象(栈)构造函数 |
| ~Test()
|
|
|
| t3的析构函数 |
| ~Test()
|
|
|
| t2的析构函数 |
| ~Test()
|
|
|
| t4的析构函数 |
| ~Test()
|
|
|
| t5的析构函数 |
| ~Test()
|
|
|
| t1的析构函数 |