国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

類指針學習1.1|C++類指針例子

2022-01-03 23:32 作者:LogByMax  | 我要投稿


#include <iostream>

?

using namespace std;

class Box

{

? ?public:

? ? ? // 構造函數(shù)定義

? ? ? Box(double l=2.0, double b=2.0, double h=2.0)

? ? ? {

? ? ? ? ?cout <<"Constructor called." << endl;

? ? ? ? ?length = l;

? ? ? ? ?breadth = b;

? ? ? ? ?height = h;

? ? ? }

? ? ? double Volume()

? ? ? {

? ? ? ? ?return length * breadth * height;

? ? ? }

? ?private:

? ? ? double length; ? ? // Length of a box

? ? ? double breadth; ? ?// Breadth of a box

? ? ? double height; ? ? // Height of a box

};

int main(void)

{

? ?Box Box1(3.3, 1.2, 1.5); ? ?// Declare box1

? ?Box Box2(8.5, 6.0, 2.0); ? ?// Declare box2

? ?Box Box3(3, 7.0, 1.0); ? ?// Declare box2

? ?Box *ptrBox; ? ? ? ? ? ? ? ?// Declare pointer to a class.

? ?// 保存第一個對象的地址

? ?ptrBox = &Box1;

? ?// 現(xiàn)在嘗試使用成員訪問運算符來訪問成員

? ?cout << "Volume of Box1: " << ptrBox->Volume() << endl;

? ?// 保存第二個對象的地址

? ?ptrBox = &Box2;

? ?// 現(xiàn)在嘗試使用成員訪問運算符來訪問成員

? ?cout << "Volume of Box2: " << ptrBox->Volume() << endl;

? ?

? ?// 保存第三個對象的地址

? ?ptrBox = &Box3;

? ?// 現(xiàn)在嘗試使用成員訪問運算符來訪問成員

? ?cout << "Volume of Box3: " << ptrBox->Volume() << endl;

??

? ?return 0;

}

輸出結果:

Constructor called.

Constructor called.

Constructor called.

Volume of Box1: 5.94

Volume of Box2: 102

Volume of Box3: 21


=reference=

[1]https://www.runoob.com/cplusplus/cpp-pointer-to-class.html

類指針學習1.1|C++類指針例子的評論 (共 條)

分享到微博請遵守國家法律
柘城县| 叙永县| 清河县| 马关县| 木兰县| 铜梁县| 安西县| 苍南县| 江陵县| 双鸭山市| 延寿县| 聂拉木县| 桦川县| 鄄城县| 施甸县| 武夷山市| 伊川县| 蓬溪县| 木里| 建平县| 和林格尔县| 保山市| 方正县| 封丘县| 望奎县| 彭山县| 岗巴县| 威远县| 裕民县| 宝坻区| 阿尔山市| 嘉祥县| 迁西县| 囊谦县| 台北市| 南和县| 宾川县| 綦江县| 抚宁县| 辽宁省| 信阳市|