【C++】构造函数与析构函数,类与继承

文章详细介绍了C++中的构造函数、析构函数的用法,以及类的三种访问修饰符(public,private,protected)对成员变量的影响。通过实例展示了基类和派生类的继承方式以及它们之间的成员访问规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

构造函数

代码

#include <iostream>

using namespace std;

class Line{
public:
    void setLength(double len);         //3个函数声明
    double getLength(void);
    Line(double line);                  //带参数的构造函数
    ~Line();							//析构函数,释放空间

private:
    double length;
};     //end of class Line

/*
    Line::Line(double line)
    {
        length = line;                          //这条语句的作用和下面初始化列表形式的作用相同
        cout << "object is created!" << endl;
        cout << "Length is:" << line << endl;
    }
*/

Line::Line(double line):length(line)            //使用初始化列表的形式,冒号后面跟的是赋值
{
        cout << "object is created!" << endl;
        cout << "Length is:" << line << endl;
}
Line::~Line()									//析构函数
{
    cout << "deleted!" << endl;
}
void Line::setLength(double len)
{
	length = len;
    cout << "You set:" << length << endl;
}
double Line::getLength()
{
	return length;
}

int main()
{
    //Line line1;
    //Line line2;
    //Line line3;

    Line line(3.5);
    line.setLength(7.36);

    Line line2(487.29);

    cout << "The length1 is:" << line.getLength() << endl;

    cout << "The length2 is:" << line2.getLength() << endl;

//    return 0;
}

参考链接: C++ 类构造函数 & 析构函数 | 菜鸟教程

输出结果

// An highlighted block
object is created!
Length is:3.5
You set:7.36
object is created!
Length is:487.29
The length1 is:7.36
The length2 is:487.29
deleted!
deleted!

Process returned 0 (0x0)   execution time : 0.021 s
Press any key to continue.

类与继承

基本

public: 即公开。
private: 无法在类的外部被访问。
protected: 可以被其派生类访问。

class B: public A
B为派生类,public继承于基类A。
在B中访问A的成员变量,即为在类的外部访问。

三种继承方式:public, private, protected.
在继承时,不同的继承方式会改变成员变量的“属性”。如果继承时不显示声明是private,protected,public继承,则默认是private继承,在struct默认public 继承。
如在基类A中,a1为public,派生类D为private方式继承,那么在D中则无法访问到A中的a1.但若为public方式继承,则仍可访问到。

在类内声明它的基类(即实例化),则在函数中会无法访问,因为作用域不同

代码

#include <iostream>
#include <assert.h>
using namespace std;

class A{                //A是基类
public:
    int a;

    A(){
        a1 = 1;
        a2 = 2;
        a3 = 3;
        a = 4;
    }

    void fun()
    {
        cout << a <<endl;
        cout << a1 <<endl;
        cout << a2 <<endl;
        cout << a3 <<endl;

    }

    int a1;

protected:
    int a2;
private:
    int a3;
};

//class <派生类名>:<继承方式><基类名>
class B:public A{           //A为基类,B为派生类
public:
    int a;
    B(int i)
    {
        A();
        a = i;
    }
    void fun()
    {
        cout << a <<endl;
        cout << a1 <<endl;
        cout << a2 <<endl;
//        cout << a3 <<endl;
    }
};

//class <派生类名>:<继承方式><基类名>
class C:protected A{           //A为基类,C为派生类
public:
    int a;
    C(int i)
    {
        A();
        a = i;
    }
    void fun()
    {
        cout << a <<endl;       //这里的a不是A中的a
        cout << a1 <<endl;      //基类为public
        cout << a2 <<endl;      //基类为protected
//        cout << a3 <<endl;     //基类为private
    }
};

//class <派生类名>:<继承方式><基类名>
class D:private A{           //A为基类,D为派生类
public:
    int a;                    //这里无论是否声明a,修改的均不为A中的a
    //int a1;
    D(int i)
    {
        A();
        a = i;
        //a1 = a + 1;
    }
    void fun()
    {
        cout << a <<endl;       //这里的a不是A中的a
        cout << a1 <<endl;      //基类为public
        cout << a2 <<endl;      //基类为protected
        //cout << a3 <<endl;     //基类为private
    }

    /*    A A33;

    void seta(int i)
    {
        A33.a = i;
        cout << A33.a << endl;
    }
    */
};

int main()
{
    A A1;
    A1.fun();

    B B1(79);               //这里仅仅修改了类B中的a值
    cout << B1.a << endl;
    cout << A1.a << endl;

    B1.fun();
    cout << B1.a1 << endl;   //类外均不能访问private,protected
//    cout << B1.a2 << endl;
//    cout << B1.a3 << endl;

    C C1(666);
    C1.fun();
    cout << C1.a << endl;
    //cout << C1.a2 << endl;
    //cout << C1.a3 << endl;

    D D1(7777);
    D1.fun();
    cout << D1.a << endl;
//    cout << D1.a1 << endl;
//   cout << D1.a2 << endl;
}

总结

在这里插入图片描述参考: C++ 类访问修饰符 | 菜鸟教程

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值