C++

C++ - 构造函数与析构函数

2016-01-13  本文已影响183人  Mitchell

参考链接

一、构造函数

#include<iostream>
class Student{
  public:
  Student(){//无参数构造函数
  number = 1;
  score = 100;
  }
void show();
protected:
int number;
int score;
};
void Student::show(){
  cout<<number<<endl<<score<<endl;
}
void main()
{
  Student a;
  a.show();
  cin.get();
}
#include<iostream>
using namespace std;
class Teacher
{
    public:
    Teacher(char * input_name)//有参数的构造函数
    {
        name = new char[10];
        //name = input_name;//这样赋值是错误的
        strcpy(name,input_name);
    }
    void show();
protected:
char *name;
}
void Teacher::show()
{
    cout<<name<<endl;
}
void main()
{
    //Teacher a;//这里是错误的,因为没有无参数的构造函数
    Teacher a("test");
    a.show();
    cin.get();
}
#include <iostream>  
using namespace std;    
class Teacher  
{  
    public:  
    Teacher(char *input_name)  
    {  
        name=new char[10];  
        //name=input_name;//这样赋值是错误的  
        strcpy(name,input_name);  
    }  
    Teacher()//无参数构造函数,进行函数重载  
    {  
      
    }  
    void show();  
  
    protected:  
    char *name;  
  
};  
void Teacher::show()  
{  
    cout<<name<<endl;  
}  
void main()  
{  
    Teacher test;  
    Teacher a("test");  
    a.show();  
    cin.get();  
}
#include <iostream>  
using namespace std;    
class Teacher  
{  
    public:  
    Teacher()  
    {  
        director = new char[10];  
        strcpy(director,"王大力");  
    }  
    char * show();  
    protected:  
    char * director;  
};  
char *Teacher::show()  
{  
    return director;  
}  
class Student  
{  
    public:  
    Student()  
    {  
        number = 1;  
        score = 100;  
    }  
    void show();  
    protected:  
    int number;  
    int score;  
    Teacher teacher;//这个类的成员teacher是用Teacher类进行创建并初始化的  
};  
void Student::show()  
{  
    cout<<teacher.show()<<endl<<number<<endl<<score<<endl;  
}  
void main()  
{  
    Student a;  
    a.show();  
    Student b[3];  
    for(int i=0; i<sizeof(b)/sizeof(Student); i++)  
    {  
        b[i].show();  
    }  
    cin.get();  
}

二、析构函数

#include <iostream>  
#include <string>  
using namespace std;    
class Teacher  
{  
    public:  
    Teacher()  
    {  
        director = new char[10];  
        strcpy(director,"王大力");  
        //director = new string;  
        // *director="王大力";//string情况赋值  
    }  
    ~Teacher()  
    {  
        cout<<"释放堆区director内存空间1次";  
        delete[] director;  
        cin.get();  
    }  
    char *show();  
    protected:  
    char *director;  
    //string *director;  
};  
char *Teacher::show()  
{  
    return director;  
}  
class Student  
{  
    public:  
    Student()  
    {  
        number = 1;  
        score = 100;  
    }  
    void show();  
    protected:  
    int number;  
    int score;  
    Teacher teacher;  
};  
void Student::show()  
{  
    cout<<teacher.show()<<endl<<number<<endl<<score<<endl;  
}  
void main()  
{  
    Student a;  
    a.show();  
    Student b[3];  
    for(int i=0; i<sizeof(b)/sizeof(Student); i++)  
    {  
        b[i].show();  
    }  
    cin.get();  
}
#include <iostream>    
#include <string>    
using namespace std;      
class Teacher    
{    
    public:    
    Teacher(char *temp)    
    {    
        director = new char[10];    
        strcpy(director,temp);  
    }  
    ~Teacher()    
    {    
        cout<<"释放堆区director内存空间1次";    
        delete[] director;    
        cin.get();    
    }    
    char *show();    
    protected:    
    char *director;    
};    
char *Teacher::show()    
{    
    return director;    
}    
class Student    
{    
    public:    
    Student()    
    {    
        number = 1;    
        score = 100;    
    }    
    void show();    
    protected:    
    int number;    
    int score;    
    Teacher teacher("王大力");//错误,一个类的成员如果是另外一个类的对象的话,不能在类中使用带参数的构造函数进行初始化      
};    
void Student::show()    
{    
    cout<<teacher.show()<<endl<<number<<endl<<score<<endl;    
}    
void main()    
{    
    Student a;    
    a.show();    
    Student b[3];    
    for(int i=0; i<sizeof(b)/sizeof(Student); i++)    
    {    
        b[i].show();    
    }    
    cin.get();    
}

这样不行,程序不能够编译成功,
因为:类是一个抽象的概念,并不是一个实体,并不能包含属性值,只有对象才占有一定的内存空间,含有明确的属性值。

