六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 50|回复: 0

Difference between C++ class and struct

[复制链接]

升级  59.33%

112

主题

112

主题

112

主题

举人

Rank: 3Rank: 3

积分
378
 楼主| 发表于 2013-1-29 22:39:00 | 显示全部楼层 |阅读模式
C++ Standard has an explanation for the difference. The first difference is described in 11.2 [size=11.6667px]Accessibility of base classes and base class members
 

In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with [size=11.6667px]the class-key struct and private is assumed when the class is defined with the class-key class.
 
The second difference is described in 11.1 Access specifiers. The explanation is in comments in the example code.
 
The following code shows this difference.
 

#include <iostream>using namespace std;struct SPerson {  void hello() const {    cout << "hello\n";  }};class CPerson {  void hello() const {    cout << "hello\n";  }};class Base {  public:    void hello() const {      cout << "hello\n";    }};struct SDerived: Base {};class CDerived: Base {};int main(int argc, const char *argv[]) {  SPerson sp;  sp.hello();  CPerson cp;  cp.hello(); // compile error  SDerived sd;  sd.hello();  CDerived cd;  cd.hello(); // compile error  return 0;} http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c is a discussion about the difference. Since [size=11.6667px]stackoverflow can't be accessed sometimes in China, I write this post.
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表