SlideShare a Scribd company logo
OOP in C
 Inherit


           elpam.tw@gmail.com
           elpam@Taiwan, Taipei, 2008
LICENSE
●   本投影片授權方式為:
    –   姓名標示─非商業性─相同方式分享 3.0  台灣版
    –   http://creativecommons.org/licenses/by­nc­sa/3.0/tw/




●   所有的範例程式皆為 Public Domain




                              elpam.tw@gmail.com
About This Slides
●   All example was build by 
    –   GCC­4.1.3
    –   GLIB­1.2.10
    –   GMAKE­3.81




                      elpam.tw@gmail.com
C  Language Review
●   Stack
    –   Caller Function's Address
    –   CPU's Register
    –   Local Value
●   Heap (run­time heap)
    –   managed by malloc




                         elpam.tw@gmail.com
Address Space
●   Linux Memory Model
    –   copy from Jserv's               STACK
        Hacking Hello World




                                        HEAP




                        elpam.tw@gmail.com
Function Stack          Caller's Address
                                             Return Address


void Func_B()                                  Local Value
{
    int a,b;
   /* do nothing*/
}                                                               Low
void Func_A()                                  int b
{                       Func_B's Stack         int a
    char a;                                   Func_A
    Func_B();
}
                                               char a
                        Func_A's Stack
                                                main
int main()
{
    Func_A();
}                        main's stack            ....

                        elpam.tw@gmail.com                      High
After Review
●   請了解 Callee Stack 裡頭會放置那些東西
●   請了解 Callee Stack Release 後那些變數會被清
    除掉
●   請確定已正確的了解 Stack 與 Heap 的差別




                elpam.tw@gmail.com
Structure




elpam.tw@gmail.com
Structure (I)
                                      /* 1­1.c */
 ●   Address + Offset                 struct A{
                                         int a;
                                         int b;
     –   pa = 0x804a008               };
     –   pa ­> a == pa + 0            int main()
                                      {
     –   pa ­> b == pa + 4             struct A* pa =malloc(sizeof(struct A));

                                        printf("%x,%x,%xn"
                                                ,pa, &(pa­>a), &(pa­>b) );

                                        free( pa );
 #> ./1­1                             }
804a008,804a008,804a00c
 #>


                             elpam.tw@gmail.com
●   所有的東西都是 Memory Address
●   ­>  所代表的意義是 offset ,而這一個數值的大
    小可以在 Compiler Time 被決定




               elpam.tw@gmail.com
Structure (II)
                                      /* 1­2.c */
                                      struct A{                       Size = 8 byte
 ●   pa == pb                            int a;   int b;
                                      };
      –     pa ­> c Compile Error     struct B{
                                         int a;   int b;
      –     pb ­> c Correct              int c;                       Size = 12 byte
                                      };
                                      int main()
                                      {
                                       struct A* pa =malloc(sizeof(B));
                                       struct B* pb = pa;
                                        pa ­> a = 1;     pa ­> b = 2;
 #> ./1­2                               pb ­> c = 3;
1,2,3
  #>                                    printf("pb(%d,%d,%d)n"
                                                       ,pb­>a,pb­>b,pb­>c );
                                      }


                              elpam.tw@gmail.com
●   Pointer 的 Type 與被 allocate 的記憶體大小是沒
    有關係的
●   請注意我們是如何使用 casting




                 elpam.tw@gmail.com
/* 1­3.c */
                  Structure (III)
struct A{
     int a;
●
}; Casting        int main()
                  {
                       struct A* pa = malloc( sizeof(struct A) );
struct B{              struct B* pb = pa;
    short b;           struct C* pc = pa;
    short c;
};                    pc ­> hi_b = 0x12;
                      pc ­> low_b = 0x34;
struct C{             pc ­> hi_c = 0x56;
    char low_b;       pc ­> low_c = 0x78;
    char hi_b;
    char low_c;       printf("pb­>b(0x%x)  pb­>c(0x%x)n",pb­>b,pb­>c );
    char hi_c;        printf("pa­>a(0x%x)n",pa­>a );
};
                      free( pa );
                  }


                        elpam.tw@gmail.com
