美文网首页上嵌学习笔记
C++基础-(堆与拷贝构造函数)

C++基础-(堆与拷贝构造函数)

作者: I踏雪寻梅 | 来源:发表于2016-11-10 21:21 被阅读14次

link

  • C++对malloc的升级
#include <iostream>
#include <stdlib.h>
using namespace std;
class test
{
    public:
        test(int a)
        {
            cout<<"hello world"<<a<<endl;
        }
        test()
        {
            cout<<"hello world"<<endl;
        }
        ~test()
        {
            cout<<"析构测试"<<endl;
        }
};
int main()
{
    //test *t=(test *)malloc(sizeof(test));
    test *t=new test;//new test(100);
    cout<<"+++++++++"<<endl;
    delete t;
    //free(p);
    /*
    test *t=new test[5];
    delete[] t;
    */
}
  • 使用堆空间的原因
    • 直到运行时才知道需要多少空间
    • 不知道对象的生存期到底有多长
    • 直到运行时才知道一个对象需要多少内存空间

拷贝构造函数

#include <iostream>
using namespace std;
class test 
{
    int m_a;
    public:
        test (int a)
        {
            m_a=a;
            cout<<"test construct"<<m_a<<endl;
        }
        ~test()
        {
            cout<<"析构测试"<<endl;
        }
/*      test(const test &q)//test(test &q)
        {
            m_a=q.m_a;
        }*///默认构造函数只能完成浅拷贝,将形参传给自身
        void show()
        {
            cout<<"m_a="<<m_a<<endl;
        }
};
void hello (test temp)
{
    temp.show();
}//test temp(t1);
int main()
{   
    test t1(10);
    t1.show();
    //test t2(t1);
    //t2.show();
    hello(t1);
}

如果成员为指针类型,会造成系统浅拷贝

#include <iostream>
using namespace std;
class test
{
    int *m_a;
    public:
        test(int a)
        {
            m_a=new int;
            *m_a=a;
            cout<<"test construct"<<*m_a<<endl;
        }
        ~test()
        {
            cout<<"析构测试"<<endl;
            delete m_a;
        }
        void show()
        {
            cout<<"m_a="<<*m_a<<endl;
        }
        test(const test &q)
        {
            m_a=new int;
            *m_a=*q.m_a;
        }//先分配空间再拷贝
};
int main()
{
    test t1(10);
    t1.show();
    test t2(t1);
    t2.show();
}//浅拷贝:创建t2时,t1被复制给了t
2,但是资源未被复制,因此t1,t2指向同一个资源。
  • 为什么拷贝构造函数的传参都是引用
    • 传值的方式会调用该类的拷贝构造函数,从而造成无穷递归地调用拷贝构 造函数。因此拷贝构造函数的参数必须是一个引用
  • 仿写 string 类 ,取名 MyString
    1. 完成4种构造函数

      1. MyString s;
      2. MyString s("weiwei");
      3. MyString s1(5,'c');
      4. MyString s(s1);
      5. ~MyString() 要释放空间
      if( NULL != m_str)
      {
          delete[]m_p;//指针释放完空间,必须指向NULL
          m_str = NULL;
      }
      
    2. 完成 size() / bool empty() 成员函数

    3. String类中 只能有两个成员变量 char *m_str和int m_nLength

#include <iostream>
#include <string.h>
using namespace std;
class myString
{
    char *m_str;
    int m_length;
    public: 
        myString();     
        myString(const char *ch);
        myString(unsigned int length,const char &ch);
        myString(const myString &s);
        int size()
        {
            return m_length;
        }
        bool empty()
        {
            return m_length==0;
        }
        ~myString()
        {
            if( NULL != m_str)
                delete []m_str;
            m_str=NULL;
        }
        void show()
        {
            cout<<m_str<<endl;
        }
};
myString ::myString()
{
    m_str=new char[1];
    m_str=0;
    m_length=0;
}
myString ::myString(const char *ch)
{
    if(ch==NULL)
    {
        m_str=new char[1];
        m_str=0;
        m_length=0;
    }
    else
    {
        m_str=new char[sizeof(ch)+1];
        strcpy(m_str,ch);
        m_length=strlen(ch);
    }
}
myString ::myString(unsigned int length,const char &ch)
{
    int i;
    m_length=length;
    m_str=new char[length+1];
    for(i=0;i<length;i++)
    {
        m_str[i]=ch;
    }
    m_str[length]='\0';
}
myString ::myString(const myString &s)
{
    m_str=new char[sizeof(s.m_str)+1];
    strcpy(m_str,s.m_str);
    m_length=strlen(s.m_str);
}
int main()
{
    myString s(5,'c');
    myString s1(s);
    s.show();
    s1.show();
    cout<<s.size()<<endl;
    cout<<(s.empty()?"空":"不空")<<endl;
}
  • josephus问题
#include <iostream>
#include<stdlib.h>
using namespace std;
typedef struct Node
{
    int data;
    struct Node *next;
}Node,*List;
class Ring//环链表
{
private:
    List First;
    int Count;
    int Current;
    //List Begin;
public:
    void Print();
    void Clear(){};
    void Creatlist(int n,int m,int z);
    void Pivot();
};
void Ring::Creatlist(int n,int m,int z)
{
    List p;
    int i;
    First=(Node*)malloc(sizeof(Node));
    if(!First)
    {
        cout<<"memory allocation error!\n";
        return ;
    }
    First->data=1; First->next=First;
    for(i=n;i>1;--i)
    {
        p=(Node*)malloc(sizeof(Node));
        if(!p)
        {
            cout<<"memory allocation error!\n";
            return;
        }
        p->data=i; p->next=First->next; First->next=p;
    }
    Count = n;
    Current = m;
    if(z<1)return;
    for(int j = 0;j<z;j++)
    {
        First=First->next;
    }bu
}
void Ring::Print()
{
    List p=First;
    do
    {
        cout<<p->data<<" ";
        p=p->next;
    }while(p!=First);
    cout<<endl;
}
void Ring::Pivot()
{
    Node* p=First,*pre=NULL;
    int i,j;
    for(i=0;i<Count-1;++i)
    {
        for(j=1;j<Current;++j)
        {
            pre=p;
            p=p->next;
        }
        cout<<"出列的人是"<<p->data<<endl;
        pre->next=p->next; free(p);
        p=pre->next;
    }
    cout<<"The winner is "<<p->data<<endl;
}
class Josephus
{
private:
    int Boynumber;//孩子数量
    int Beginpos;//开始位置
    int Inteval;//间隔
    Ring Head;
public:
    void Inital();//初始化
    void Getwinner();//获得胜利者
    void Output();
};
void Josephus::Inital()
{
    cout<<"Input the Boynumber , Inteval and Beginpos:";
    cin>>Boynumber>>Inteval>>Beginpos;
    if(Inteval < 2)Inteval = 2;
    Head.Creatlist(Boynumber,Inteval,Beginpos);
}
void Josephus::Getwinner()
{
    Head.Pivot();
}
void Josephus::Output()
{
    Head.Print();
}
int main()
{
Josephus jose;
jose.Inital();
jose.Output();
jose.Getwinner();
return 0;
}

相关文章

网友评论

    本文标题:C++基础-(堆与拷贝构造函数)

    本文链接:https://www.haomeiwen.com/subject/oyixpttx.html