C/C++class和struct有什么区别?

C/C++面试题:class和struct有什么区别?
考点:class和struct的区别
出现频率:☆☆☆☆
解析:
这里有两种情况下的区别。
(1)C的struct与C++的class的区别。
(2)C++中的struct和class的区别。
在第一种情况下,struct与class有着非常明显的区别。C是一种过程化的语言,struct只是作为一种复杂数据类型定义, struct中只能定义成员变量,不能定义成员函数。例如下面的C代码片断:
            structPoint
            {
                    int x; //合法
                    int y; //合法
                    voidprint()
                    {
                                printf("Point print\n"); //编译错误
                    };
}9            ;
这里第7行会出现编译错误,提示如下的错误消息:“函数不能作为Point结构体的成员”。因此大家看到在第一种情况下struct只是一种数据类型,不能使用面向对象编程。现在来看第二种情况。首先请看下面的代码:
            #include<iostream>
            usingnamespace std;
            classCPoint
            {
                    intx;                        //默认为private
                    inty;                        //默认为private
                    voidprint()                //默认为private
                    {
                                cout<< "CPoint: ("<< x <<", " << y<< ")"<< endl;
                    }
            public:
                    CPoint(intx, int y)        //构造函数,指定为public
                    {
                                this->x = x;
                                this->y = y;
                    }
                    voidprint1() //public
                    {
                                cout<< "CPoint: ("<< x <<", " << y<< ")"<< endl;
                    }
            };
           
            structSPoint
            {
                    intx;                    //默认为public
                    inty;                    //默认为public
                    voidprint()            //默认为public
                    {
                                cout<< "SPoint: ("<< x <<", " << y<< ")"<< endl;
                    }
                    SPoint(intx, int y)    //构造函数,默认为public
                    {
                                this->x = x;
                                this->y = y;
                    }
            private:
                    voidprint1()        //private类型的成员函数
                    {
                                cout<< "SPoint: ("<< x <<", " << y<< ")"<< endl;
                    }
            };
           
            intmain(void)
            {
                    CPointcpt(1,2);    //调用CPoint带参数的构造函数
                    SPointspt(3,4);    //调用SPoint带参数的构造函数
                   
                    cout<< cpt.x<< " "<< cpt.y<<endl;    //编译错误
                    cpt.print();        //编译错误
                    cpt.print1();        //合法
           
                    spt.print();        //合法
                    spt.print1();        //编译错误
                    cout<< spt.x<< " "<< spt.y<<endl;    //合法
           
                    return0;
            }
在上面的程序里,struct还有构造函数和成员函数,其实它还拥有class的其他特性,例如继承、虚函数等。因此C++中的struct扩充了C的struct功能。那它们有什么不同呢?
main函数内的编译错误全部是因为访问private成员而产生的。因此我们可以看到 class中默认的成员访问权限是private的,而struct中则是public的。在类的继承方式上,struct和class又有什么区别?请看下面的程序:
            #include<iostream>
            usingnamespace std;
            classCBase
            {
            public:
                    voidprint()                    //public成员函数
                    {
                                cout<< "CBase: print()..."<< endl;
                    }
            };
            classCDerived1 : CBase            //默认private继承
            {
            };
           
            classCDerived2 : public Cbase    //指定public继承
            {
            };
           
            structSDerived1 : Cbase            //默认public继承
            {
            };
           
            structSDerived2 : privateCbase    //指定public继承
            {
            };
           
            intmain()
            {
                    CDerived1cd1;
                    CDerived2cd2;
                    SDerived1sd1;
                    SDerived2sd2;
           
                    cd1.print();    //编译错误
                    cd2.print();
                    sd1.print();
                    sd2.print();    //编译错误
           
                    return0;
            }
可以看到,以private方式继承父类的子类对象不能访问父类的public成员。class 继承默认是private继承,而struct继承默认是public继承。
另外,在C++模板中,类型参数前面可以使用class或typename,如果使用struct,则含义不同,struct后面跟的是“non-typetemplate parameter”,而class或typename后面跟的是类型参数。
事实上,C++中保留struct的关键字是为了使C++编译器能够兼容C开发的程序。
答案:
分以下所示两种情况。
C的struct与C++的class的区别:struct只是作为一种复杂数据类型定义,不能用于面向对象编程。
C++中的struct和class的区别:对于成员访问权限以及继承方式,class中默认的是private的,而struct中则是public的。class还可以用于表示模板类型,struct则不行。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值