最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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++類指針例子的評論 (共 條)

分享到微博請遵守國家法律
盈江县| 班戈县| 福安市| 康保县| 循化| 临沧市| 堆龙德庆县| 海淀区| 永丰县| 陈巴尔虎旗| 河曲县| 大姚县| 岑巩县| 二连浩特市| 巫溪县| 英山县| 武宣县| 策勒县| 湛江市| 普兰县| 广饶县| 克拉玛依市| 曲周县| 武城县| 宜城市| 佛教| 洪湖市| 马鞍山市| 华池县| 扎囊县| 岳阳市| 甘肃省| 连山| 阳西县| 天台县| 伊川县| 汝阳县| 英山县| 临朐县| 汨罗市| 屯留县|