首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >C/C++构造函数

C/C++构造函数

作者头像
用户7886150
修改于 2021-02-02 11:41:43
修改于 2021-02-02 11:41:43
5680
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: C++构造函数

1、在类中定义构造函数 

#include <stdio.h>

#include <string.h>

class Student

{

private:

    int id;

    char name[32];

public:

    Student(int id, const char* name)

    {

        this->id = id;

        strcpy(this->name, name);

    }

};

int main()

{

    Student  s ( 201601, "shaofa");

    return 0;

} 

①构造函数时,需要加上public:,类元素都是private。构造函数+(参数)即可,不需要重新定义一个新的函数名 

②关于字符串的复制,用函数复制字符串。不能直接复制 

③(参数),形参的命名直接采用类中元素名,字符串用char* name 

④this->id=id,形参赋值到类元素 

2、在类的构造函数对类的元素赋值 

#include <stdio.h>

#include <string.h>

class Student

{

private:

    int id;

    char name[32];

public:

    Student()

    {

        id = 0;

        name[0] = 0;

    }

};

int main()

{

    Student  s ;

    return 0;

} 

 直接在{}中赋值(自己错误的在()中赋值) 

注意:类对应的{}后是加了;号,里面定义public和private时,{}后面是没有;的 

3、重载构造函数 

#include <stdio.h>

#include <string.h>

class Student

{

private:

    int id;

    char name[32];

public:

    // 默认构造函数

    Student()

    {

        id = 0;

        name[0] = 0;

    }

    //  带参构造函数

    Student(int id, const char* name)

    {

        this->id = id;

        strcpy(this->name, name);

    }

};

int main()

{

    Student  s1 ;

    Student  s2 ( 201601, "shaofa");

    return 0;

} 

默认构造函数:不传入参数,直接赋值为0 

带参数的构造函数:传入形参后进行赋值 

4、当构造的函数与类名不相同时 

#include <stdio.h>

#include <string.h>

class Triangle

{

    // 成员变量

    // 成员函数

private:

    int a, b, c;

public:

    Triangle(int a, int b, int c): a(a), b(b), c(c)

    {

    }

    // 判断是否为直角三角形

    bool right()

    {

        int aa = a * a;

        int bb = b * b;

        int cc = c * c;

        if(aa+bb == cc || aa+cc==bb || bb+cc==aa)

        {

            return true;

        }

        return false;

    }

};

int main()

{

    Triangle t(3,4,6);

    if(t.right())

    {

        printf("是直角三角形!\n");

    }

    else

    {

        printf("不是直角三角形!\n")

    }

    return 0;

} 

在类中: 先定义构造函数,再定义成员函数 

在main中:先定义对象t,再通过t.right()调用类中的成员函数 

重点题目: 

1、求两点之间的距离 

#include <stdio.h>

#include <math.h>

class AfPoint

{

private:

    int x, y;

public:

    AfPoint()

        : x(0), y(0)

    {

    }

    AfPoint(int x, int y) 

        : x(x), y(y)

    {

    }

public:

    double distance(const AfPoint& other)

    {

        int dx = x - other.x;

        int dy = y - other.y;

        return sqrt( double(dx*dx + dy*dy));

    }

};

int main()

{

    AfPoint p1(0,0);

    AfPoint p2(10,10);

    double d = p1.distance(p2);

    printf("distance: %.2lf\n", d);

    return 0;

} 

 定义新的函数来求坐标差dx,传入参数为const AFpoint& other,通过other.x传入另一个坐标 

mian中通过p1.distance(p2)实现求坐标差 

类定义和main中参数传入 

#include <stdio.h>

class AfInteger

{

private:

    int value;

public:

    AfInteger()

    {

        this->value = 0;

    }

    AfInteger(int value):value(value)

    {        

    }

    int getValue()

    {

        return value;

    }

};

int main()

{

    AfInteger obj = 12;

    printf("value: %d \n", obj.getValue());

    return 0;

} 

mian中调用类中函数,则在类中需要在成员函数中return 变量 

在默认构造函数中给变量赋初值 

直接将值赋给创建的对象,此值会通过带参数的构造函数传入给value吗? 

每一个类中都需要定义一个默认的构造函数,默认构造函数一定是用来赋初值的(两种赋初值的方法,另一种for循环赋值方法如下题) 

在一个类的成员函数中传入另一个类:参数的使用方法 

#include <stdio.h>

class AfPoint

{

public:

    AfPoint():x(0), y(0) 

    {}

    AfPoint(int x, int y):x(x), y(y) 

    {}

    int x;

    int y;

};

class AfSize

{

public:

    AfSize():width(0), height(0) 

    {}

    AfSize(int w, int h):width(0), height(0) 

    {}

    int width;

    int height;

};

class AfRect

{

private:

    AfPoint location;

    AfSize size;

public:

    AfRect()

    {

    }

    AfRect(int x, int y, int width, int height)

        : location(x,y), size(width, height)

    {

    }

    AfRect(const AfPoint& pt, const AfSize& size)        

    {

        location = pt;

        size = size;

    }

};

int main()

{

    AfRect r1 (0,0, 640, 480);

    AfRect r2 (AfPoint(0,0), AfSize(640, 480)); // 使用临时对象,语法见书本内描述

    return 0;

}

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档