码迷,mamicode.com
首页 > 编程语言 > 详细

C++第七天笔记2016年02月24日(周三)A.M

时间:2016-02-25 22:57:42      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

1.    拷贝构造函数:也是构造函数。
2.    拷贝构造的作用:
(1). 用一个已经存在的对象创建并初始化新对象。
1 object=another_object; //赋值重载
2 Person object=another_object; //拷贝构造函数。
3 Person object(another_object);

 //开辟堆空间

1 Person obj;
2 Person* p=new Person(obj); //编辑器会调用拷贝构造函数。 delete p;

3.   构造函数

1 Person(Person _p)
2 {
3 cout<<”拷贝”<<endl;
4 }//会导致程序陷入死循环。
5 修改:
6 Person(const Person& _p)
7 {
8 }
4.    程序员在类中没有定义拷贝构造函数,那么此时编译器会提供一个拷贝
构造函数。但是编译器提供的拷贝构造提供的是浅拷贝。
5.    三大件:拷贝构造函数、赋值运算符重载函数、析构函数
如果类中有指向堆空间的指针变量,则类中需要实现big three。
6.   Vector(int*,int) 解析:
1 Vector(int*,int)  
2 int a[5]={}
3 Vector(a,5);

7. 源码:

  1 #include <iostream>
  2 #include "cstring"
  3 using namespace std;
  4 
  5 class Vector
  6 {
  7 public:
  8     Vector(int s=0);
  9     Vector(int*,int);
 10     Vector(const Vector& v);
 11     ~Vector(){dispose();}
 12     int get_size()const{return size;}
 13     Vector& operator=(const Vector& x);
 14     int & operator[](int index){return rep[index];}
 15     const int & operator[](int index)const{return rep[index];}
 16     friend ostream& operator<<(ostream& out,Vector& x);
 17     
 18 private:
 19     int* rep;
 20     int size;
 21     void clone(const Vector& a);
 22     void dispose();
 23 };
 24 void Vector::clone(const Vector& a)
 25 {
 26     (*this).size=a.size;
 27     rep=new int[a.get_size()+1];
 28     for (int i=0; i<size; i++) {
 29         rep[i]=a[i];
 30     }
 31 }
 32 void Vector::dispose()
 33 {
 34     delete [] rep;
 35 }
 36 Vector::Vector(int s):size(s)
 37 {
 38     if (size<=0) {
 39         rep=NULL;
 40     }
 41     else{
 42         rep=new int [size];
 43         for (int i=0; i<size; i++) {
 44             rep[i]=0;
 45         }
 46     }
 47 }
 48 Vector::Vector(int* a,int s):rep(new int[s]),size(s)
 49 {
 50     for (int i=0; i<size; ++i) {
 51         rep[i]=a[i];
 52     }
 53 }
 54 Vector::Vector (const Vector& v)
 55 {
 56     clone(v);
 57 }
 58 Vector& Vector::operator=(const Vector& x)
 59 {
 60     if (this!=&x) {
 61         delete [] rep;
 62         this->size=x.size;
 63         rep=new int[size];
 64         for (int i=0; i<size; ++i) {
 65             rep[i]=x[i];
 66         }
 67     }
 68     return *this;
 69 }
 70 ostream& operator<<(ostream& out,Vector& x)
 71 {
 72     int s=x.get_size();
 73     for (int i=0; i<s; i++) {
 74         out<<x[i]<<" ";
 75     }
 76     out<<endl;
 77     return out;
 78 }
 79 bool operator==(const Vector& a,const Vector & b)
 80 {
 81     bool yes=true;
 82     if (a.get_size()!=b.get_size()) {
 83         yes=false;
 84     }
 85     else
 86     {
 87         int s,index=0;
 88         s=a.get_size();
 89         while (index<s&&a[index]==b[index]) {
 90             ++index;
 91         }
 92         if (index<s) {
 93             yes=false;
 94         }
 95     }
 96     return yes;
 97 }
 98 int main(int argc, const char * argv[]){
 99     int a[5]={3,2,4,2,8};
100     int b[3]={2,4,3};
101     Vector v1(a,5);
102     cout<<v1;
103     Vector v2(b,3);
104     cout<<v2;
105     if (v1==v2) {
106         cout<<"v1,v2不等!";
107     }
108     else
109         cout<<"v1,v2相等";
110     cout<<endl;
111     Vector v3=v2;
112     cout<<v3;
113     return 0;
114 }

 

C++第七天笔记2016年02月24日(周三)A.M

标签:

原文地址:http://www.cnblogs.com/cai1432452416/p/5218514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!