C+繼承

來源:互聯網
上載者:User

標籤:amp   指標   include   str   範圍   return   ica   word   []   

// 繼承.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


struct person{
int age;
int sex;
};
struct student:person{
int grade;
int intst;
};


void test(){

//person per;
//per.age=1;
//per.age=2;

/*
00401028 mov dword ptr [ebp-8],1
0040102F mov dword ptr [ebp-8],2
*/
student xm;
xm.age=1;
xm.grade=2;
xm.intst=3;
xm.sex=4;

/*00401028 mov dword ptr [ebp-10h],1
0040102F mov dword ptr [ebp-8],2
00401036 mov dword ptr [ebp-4],3
0040103D mov dword ptr [ebp-0Ch],4
*/

 

//xm.age=12;
//xm.sex=13;
//xm.age=14;

//person* p1=&xm;
//printf("%d\n",p1->age);//可以用父類的指標指向子類的對象,但是只能訪問父類的成員,而且安全,強制類型轉化之後可以用子類的指標指向父類的對象
//但是不安全,因為子類指標所能表示的範圍大於父類對象所表示的範圍,
//student* p2=(student*)&per;
//printf("%d\n",p2->age);

}
int main(int argc, char* argv[])
{
test();
printf("Hello World!\n");
return 0;
}

//繼承的本質就是成員的的複製,在子類初始化時,會將父類擁有的成員也複製到子類中

C+繼承

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.