C++ 指向類的指標
阿新 • • 發佈:2019-02-13
#include <iostream>
#include <cmath>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
using namespace std;
class Box
{
public:
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;
double breadth;
double height;
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5);
//定義指向類的指標
Box *ptrBox;
//初始化
ptrBox = &Box1;
// 使用成員訪問運算子
cout << "Volume of Box1: " << ptrBox->Volume() << endl;
return 0;
}
#include <cmath>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
using namespace std;
class Box
{
public:
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;
double breadth;
double height;
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5);
//定義指向類的指標
Box *ptrBox;
//初始化
ptrBox = &Box1;
// 使用成員訪問運算子
cout << "Volume of Box1: " << ptrBox->Volume() << endl;
return 0;
}