Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // =oper.cpp : Defines the entry point for the console application.
- //
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- using namespace std;
- int i = 0;
- char names[][7] = {
- "object",
- "str",
- "op_r 1",
- "op_r 2",
- "op_r 3"
- };
- class sample {
- char *s;
- int name;
- public:
- sample(int i) { s = 0; name = i; cout << "Usual constructor of #" << names[name] << "\n"; }
- sample(const sample &obj);
- ~sample() {
- if (s)
- {
- delete[] s;
- cout << "Destructor of #" << names[name] << "\n";
- }
- }
- void show() { cout << s << "\n"; }
- void set(char *str);
- sample operator=(sample op_r);
- };
- sample::sample(const sample &obj)
- {
- s = new char[strlen(obj.s) + 1];
- strcpy(s, obj.s);
- name = i++;
- cout << "Constructor of copy #" << names[name] << "\n";
- }
- void sample::set(char *str)
- {
- s = new char[strlen(str) + 1];
- strcpy(s, str);
- }
- sample input()
- {
- char instr[80];
- sample str(i++);
- cout << "Enter the word: ";
- cin >> instr;
- str.set(instr);
- return str;
- }
- sample sample::operator=(sample op_r)
- {
- s = new char[strlen(op_r.s) + 1];
- strcpy(s, op_r.s);
- return *this;
- }
- int main()
- {
- sample object(i++);
- object = input();
- object.show();
- //system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement