现在的位置: 首页 > 综合 > 正文

C/C++面试题:class和struct有什么区别?

2017年12月24日 ⁄ 综合 ⁄ 共 2525字 ⁄ 字号 评论关闭

(1)C的struct与C++的class的区别。
(2)C++中的struct和class的区别。
在第一种情况下,struct中只能定义成员变量,不能定义成员函数。例如下面的C代码片断:
        struct Point
        {
                int x; // 合法
                int y; // 合法
                void print()
                {
                        printf("Point print\n"); //编译错误
                };
}9        ;
这里第7行会出现编译错误,提示如下的错误消息:“函数不能作为Point结构体的成员”。因此大家看到在第一种情况下struct只是一种数据类型,不能使用面向对象编程。
第二种情况。首先请看下面的代码:
        #include <iostream>
        using namespace std;
        class CPoint
        {
                int x;                  //默认为private
                int y;                  //默认为private
                void print()            //默认为private
                {
                        cout << "CPoint: (" << x << ", " << y << ")" << endl;
                }
        public:
                CPoint(int x, int y)      //构造函数,指定为public
                {
                        this->x = x;
                        this->y = y;
                }
                void print1() //public
                {
                        cout << "CPoint: (" << x << ", " << y << ")" << endl;
                }
        };
        
        struct SPoint
        {
                int x;              //默认为public
                int y;              //默认为public
                void print()         //默认为public
                {
                        cout << "SPoint: (" << x << ", " << y << ")" << endl;
                }
                SPoint(int x, int y)  //构造函数,默认为public
                {
                        this->x = x;
                        this->y = y;
                }
        private:
                void print1()      //private类型的成员函数
                {
                        cout << "SPoint: (" << x << ", " << y << ")" << endl;
                }
        };
        
        int main(void)
        {
                CPoint cpt(1, 2);  //调用CPoint带参数的构造函数
                SPoint spt(3, 4);  //调用SPoint带参数的构造函数
                
                cout << cpt.x << " " << cpt.y << endl;  //编译错误
                cpt.print();       //编译错误
                cpt.print1();      //合法
        
                spt.print();      //合法
                spt.print1();     //编译错误
                cout << spt.x << " " << spt.y << endl;  //合法
        
                return 0;
        }
struct还有构造函数和成员函数,其实它还拥有class的其他特性,例如继承、虚函数等。
class中默认的成员访问权限是private的,而struct中则是public的。
在类的继承方式上,struct和class又有什么区别?请看下面的程序:
        #include <iostream>
        using namespace std;
        class CBase
        {
        public:
                void print()                //public成员函数
                {
                        cout << "CBase: print()..." << endl;
                }
        };
        class CDerived1 : CBase        //默认private继承
        {
        };
        
        class CDerived2 : public Cbase   //指定public继承
        {
        };
        
        struct SDerived1 : Cbase        //默认public继承
        {
        };
        
        struct SDerived2 : private Cbase  //指定public继承
        {
        };
        
        int main()
        {
                CDerived1 cd1;
                CDerived2 cd2;
                SDerived1 sd1;
                SDerived2 sd2;
        
                cd1.print();    //编译错误
                cd2.print();
                sd1.print();
                sd2.print();    //编译错误
        
                return 0;
        }
可以看到,以private方式继承父类的子类对象不能访问父类的public成员。class继承默认是private继承,而struct继承默认是public继承。
另外,在C++模板中,类型参数前面可以使用class或typename,如果使用struct,则含义不同,struct后面跟的是“non-type template parameter”,而class或typename后面跟的是类型参数。

【上篇】
【下篇】

抱歉!评论已关闭.