C++ 第三周

shape.h 

#pragma once
#ifndef __SHAPE_H__
#define __SHAPE_H__
struct Point
{
	double x;
	double y;
};

struct CircleA
{
	double x;
	double y;
	double r;
};

struct CircleB:public Point
{
	double r;
};

struct CircleC
{
	Point  p;
	double r;
};

#endif

test1.cpp

#include <iostream>
using namespace std;
#include "shape.h"
int main()
{
    Point* p = new Point();
    p->x = 1;
    p->y = 2;

    double* dp = (double*)p;
    cout << dp[0] << " " << dp[1] << endl;

    double* dpp = &p->x;
    cout << dpp[0] << " " << dpp[1] << endl;

    delete p;

    CircleA* c1 = new CircleA();
    c1->x = 3;
    c1->y = 4;
    c1->r = 5;
    p = (Point*)c1;
    cout << "circle as point " << p->x << " " << p->y << endl;

    CircleB* c2 = (CircleB*)c1;
    cout << "circleA as circleB " << c2->x << " " << c2->y << " " << c2->r << endl;

    double* dppp = &c2->x;
    cout << dppp[0] << " " << dppp[1] << " "<< dppp[2] << endl;

    cout << sizeof(CircleA) << " " << sizeof(CircleB) << " " << sizeof(CircleC) << endl;

    CircleC* c3 = new CircleC();
    Point* p3 = &c3->p;
    CircleC* c33 = (CircleC*)p3;


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值