c++ 类的构造函数及子类的构造函数

类在创建的时候需要调用构造函数,假如对系统没有任何说明,则会调用系统默认的构造函数,在需要自行写构造函数的时候往往是一下几种情况:

1.构造函数中不带参数:

#include<iostream>
using namespace std;
class Student{
    public:
        void setID(int ID);
        int getID(void);
        Student();    //构造函数
    private:
        int ID;
};
Student::Student (void){   //不带参数
    cout<<"Object is being created"<<endl;
}
void Student::setID(int ID){
    this->ID=ID;
}
int Student::getID(void){
    return ID;
}
int main(){
    Student s;
    s.setID(1);
    cout<<"ID of Stu : "<<s.getID()<<endl;
    return 0;
} 

 

2.构造函数中含有参数:

#include<iostream>
using namespace std;
class Student{
    public:
        void setID(int ID);
        int getID(void);
        Student(int ID);    //定义构造函数时含有参数
    private:
        int ID;
};
Student::Student(int ID){
    cout<<"Object is being created"<<endl;
    this->ID=ID;
}
void Student::setID(int ID){
    this->ID=ID;
}
int Student::getID(void){
    return ID;
}
int main(){
    Student s(1);//当构造函数中含有参数的时候,初始化这个类所定义的变量方式将会不同
    cout<<"ID of Stu : "<<s.getID()<<endl;
    s.setID(2);
    cout<<"ID of Stu : "<<s.getID()<<endl;
    return 0;
} 

 

3.类的多重构造函数:

当一个类中存在多个构造函数的时候,系统会自动选出匹配的构造函数

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

class Student{
    public:
        Student(int);
        Student(string);//存在两个构造函数
        int getID(void);
        string getName(void);
        string setName(string getname);
    private:
        int ID;
        string name;
};
Student::Student(int ID){//分别定义
    this->ID=ID;
}
Student::Student(string name){
    this->name=name;
}
string Student::getName(void){
    return name;
}
int Student::getID(void){
    return ID;
}
string Student::setName(string name){
    return this->name=name;
}

int main(){
    Student s(1);//两种初始化方式
    cout<<s.getID()<<endl;
    s.setName("wybnmsl");
    cout<<s.getName()<<endl;
    /*
    Student s("wybnmsl");
    cout<<s.GetName()<<endl;
    s.SetID(1);
    cout<<s.GetID()<<endl;;
    */
    return 0;
} 

 

4.类的析构函数:

析构函数不返回任何值,没有返回类型,也没有函数参数。在类对象生命周期结束时,会自动调用析构函数

#include<iostream>
using namespace std;
class Student{
    public:
        void setID(int ID);
        int getID(void);
        Student(int ID);    
        ~Student();//析构函数
    private:
        int ID;
};
Student::Student(int ID){
    cout<<"Object is being created"<<endl;
    this->ID=ID;
}
Student::~Student(void){//无返回值也没有函数参数
    cout<< "Object is being deleted"<<endl;
}
void Student::setID(int ID){
    this->ID=ID;
}
int Student::getID(void){
    return ID;
}
int main(){
    Student s(1);
    cout<<"ID of Stu : "<<s.getID()<<endl;
    s.setID(2);
    cout<<"ID of Stu : "<<s.getID()<<endl;
    return 0;
} 

 

5.构造函数的简单写法:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

class Student{
    public:
        void setID(int ID);
        int getID();
        void setName(string name);
        string getName();
        Student(string _name,int _ID):name(_name),ID(_ID){};//简写形式,但是不能有this指针
    private:
        int ID;
        string name;
};

void Student::setID(int ID){
    this->ID=ID;
}
int Student::getID(){
    return ID;
}
void Student::setName(string name){
    this->name=name;
}
string Student::getName(){
    return name;
}

int main(){
    Student s("wybnmsl",1);
    cout<<s.getID()<<" "<<s.getName()<<endl;
    s.setID(2);
    s.setName("wybnmsl123");
    cout<<s.getID()<<" "<<s.getName()<<endl;
    return 0;
}

 

6.子类的构造函数:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

class People{
    public:
        void setID(int ID);
        int getID();
        void setName(string name);
        string getName();
        People(string _name,int _ID):name(_name),ID(_ID){};
    private:
        int ID;
        string name;
};
void People::setID(int ID){
    this->ID=ID;
}
int People::getID(){
    return ID;
}
void People::setName(string name){
    this->name=name;
}
string People::getName(){
    return name;
}


class Student:public People{
    public:
        void setStuID(int StuID);
        int getStuID(void);
        Student(int _StuID,string _name,int _ID):StuID(_StuID),People(_name,_ID){}//子类调用父类的构造函数
    private:
        int StuID;
        
};
void Student::setStuID(int StuID){
    this->StuID=StuID;
}
int Student::getStuID(void){
    return StuID;
}

int main(){
    Student s(2017,"wybnmsl",1);
    cout<<s.getStuID()<<" "<<s.getName()<<" "<<s.getID()<<endl;
    s.setStuID(2020);
    s.setName("wybnmsl123");
    s.setID(2);
    cout<<s.getStuID()<<" "<<s.getName()<<" "<<s.getID()<<endl;
    return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值