#include <iostream>    
using namespace std;      
class Teacher    
{    
    public:    
    Teacher(char *temp)    
    {    
        director = new char[10];    
        strcpy(director,temp);    
    }  
    ~Teacher()    
    {    
        cout<<"释放堆区director内存空间1次";    
        delete[] director;    
        cin.get();    
    }    
    char *show();    
    protected:    
    char *director;    
};    
char *Teacher::show()    
{    
    return director;    
}    
class Student    
{    
    public:    
    Student(char *temp):teacher(temp)  
    {    
        number = 1;    
        score = 100;    
    }    
    void show();    
    protected:    
    int number;    
    int score;    
    Teacher teacher;    
};    
void Student::show()    
{    
    cout<<teacher.show()<<endl<<number<<endl<<score<<endl;    
}    
void main()    
{    
    Student a("王大力");    
    a.show();    
    //Student b[3]("王大力");  //这里这么用是不对的,数组不能够使用带参数的构造函数,以后我们将详细介绍vector类型  
    // for(int i=0; i<sizeof(b)/sizeof(Student); i++)    
    //{    
    //    b[i].show();    
    //}    
    cin.get();    
}
Student(char *temp):teacher(temp) 

冒号后的 teacher 就是要告诉调用 Student 类的构造函数的时候把参数传递给成员 teacher 的 Teacher 类的构造函数,这样一来我们就成功的在类体外对 teacher 成员进行了初始化,既方便也搞笑,这种冒号后制定调用某成员构造函数的方式,可以同时制定多个成员,这一特性使用都好方式,例如:

Student(char *temp):teacher(temp),abc(temp),def(temp) 

修改一下代码:

#include <iostream>    
#include <string>    
using namespace std;      
class Teacher    
{    
    public:    
    Teacher(char *temp)    
    {    
        director = new char[10];    
        strcpy(director,temp);    
    }  
    ~Teacher()    
    {    
        cout<<"释放堆区director内存空间1次";    
        delete[] director;    
        cin.get();  
    }    
    char *show();    
    protected:    
    char *director;    
};    
char *Teacher::show()    
{    
    return director;    
}    
class Student    
{    
    public:    
    Student(char *temp,int &pk):teacher(temp),pk(pk),ps(10)  
    {    
        number = 1;    
        score = 100;  
    }    
    void show();    
    protected:    
    int number;    
    int score;    
    Teacher teacher;  
    int &pk;  
    const int ps;  
};    
void Student::show()    
{    
    cout<<teacher.show()<<endl<<number<<endl<<score<<endl<<pk<<endl<<ps<<endl;    
}    
void main()    
{    
    char *t_name="王大力";  
    int b=99;  
    Student a(t_name,b);  
    a.show();  
    cin.get();  
}
Student a(t_name,b); 
#include <iostream>    
#include <string>    
using namespace std;      
class Test  
{  
public:  
    Test(int a)  
    {  
        kk=a;  
        cout<<"构造参数a:"<<a<<endl;  
    }  
public:  
    int kk;  
};  
void fun_t(int n)  
{  
    static Test a(n);  
    //static Test a=n;//这么写也是对的  
    cout<<"函数传入参数n:"<<n<<endl;  
    cout<<"对象a的属性kk的值:"<<a.kk<<endl;  
}  
Test m(100);  
void main()  
{  
    fun_t(20);  
    fun_t(30);  
    cin.get();  
}
#include <iostream>    
#include <string>    
using namespace std;      
class Test  
{  
public:  
    Test(int a)  
    {  
        kk=a;  
        cout<<"构造参数a:"<<a<<endl;  
    }  
public:  
    int kk;  
};  
void fun_t(int n)  
{  
    static Test a(n);  
    //static Test a=n;//这么写也是对的  
    cout<<"函数传入参数n:"<<n<<endl;  
    cout<<"对象a的属性kk的值:"<<a.kk<<endl;  
}  
Test m(100);  
void main()  
{  
    fun_t(20);  
    fun_t(30);  
    cin.get();  
}
#include <iostream>    
using namespace std;      
  
class Test  
{  
public:  
    Test(int j):pb(j),pa(pb+5)  
    {  
          
    }  
public:  
    int pa;  
    int pb;  
};  
void main()  
{  
    Test a(10);  
    cout<<a.pa<<endl;  
    cout<<a.pb<<endl;  
    cin.get();  
}

类成员的构造是按照在类中定义的顺序进行的,而不是按照构造函数说明后的冒号顺序进行构造的。

三、Q&A

class ob
ob a;
ob *p=&a;
delete p;//这样会导致调用两次析构函数.是会引起程序错误的

只有

class ob
ob * p= new ob;
delete p; //这样是正确的
上一篇 下一篇

猜你喜欢

热点阅读