代码拉取完成,页面将自动刷新
#include <iostream>
using namespace std;
//interface
class Useless
{
private:
int n;
char *pc;
static int ct;
void ShowObject() const;
public:
Useless();
explicit Useless(int k);
Useless(int k, char ch);
Useless(const Useless & f);
Useless(Useless && f);
~Useless();
Useless operator+(const Useless & f) const;
void ShowData() const;
};
//implementation
int Useless::ct = 0;
Useless::Useless()
{
++ct;
n = 0;
pc = nullptr;
cout << "default constructor called; number of objects: " << ct << endl;
ShowObject();
}
Useless::Useless(int k) : n(k)
{
++ct;
cout << "int constructor called; number of objects: " << ct << endl;
pc = new char[n];
ShowObject();
}
Useless::Useless(int k, char ch) : n(k)
{
++ct;
cout << "int, char constructor called; Number of objects: " << ct << endl;
pc = new char[n];
for(int i = 0; i < n; i++)
pc[i] = ch;
ShowObject();
}
Useless::Useless(const Useless & f) : n(f.n)
{
++ct;
cout << "copy const called; number of objects: " << ct << endl;
pc = new char[n];
for(int i = 0; i < n; i++)
pc[i] = f.pc[i];
ShowObject();
}
Useless::Useless(Useless && f) : n(f. n)
{
++ct;
cout << "move constructor called; number of objects: " << ct << endl;
pc = f.pc;
f.pc = nullptr;
f.n = 0;
ShowObject();
}
Useless::~Useless()
{
cout << "destructor called; objects left: " << --ct << endl;
cout << "deleted object: \n";
ShowObject();
delete [] pc;
}
Useless Useless::operator+(const Useless & f) const
{
cout << "Enter operator+()\n";
Useless temp = Useless(n + f.n);
for(int i = 0; i < n; i++)
temp.pc[i] = pc[i];
for(int i = n; i< temp.n; i++)
temp.pc[i] = f.pc[i - n];
cout << "temp object: \n";
cout << "Leaving operator+()\n";
return temp;
}
void Useless::ShowObject() const
{
cout << "Number of elements: " << n << " Data address: " << (void *) pc << endl;
}
void Useless::ShowData() const
{
if(n == 0)
cout << "(Object empty)";
else
for(int i = 0; i < n; i++)
cout << pc[i];
cout << endl;
}
//application
int main()
{
{
Useless one(10, 'x');
Useless two = one;
Useless three(20, 'o');
Useless four (one + three);
cout << "Object one: ";
one.ShowData();
cout << "Object two: ";
two.ShowData();
cout << "Object three: ";
three.ShowData();
cout << "Object four: ";
four.ShowData();
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。