c++知识学习 (3)

拷贝构造函数

一个类对象向该类的另外一个对象作拷贝是通过依次拷贝每个非静态数据成员来实现的。

只有在创建新的对象的时候才会调用拷贝构造函数

设计者也可以通过提供特殊的拷贝构造函数来改变缺省的行为

Person(const Person & person);

依次Person.h,Person.cpp,main.cpp

 1 //
 2 //  Person.h
 3 //  ArrayTest
 4 //
 5 //  Created by 张学院 on 14-1-8.
 6 //  Copyright (c) 2014年 com.mix. All rights reserved.
 7 //
 8 
 9 //防止重复引用
10 #ifndef __ArrayTest__Person__
11 #define __ArrayTest__Person__
12 
13 #include <iostream>
14 using namespace std;
15 class Person{
16 //---------成员变量--------
17 public :
18     
19     int age;
20     
21 private :
22     int weight;
23     char  * name;
24     char sex;
25 //---------成员方法--------
26 public:
27     //----构造函数-------
28     Person();
29     //-----构造函数重载-----
30     Person(int age);
31     
32     //------拷贝构造函数-------
33     Person(const Person & per);
34     
35     //----------操作符重载----------
36     Person & operator=(const Person &right);
37     
38     void setWeight(int weight);
39     int getWeight() const;
40     //char * getName() const;
41     //const 指针,保证指针不被修改
42     const char * getName() const;
43     void setName(const char * name);
44     void info() const;
45     ~Person();
46 };
47 #endif /* defined(__ArrayTest__Person__) */

 

  1 //
  2 //  Person.cpp
  3 //  ArrayTest
  4 //
  5 //  Created by 张学院 on 14-1-8.
  6 //  Copyright (c) 2014年 com.mix. All rights reserved.
  7 //
  8 
  9 #include "Person.h"
 10 //------方法实现格式------
 11 //----返回值 类::方法名 (参数)------
 12 void Person::setWeight(int weight){
 13     //-----this 代表
 14     this->weight=weight;
 15 }
 16 //--------const 编译限制-------
 17 int  Person::getWeight() const{
 18     //weight++;报错
 19     return weight;
 20 }
 21 const char * Person::getName() const{
 22     
 23     return name;
 24     
 25 }
 26 void Person::setName(const char * name){
 27 
 28     strcpy(this->name, name);
 29     
 30 }
 31 void Person::info() const{
 32 
 33     printf("%s\n%d\n%c\n%d\n",name,age,sex,weight);
 34 }
 35 //--------构造函数:初始化成员变量------------
 36 Person::Person(){
 37     printf("call the functon Person()\n");
 38     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
 39     //strcpy(name, "a man");
 40     //在堆里分配内存,返回首地址,在堆里面分配的空间一定要记得释放内存!!!!!
 41     name = new char[255];
 42     strcpy(name, "a man");
 43     weight=60;
 44     age=20;
 45     sex='m';
 46 }
 47 //--------构造函数:重载------------
 48 Person::Person(int age){
 49     printf("call the functon Person(int age)\n");
 50     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
 51     //strcpy(name, "a man");
 52     name = new char[255];
 53     strcpy(name, "a man");
 54     weight=60;
 55     this->age=age;
 56     sex='m';
 57     
 58 }
 59 
 60 //------拷贝构造函数-------
 61 Person::Person(const Person & person){
 62 /* 自己不实现的拷贝构造函数的话,系统生成的拷贝构造函数
 63     this->name=person.name;
 64     this->age=person.age;
 65     this->weight=person.weight;
 66     this->sex=person.sex;
 67 */
 68  // 自己实现的拷贝构造函数的话
 69     //重新分配内存
 70     this->name= new char[255];
 71     strcpy(this->name, person.name);
 72     this->age=person.age;
 73     this->weight=person.weight;
 74     this->sex=person.sex;
 75     
 76 }
 77 
 78 //-----------析构函数---------
 79 Person::~Person(){
 80     //在析构函数里面释放内存
 81     printf("per.name is %s call the functon ~Person()析构函数 \n",name);
 82     delete [] name;
 83    
 84 }
 85 
 86 //----------操作符重载----------
 87 Person & Person::operator=(const Person &right){
 88     /* 自己不实现的操作符重载的话,系统生成的拷贝构造函数
 89     
 90     this->name=right.name;
 91     this->age=right.age;
 92     this->sex=right.sex;
 93     this->weight=right.weight;
 94       */
 95     //---------不需要再次分配内存-------
 96    // this->name= new char[255];
 97     strcpy(this->name, right.name);
 98     this->age=right.age;
 99     this->sex=right.sex;
100     this->weight=right.weight;
101     return *this;
102 }

 

 1 //
 2 //  main.cpp
 3 //  ArrayTest
 4 //
 5 //  Created by 张学院 on 14-1-6.
 6 //  Copyright (c) 2014年 com.mix. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 
11 #include <string>
12 #include "Person.h";
13 int main()
14 {
15     //---栈分配:函数结束,per出栈,内存释放之前,调用析构函数-------
16     Person per= Person();
17     per.setName("a woman");
18     
19    
20     //------如果自己调用系统默认的拷贝构造函数-----------
21     //-----用一个对象去初始化另外一个对象--------------
22     Person per1=per;//per.name new char[255] ,per1.name 也指向这一块内存。per析构的已经释放了name。per1调用析构的时候就报错了。
23     //-------所以需要copy构造函数------------------
24     
25     //-------Person per2 = per效果一样------------
26     Person per2(per);
27   
28     Person per3=Person();
29     Person per4=Person();
30     
31     //------------不会调用拷贝构造函数--------
32    // per3=per4;
33     
34     //-----拷贝构造函数只会在创建一个新的-------
35     //----------操作符重载----------
36     per3=per4;
37     per3.info();
38 }

输出:

call the functon Person()

call the functon Person()

call the functon Person()

a man

20

m

60

per.name is a man call the functon ~Person()析构函数 

per.name is a man call the functon ~Person()析构函数 

per.name is a woman call the functon ~Person()析构函数 

per.name is a woman call the functon ~Person()析构函数 

per.name is a woman call the functon ~Person()析构函数 

 

 

posted @ 2014-01-09 15:36  aosting  阅读(147)  评论(0编辑  收藏  举报