[C++]对象地址

Shape.h

#ifndef SHAPE_H
#define SHAPE_H

#include <iostream>
using namespace std;

class Shape
{
public:
    Shape();
    ~Shape();
    double calcArea();
};

#endif 

Shape.cpp

#include "Shape.h"

Shape::Shape()
{
    
}

Shape::~Shape()
{

}

double Shape::calcArea()
{
    cout << "Shape->calcArea()" << endl;
    return 0;
}

Circle.h

#ifndef CIRCLE_H
#define CIRCLE_H

#include "Shape.h"

class Circle :public Shape
{
public:
    Circle(int r);
    ~Circle();
protected:
    int m_iR;
};

#endif 

Circle.cpp

#include "Circle.h"

Circle::Circle(int r) {
    m_iR = r;
}

Circle::~Circle()
{

} 

demo.cpp

#include "Circle.h"

int main(void)
{
    Shape shape;
    //cout << sizeof(shape) << endl; 1

    int *p = (int *)&shape;
    cout << p << endl;//00AFFEDB
    cout << (unsigned int)(*p) << endl;//3435973836


    Circle circle(100);
    //cout << sizeof(circle) << endl; 4
    int *q = (int *)&circle;
    cout << q << endl;//00AFFCD8
    cout << (unsigned int)(*q) << endl;//100


    system("pause");
    return 0;
}
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容