C++中的struct也能定义类!!!

    xiaoxiao2021-03-25  91

    本博参考了以下博客:http://www.cnblogs.com/ccsccs/articles/4025215.html 一、struct 陌生功能介绍 1、stuct用构造函数赋默认值

    struct A { int a; int b; A() //构造函数 { a=0; b=0; } }

    2、struct可以实现封装、继承,据说也可以实现多态。

    struct A { char a; }; struct B : A { char b; };

    这个时候B是public继承A的。

    二、struct与class的区别 1、在继承中,默认访问权限不同

    class A { char a; }; class B : A { char b; };

    这是默认B是private继承A的。这就是默认的继承访问权限。所以我们在平时写类继承的时候,通常会这样写:

    class B :pulic A

    2、“class”这个关键字能定义模板参 数,就像“typename”。但关键字“struct”不用于定义模板参数。这一点在Stanley B.Lippman写的Inside the C++ Object Model有过说明。

    转载请注明原文地址: https://ju.6miu.com/read-19615.html

    最新回复(0)