Structure (III) (cont')
●   Memory Casting
●   x86 is little­endian                    pc ­> hi_b = 0x12;
                                            pc ­> low_b = 0x34;
                                            pc ­> hi_c = 0x56;
                                            pc ­> low_c = 0x78;




                                        #>./1­3
                                      pb­>b(0x1234)  pb­>c(0x5678)
                                      pa­>a(0x56781234)
                                        #>




                       elpam.tw@gmail.com
Inherit




elpam.tw@gmail.com
C++ Review
                                      /* 1­6.cpp */
                                      class A{
●   Casting to Parent                    public:
                                            int a;
                                      };
                                      class B : public A{
                                         public:
                                            int b;
                                      };

                                      int main()
                                      {
                                           B b;
                                           b . b  = 2;
                                           A(b) . a = 1
                                      }




                        elpam.tw@gmail.com
Inherit
                                          int main()
                                          {
   ●   sizeof(A) =  8                      struct B* pb = malloc(sizeof(struct B));
                                            pb ­> parent . a = 0;
   ●   sizeof(B) = 12                       pb ­> parent . b = 1;
                                            pb ­> c = 2
                    /* 1­4.h */           }
                    struct A{
                       int a;
                       int b;             /* 1­4.c */
       Inherit A    };                    int main()
                    struct B{             {
                       struct A parent;    struct A* pa =malloc(sizeof(struct B));
                       int c;              struct B* pb = pa;
                    };                      pa ­> a = 0;
                                            pa ­> b = 1;
  #>./1­4                                   pb ­> c = 2;
pa(1,2) pb(3)                             }
  #>
                             elpam.tw@gmail.com
Inherit & Casting
                                         /* 1­5.c */
   ●   Casting to Parent                 int main()
                                         {
                                          struct B* pb =malloc(sizeof(struct B));
                                           pb ­> c = 3;
                                          struct A* pa = pb;
                   /* 1­5.h */             pa ­> a = 1;
                   struct A{               pa ­> a = 2;
                      int a;
                      int b;               printf("pa(%d,%d) pb(%d)n"
                   };                              ,pa­>a,pa­>b,pb­>c );
                   struct B{             }
                      struct A parent;
                      int c;  
                   };

  #>./1­5
pa(1,2) pb(3)
  #>
                            elpam.tw@gmail.com
Public Member Value in C++
                                      int main()
                                      {
class A{
                                        B* b = new B;
public:
   int a;
                                        b . a = 0;
   int b;
                                        b . b = 1;
};
                                        b . c = 2;
class B : public A {
                                      }
public:
   int c;  
};
                                      int main()
                                      {
                                        A* a = new b;

                                        a . a = 0;
                                        a . b = 1;
                                        a . c = 2;       /* error */
                                        B(a) . c = 2;  /* correct */
                                      }
                       elpam.tw@gmail.com
Object Inherit
●   C Language                           ●   C++


     struct A{                                     class A{
        int a;                                     public:
        int b;                                        int a;
     };                                               int b;
     struct B{                                     };
        struct A parent;                           class B : public A {
        int c;                                     public:
     };                                               int c;  
                                                   };




                              elpam.tw@gmail.com
Type Casting ( 形態轉換 )
●   C Language                         ●   C++
    –   Structure­to­Structure             –     dynamic_cast
         ●   Address­to­Address            –     static_cast
                                           –     reinterpret_cast
                                           –     const_cast




                            elpam.tw@gmail.com
Inherit in C
●   C ­> B ­> A
                                       /* error memory access */
                                       int main()
    struct A{                          {
       int v1;                          struct B* pb = malloc(sizeof(B));
       int v2;                          struct C* pc = pb; /* correct */
    };                                  pc ­> v1 = 0; /* run time error */
    struct B{
       struct A parent;                }
       int v1;  
    };
    struct C{
       struct B parent;                struct C c;
       int v1;                         struct B* pb = &c;  /* use B's member */
    };                                 struct A* pb = &c;  /* use A's member */

                            elpam.tw@gmail.com
Conclusion 



我們可以使用 memory casting
的技巧來描述繼承關係

*  因為缺乏語法 (Compiler) 的保護,所以我們必須
要小心的使用



            elpam.tw@gmail.com
Questions
●   建構子 (Constructor)  還有需要嗎 ?
●   但 C++ 的繼承還提供了 Virtual Function,  需要
    這樣的功能嗎 ?  該如何實作 ?
    –   下一個投影片將討論這兩個問題




                 elpam.tw@gmail.com

More Related Content

What's hot

C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
Stephane Gleizes
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
Geeks Anonymes
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
Sumant Tambe
 
C++11
C++11C++11
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
Chris Ohk
 
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
[C++ korea] effective modern c++ study   item 3 understand decltype +이동우[C++ korea] effective modern c++ study   item 3 understand decltype +이동우
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
Seok-joon Yun
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
Jan Rüegg
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
Geeks Anonymes
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5
Ismar Silveira
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guide
Tiago Faller
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
Vivek Das
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
karmuhtam
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
Olve Maudal
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义
yiditushe
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
xu liwei
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
Ali Aminian
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Sachin Sharma
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
Nico Ludwig
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
Ismar Silveira
 

What's hot (20)

C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
C++11
C++11C++11
C++11
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
 
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
[C++ korea] effective modern c++ study   item 3 understand decltype +이동우[C++ korea] effective modern c++ study   item 3 understand decltype +이동우
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guide
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 

Similar to OOP in C - Inherit (Chinese Version)

CS50 Lecture3
CS50 Lecture3CS50 Lecture3
CS50 Lecture3
昀 李
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
Computer Network Assignment Help
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
Little Tukta Lita
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
htaitk
 
C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
Janani Anbarasan
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
OliverKane3
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
OliverKane3
 
C test
C testC test
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded Python
Yi-Lung Tsai
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
changehee lee
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
Programming Homework Help
 
C++ Language
C++ LanguageC++ Language
C++ Language
Vidyacenter
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
GlobalLogic Ukraine
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
ecomputernotes
 
C# 8 and Beyond
C# 8 and BeyondC# 8 and Beyond
C# 8 and Beyond
Filip Ekberg
 
Objective-Cひとめぐり
Objective-CひとめぐりObjective-Cひとめぐり
Objective-Cひとめぐり
Kenji Kinukawa
 
C Programming
C ProgrammingC Programming
C Programming
Adil Jafri
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
Kho コー。イエー。イエン
 
C++totural file
C++totural fileC++totural file
C++totural file
halaisumit
 

Similar to OOP in C - Inherit (Chinese Version) (20)

CS50 Lecture3
CS50 Lecture3CS50 Lecture3
CS50 Lecture3
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
C test
C testC test
C test
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded Python
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
C# 8 and Beyond
C# 8 and BeyondC# 8 and Beyond
C# 8 and Beyond
 
Objective-Cひとめぐり
Objective-CひとめぐりObjective-Cひとめぐり
Objective-Cひとめぐり
 
C Programming
C ProgrammingC Programming
C Programming
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
C++totural file
C++totural fileC++totural file
C++totural file
 

Recently uploaded

Aggression - Applied Social Psychology - Psychology SuperNotes
Aggression - Applied Social Psychology - Psychology SuperNotesAggression - Applied Social Psychology - Psychology SuperNotes
Aggression - Applied Social Psychology - Psychology SuperNotes
PsychoTech Services
 
1-CIE-IGCSE-Additional-Mathematics-Topical-Past-Paper-Functions.pdf
1-CIE-IGCSE-Additional-Mathematics-Topical-Past-Paper-Functions.pdf1-CIE-IGCSE-Additional-Mathematics-Topical-Past-Paper-Functions.pdf
1-CIE-IGCSE-Additional-Mathematics-Topical-Past-Paper-Functions.pdf
shahul62
 
healthy relationships and building a friendship
healthy relationships and building a friendshiphealthy relationships and building a friendship
healthy relationships and building a friendship
HaydarbekYuldoshev1
 
Assignment 1 (Introductions to Microsoft Power point 2019) kiran.pptx
Assignment 1 (Introductions to Microsoft Power point 2019) kiran.pptxAssignment 1 (Introductions to Microsoft Power point 2019) kiran.pptx
Assignment 1 (Introductions to Microsoft Power point 2019) kiran.pptx
kirannaveed6
 
7 Habits of Highly Effective People.pptx
7 Habits of Highly Effective People.pptx7 Habits of Highly Effective People.pptx
7 Habits of Highly Effective People.pptx
gpangilinan2017
 
ProSocial Behaviour - Applied Social Psychology - Psychology SuperNotes
ProSocial Behaviour - Applied Social Psychology - Psychology SuperNotesProSocial Behaviour - Applied Social Psychology - Psychology SuperNotes
ProSocial Behaviour - Applied Social Psychology - Psychology SuperNotes
PsychoTech Services
 
The Six Working Genius Short Explanation
The Six Working Genius Short ExplanationThe Six Working Genius Short Explanation
The Six Working Genius Short Explanation
abijabar2
 
The Secret Warrior - Help Share a Parent or Loved Ones’ Cancer Diagnosis with...
The Secret Warrior - Help Share a Parent or Loved Ones’ Cancer Diagnosis with...The Secret Warrior - Help Share a Parent or Loved Ones’ Cancer Diagnosis with...
The Secret Warrior - Help Share a Parent or Loved Ones’ Cancer Diagnosis with...
CANSA The Cancer Association of South Africa
 
Understanding of Self - Applied Social Psychology - Psychology SuperNotes
Understanding of Self - Applied Social Psychology - Psychology SuperNotesUnderstanding of Self - Applied Social Psychology - Psychology SuperNotes
Understanding of Self - Applied Social Psychology - Psychology SuperNotes
PsychoTech Services
 

Recently uploaded (9)

Aggression - Applied Social Psychology - Psychology SuperNotes
Aggression - Applied Social Psychology - Psychology SuperNotesAggression - Applied Social Psychology - Psychology SuperNotes
Aggression - Applied Social Psychology - Psychology SuperNotes
 
1-CIE-IGCSE-Additional-Mathematics-Topical-Past-Paper-Functions.pdf
1-CIE-IGCSE-Additional-Mathematics-Topical-Past-Paper-Functions.pdf1-CIE-IGCSE-Additional-Mathematics-Topical-Past-Paper-Functions.pdf
1-CIE-IGCSE-Additional-Mathematics-Topical-Past-Paper-Functions.pdf
 
healthy relationships and building a friendship
healthy relationships and building a friendshiphealthy relationships and building a friendship
healthy relationships and building a friendship
 
Assignment 1 (Introductions to Microsoft Power point 2019) kiran.pptx
Assignment 1 (Introductions to Microsoft Power point 2019) kiran.pptxAssignment 1 (Introductions to Microsoft Power point 2019) kiran.pptx
Assignment 1 (Introductions to Microsoft Power point 2019) kiran.pptx
 
7 Habits of Highly Effective People.pptx
7 Habits of Highly Effective People.pptx7 Habits of Highly Effective People.pptx
7 Habits of Highly Effective People.pptx
 
ProSocial Behaviour - Applied Social Psychology - Psychology SuperNotes
ProSocial Behaviour - Applied Social Psychology - Psychology SuperNotesProSocial Behaviour - Applied Social Psychology - Psychology SuperNotes
ProSocial Behaviour - Applied Social Psychology - Psychology SuperNotes
 
The Six Working Genius Short Explanation
The Six Working Genius Short ExplanationThe Six Working Genius Short Explanation
The Six Working Genius Short Explanation
 
The Secret Warrior - Help Share a Parent or Loved Ones’ Cancer Diagnosis with...
The Secret Warrior - Help Share a Parent or Loved Ones’ Cancer Diagnosis with...The Secret Warrior - Help Share a Parent or Loved Ones’ Cancer Diagnosis with...
The Secret Warrior - Help Share a Parent or Loved Ones’ Cancer Diagnosis with...
 
Understanding of Self - Applied Social Psychology - Psychology SuperNotes
Understanding of Self - Applied Social Psychology - Psychology SuperNotesUnderstanding of Self - Applied Social Psychology - Psychology SuperNotes
Understanding of Self - Applied Social Psychology - Psychology SuperNotes
 

OOP in C - Inherit (Chinese Version)

  • 1. OOP in C Inherit elpam.tw@gmail.com elpam@Taiwan, Taipei, 2008
  • 2. LICENSE ● 本投影片授權方式為: – 姓名標示─非商業性─相同方式分享 3.0  台灣版 – http://creativecommons.org/licenses/by­nc­sa/3.0/tw/ ● 所有的範例程式皆為 Public Domain elpam.tw@gmail.com
  • 3. About This Slides ● All example was build by  – GCC­4.1.3 – GLIB­1.2.10 – GMAKE­3.81 elpam.tw@gmail.com
  • 4. C  Language Review ● Stack – Caller Function's Address – CPU's Register – Local Value ● Heap (run­time heap) – managed by malloc elpam.tw@gmail.com
  • 5. Address Space ● Linux Memory Model – copy from Jserv's  STACK Hacking Hello World HEAP elpam.tw@gmail.com
  • 6. Function Stack Caller's Address Return Address void Func_B() Local Value {     int a,b;    /* do nothing*/ } Low void Func_A() int b { Func_B's Stack int a     char a; Func_A     Func_B(); } char a Func_A's Stack main int main() {     Func_A(); } main's stack .... elpam.tw@gmail.com High
  • 7. After Review ● 請了解 Callee Stack 裡頭會放置那些東西 ● 請了解 Callee Stack Release 後那些變數會被清 除掉 ● 請確定已正確的了解 Stack 與 Heap 的差別 elpam.tw@gmail.com
  • 9. Structure (I) /* 1­1.c */ ● Address + Offset struct A{    int a;    int b; – pa = 0x804a008 }; – pa ­> a == pa + 0 int main() { – pa ­> b == pa + 4  struct A* pa =malloc(sizeof(struct A));   printf("%x,%x,%xn" ,pa, &(pa­>a), &(pa­>b) );   free( pa );  #> ./1­1 } 804a008,804a008,804a00c  #> elpam.tw@gmail.com
  • 10. 所有的東西都是 Memory Address ● ­>  所代表的意義是 offset ,而這一個數值的大 小可以在 Compiler Time 被決定 elpam.tw@gmail.com
  • 11. Structure (II) /* 1­2.c */ struct A{ Size = 8 byte ● pa == pb     int a;   int b; }; – pa ­> c Compile Error struct B{    int a;   int b; – pb ­> c Correct    int c;   Size = 12 byte }; int main() {  struct A* pa =malloc(sizeof(B));  struct B* pb = pa;   pa ­> a = 1;     pa ­> b = 2;  #> ./1­2   pb ­> c = 3; 1,2,3   #>   printf("pb(%d,%d,%d)n" ,pb­>a,pb­>b,pb­>c ); } elpam.tw@gmail.com
  • 12. Pointer 的 Type 與被 allocate 的記憶體大小是沒 有關係的 ● 請注意我們是如何使用 casting elpam.tw@gmail.com
  • 13. /* 1­3.c */ Structure (III) struct A{ int a; ● }; Casting int main() { struct A* pa = malloc( sizeof(struct A) ); struct B{ struct B* pb = pa; short b; struct C* pc = pa; short c; }; pc ­> hi_b = 0x12; pc ­> low_b = 0x34; struct C{ pc ­> hi_c = 0x56; char low_b; pc ­> low_c = 0x78; char hi_b; char low_c; printf("pb­>b(0x%x)  pb­>c(0x%x)n",pb­>b,pb­>c ); char hi_c; printf("pa­>a(0x%x)n",pa­>a ); }; free( pa ); } elpam.tw@gmail.com
  • 14. Structure (III) (cont') ● Memory Casting ● x86 is little­endian pc ­> hi_b = 0x12; pc ­> low_b = 0x34; pc ­> hi_c = 0x56; pc ­> low_c = 0x78;   #>./1­3 pb­>b(0x1234)  pb­>c(0x5678) pa­>a(0x56781234)   #> elpam.tw@gmail.com
  • 16. C++ Review /* 1­6.cpp */ class A{ ● Casting to Parent    public:       int a; }; class B : public A{    public:       int b; }; int main() { B b; b . b  = 2; A(b) . a = 1 } elpam.tw@gmail.com
  • 17. Inherit int main() { ● sizeof(A) =  8  struct B* pb = malloc(sizeof(struct B));   pb ­> parent . a = 0; ● sizeof(B) = 12   pb ­> parent . b = 1;   pb ­> c = 2 /* 1­4.h */ } struct A{    int a;    int b; /* 1­4.c */ Inherit A }; int main() struct B{ {    struct A parent;  struct A* pa =malloc(sizeof(struct B));    int c;    struct B* pb = pa; };   pa ­> a = 0;   pa ­> b = 1;   #>./1­4   pb ­> c = 2; pa(1,2) pb(3) }   #> elpam.tw@gmail.com
  • 18. Inherit & Casting /* 1­5.c */ ● Casting to Parent int main() {  struct B* pb =malloc(sizeof(struct B));   pb ­> c = 3;  struct A* pa = pb; /* 1­5.h */   pa ­> a = 1; struct A{   pa ­> a = 2;    int a;    int b;   printf("pa(%d,%d) pb(%d)n" }; ,pa­>a,pa­>b,pb­>c ); struct B{ }    struct A parent;    int c;   };   #>./1­5 pa(1,2) pb(3)   #> elpam.tw@gmail.com
  • 19. Public Member Value in C++ int main() { class A{   B* b = new B; public:    int a;   b . a = 0;    int b;   b . b = 1; };   b . c = 2; class B : public A { } public:    int c;   }; int main() {   A* a = new b;   a . a = 0;   a . b = 1;   a . c = 2;       /* error */   B(a) . c = 2;  /* correct */ } elpam.tw@gmail.com
  • 20. Object Inherit ● C Language ● C++ struct A{ class A{    int a; public:    int b;    int a; };    int b; struct B{ };    struct A parent; class B : public A {    int c;   public: };    int c;   }; elpam.tw@gmail.com
  • 21. Type Casting ( 形態轉換 ) ● C Language ● C++ – Structure­to­Structure – dynamic_cast ● Address­to­Address – static_cast – reinterpret_cast – const_cast elpam.tw@gmail.com
  • 22. Inherit in C ● C ­> B ­> A /* error memory access */ int main() struct A{ {    int v1;  struct B* pb = malloc(sizeof(B));    int v2;  struct C* pc = pb; /* correct */ };  pc ­> v1 = 0; /* run time error */ struct B{    struct A parent; }    int v1;   }; struct C{    struct B parent; struct C c;    int v1;   struct B* pb = &c;  /* use B's member */ }; struct A* pb = &c;  /* use A's member */ elpam.tw@gmail.com
  • 23. Conclusion  我們可以使用 memory casting 的技巧來描述繼承關係 *  因為缺乏語法 (Compiler) 的保護,所以我們必須 要小心的使用 elpam.tw@gmail.com
  • 24. Questions ● 建構子 (Constructor)  還有需要嗎 ? ● 但 C++ 的繼承還提供了 Virtual Function,  需要 這樣的功能嗎 ?  該如何實作 ? – 下一個投影片將討論這兩個問題 elpam.tw@gmail.com