第六周项目:深复制体验
2015-04-19 10:34
211 查看
代码:
运行结果:
为类A增加复制构造函数
代码:
运行结果:
#include<iostream> #include<cstring> using namespace std; class A { private: char *a; public: A(char *aa) { a = new char[strlen(aa)+1]; strcpy(a, aa); } ~A() { delete []a; } void output() { cout<<a<<endl; } }; int main(){ A a("good morning, code monkeys!"); a.output(); A b("good afternoon, codes!"); b.output(); return 0; }
运行结果:
为类A增加复制构造函数
代码:
#include<iostream> #include<cstring> using namespace std; class A { private: char *a; public: A(char *aa) { a = new char[strlen(aa)+1]; strcpy(a,aa); } ~A() { delete []a; } A(A &b) { a = new char[strlen(b.a)+1]; strcpy(a,b.a); } void output() { cout<<a<<endl; } }; int main() { A a("good morning,code monkeys!"); a.output(); A b(a); b.output(); return 0; }
运行结果:
相关文章推荐
- 第六周 项目一-深复制体验
- 第六周项目一—深复制体验(2)
- 第六周项目一 深复制体验
- 第六周项目1:深复制体验
- 第六周 项目一 深复制体验(1)
- 第六周项目一深复制体验(1)
- 第六周 项目一-深复制体验(2)
- 第六周 项目一 深复制体验3
- 第六周项目一 深复制体验(2)
- 第六周项目一深复制体验(2)
- 第六周 项目一-深复制体验(1)
- 第六周项目1-深复制体验(3-复制构造函数)
- 第六周 项目1 深复制体验
- 第六周项目一深复制体验(3)
- 第六周项目一 深复制体验(3)
- 第六周——项目一深复制体验(一)
- 第六周 项目一-深复制体验(3)
- 第六周 项目一--深复制体验(1)
- 第六周项目一 (1)(2) 深复制体验
- 第六周项目一 深复制体验1