php 声明struct_C++_C++中声明类的class与声明结构体的struct关键字详解,class class 关键字声明类类型或 - phpStudy...

C++中声明类的class与声明结构体的struct关键字详解

classclass 关键字声明类类型或定义类类型的对象。

语法

[template-spec]

class [ms-decl-spec] [tag [: base-list ]]

{

member-list

} [declarators];

[ class ] tag declarators;

参数

template-spec

可选模板说明。

ms-decl-spec

可选存储类说明有关更多信息

tag

给定于类的类型名称。在类范围内的标记成为了保留字。标志是可选项。如果省略,定义匿名类。

base-list

此类派生其成员的类或结构的可选列表。

member-list

类成员列表。

declarators

指定类类型一个或多个实例名称的声明符列表。如果类的所有数据成员是 public,声明符可以包含初始值设定项列表。

使用举例

// class.cpp

// compile with: /EHsc

// Example of the class keyword

// Exhibits polymorphism/virtual functions.

#include

#include

#define TRUE = 1

using namespace std;

class dog

{

public:

dog()

{

_legs = 4;

_bark = true;

}

void setDogSize(string dogSize)

{

_dogSize = dogSize;

}

virtual void setEars(string type) // virtual function

{

_earType = type;

}

private:

string _dogSize, _earType;

int _legs;

bool _bark;

};

class breed : public dog

{

public:

breed( string color, string size)

{

_color = color;

setDogSize(size);

}

string getColor()

{

return _color;

}

// virtual function redefined

void setEars(string length, string type)

{

_earLength = length;

_earType = type;

}

protected:

string _color, _earLength, _earType;

};

int main()

{

dog mongrel;

breed labrador("yellow", "large");

mongrel.setEars("pointy");

labrador.setEars("long", "floppy");

cout << "Cody is a " << labrador.getColor() << " labrador" << endl;

}

structstruct 关键字定义结构类型和/或结构类型的变量。

[template-spec] struct[ms-decl-spec] [tag [: base-list ]]

{

member-list

} [declarators];

[struct] tag declarators;

参数

与class的参数相同,可以参照上面的。

备注

结构类型是用户定义的复合类型。 它由可具有不同类型的字段或成员构成。

在 C++ 中,结构与类相同,只不过其成员默认为 public。

使用结构

在 C 中,你必须显式使用 struct 关键字来声明结构。 在 C++ 中,你不需要在定义该类型之后使用 struct 关键字。

可以选择在定义结构类型时,通过在右大括号和分号之间放置一个或多个逗号分隔的变量名称来声明变量。

可以初始化结构变量。 每个变量的初始化必须括在大括号中。

有关相关信息,请参阅 class、union 和 enum。

示例

#include

using namespace std;

struct PERSON { // Declare PERSON struct type

int age; // Declare member types

long ss;

float weight;

char name[25];

} family_member; // Define object of type PERSON

struct CELL { // Declare CELL bit field

unsigned short character : 8; // 00000000 ????????

unsigned short foreground : 3; // 00000??? 00000000

unsigned short intensity : 1; // 0000?000 00000000

unsigned short background : 3; // 0???0000 00000000

unsigned short blink : 1; // ?0000000 00000000

} screen[25][80]; // Array of bit fields

int main() {

struct PERSON sister; // C style structure declaration

PERSON brother; // C++ style structure declaration

sister.age = 13; // assign values to members

brother.age = 7;

cout << "sister.age = " << sister.age << '\n';

cout << "brother.age = " << brother.age << '\n';

CELL my_cell;

my_cell.character = 1;

cout << "my_cell.character = " << my_cell.character;

}

// Output:

// sister.age = 13

// brother.age = 7

// my_cell.character = 1

相关阅读:

php实现根据IP地址获取其所在省市的方法

Mysql在debian系统中不能插入中文的终极解决方案

Objective-C处理空字符串和页面传值及自定义拷贝

js四舍五入数学函数round使用实例

xp系统重启后D盘消失了如何找回?xp系统重启后D盘不见了的解决方法

MySQL常用时间函数详解(推荐)

javascript 获取iframe里页面中元素值的方法

C#字符串使用密钥进行加解密

最简洁的CSS清除浮动的方法

javascript实现点击单选按钮链接转向对应网址的方法

Win7强制关机后突然无法重新启动的解决办法

图片放大镜jquery.jqzoom.js使用实例附放大镜图标

如何使用php实现评委评分器

Java数组的基本学习教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值