SlideShare uma empresa Scribd logo
1 de 9
Baixar para ler offline
មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
       ឌ         ៌                                                         Enter Information Technology Center




                                      មមមរៀនទ្យី 3 : សិក្សាពី
                                           Inheritance-I


1.និយមន័យ Inheritance:

      Inheritance គឺជាវ ិធីសាស្រ្តននការបង្កើតង   ើ្នូ វ Class មួយងោយងធវើការទាញយក Properties និ្ Functions រប្់

      Class ដនទងទៀតមកង្បើ ្ា្់ងៅកនុ្ Class រប្់ខ្ួលនងោយមិនចាំាច់បង្កើតង                   ើ្វ ិញង   ើយ។ ឧទាហរណ: Dog
                                                                                                              ៍

      គឺជា្បងភទ ឬមានពូជរប្់វាជា Canine ងហើយចាំង            ោះ Canine គឺជា្បងភទនន Mammal ជាងដើម។ ដូ ងចនោះជាទូ ងៅ

      Clasទាាំ្អ្់តត្តត្តូវានតបកជាងៅជា ្កុម, ថ្នក់, លាំោប់ , ្គួសារ, ជាំពូក, ឬ្បងភទជាងដើម។
                                               ន

      is-a relationship គឺ្តូវានង្បើ្ា្់្ាំរាប់បញ្ជក់អតថន័យងៅឲ Class មួយតដលានងធវើ Inheritance ងចញពី Class
                                                  ា

      ដនទងទៀតតដលជា្កុម ឬជា្បងភទរប្់វា។

      ឧទាហរណ:
            ៍

              > Camry is a ( is-a ) kind of car, and Car is a ( is-a ) kind of Vehicle.

              > Burger is a ( is-a ) kind of bread, and Bread is ( is-a ) kind of food.

              > HTC is a ( is-a ) kind of mobile phone, and Mobile Phone is a ( is-a ) kind of Phone.

      ងពលតដលមានការបង្ហញឬនិយាយពី Class មួយតដលវាគឺជា្បងភទនន Class ណាមួ យងផេ្ងទៀតងៅកនុ្ OOP
                     ា

      ងគអាចងៅថ្ន Class ងនោះគឺជា specialization នន Class ដនទងទៀត។

      ឧទាហរណ:
            ៍

              > Car is a special kind of Vehicle.

              > Bread is special kind of food.

              > Mobile Phone is a special kind of Phone.


2. Inheritance and Derivation:

      Derivation គឹជាការទាញយក Property និ្ Function ងចញពី Class ងផេ្ងទៀតតដលានងធវើ Inheritance ។ ចាំង                       ោះ

      Dog តដលវាមាន្បងភទជា Mammal ដូងចនោះវានឹ្ទទួ ល (Derive) ្គប់ Features ទាាំ្អ្់រប្់ Mammal តដល

      មាន។ ្គប់ Mammals ទាាំ្អ្់គឺអាចងធវើការ Move និ្ Breathe Air ានផ្តដរ ដូងចនោះ Class Dog គឺអាចងធវើការ

      Move និ្ Breathe Air ានងោយ្វ័យ្បវតតិងោយមិនចាំាច់បង្កើតង                  ើ្ងោយខ្លួនឯ្ង        ើយ។ កនុ្ង   ោះ Class

      Dog អាចបតនថមនូ វ Features ផ្ទល់ខ្ួលនរប្់វាបតនថមងទៀតដូចជា Barking, Wagging, ងហើយងយើ្អាចងធវើការតប្តចក
                                  ទ

      Class Dog ងៅជា្បងភទងផេ្ៗជាង្ចើនងទៀតដូចជា Working Dogs, Sporting Dogs, និ្ Terriers។ ចាំង                    ោះ

      Sporting Dogs ងយើ្អាចងធវើការបាំ តបកងៅជា Retrievers, Spaniels,… ។ រ ីឯ Class Dog ដនទងទៀតងយើ្ក៏ អាច

      Specialized វាបតនថមងទៀតដូ ចជា Retrievers អាចតប្តចកងៅជា Labradors និ្ Goldens ។

      ដូងចនោះ Golden គឺជា្បងភទនន Retriver តដលគឺ ជា្បងភទ Sporting Dog ងហើយគឺជា Dog តដល្ថិតងៅកនុ្្បងភទ

      Mamal តដលវា្ថិតងៅកនុ្្បងភទ Animal ។




C++ OOP                                             www.enteritc.com                                                       17
Enter Information Technology Center                                               មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
                                                                                          ឌ         ៌



                                                          Animal

                                                            សត្វ




                                         Mammal                           Reptile

                                         ថនិ កសត្វ                        ថ្នក់
                                                                            ា     ន
                                                                                  ូៅ




                       Horse                                Dog

                          សសេះ                              ឆ្កែ




                     Working                             Sporting                         Terriers

                     ឆ្កែស្វើការ                          ឆ្កែកីឡា                       ឆ្កែសដញសត្វ




                                        Retrievers                       Spaniels

                                        ឆ្កែពាំរបស់                    ឆ្កែបង្ហញមូ ដ
                                                                              ា




                    Labradors                            Goldens

                  ពូ ជស    ឿងឬសមៅ                        ពូ ជមាស



      ជាមួយនឹ្ C++ OOP គឺពាយាមងធវើការបង្ហញនូ វទាំ
                                        ា                  ក់ ទាំន្ទាាំ្ងនោះងដើមបីងោយង្ហយស្្ួលកនុ្បង្កើត Class

      ងោយទាញយក (Derive) Features ងចញពី Class ច្់តដលមានស្សាប់ ។ Derivation គឺជាវ ិធីសាស្រ្តននការបង្ហញពី
                                                                                                  ា

      is-a Relationship ងោយងយើ្អាចបង្កើត Class Dog ថមីងហើយ Derive ពី Class Mammal ដូងចនោះងយើ្មិនចាំាច់្ាប់

      ឲ Dog ងធវើការ Move ង   ោះងទង្    ោះវាអាច Move ងោយ្វ័យ្បវតតិងោយវាាន inherit ងចញពី Mammal ។



      Class ថមីតដល្តូវបង្កើតង      ើ្ងហើយ Add នូវ Functionality ( Property + Function) រប្់ Class ងផេ្ងទៀតតដលមាន

      ងហើយគឺអាចនិយាយថ្ន Class ថមីាន Derive ពី Original Class ។ Original Class ្តូវានងៅថ្នជា Base Class នន

      Class ថមី។ ្ប្ិនងបើ Class Dog ានងធវើការ Derive ងចញពី Class Mammal ង           ោះមានន័យថ្ន Class Mammal គឺជា

      Base Class នន Dog ។ ចាំង      ោះ Derive Class វ ិញគឺ ជា Supersets នន Base Class រប្់ពួកវា។

      ជាទូងៅ្គប់ Base Class ទាាំ្អ្់អាចមាន Derive Class ងលើ្ពីមួយ ឧទាហរណដូ ចជា Dog, Cat, Horse គឺ្ុទធ
                                                                        ៍

      តតជា្បងភទ Mammal ដូងចនោះពួកវាទាាំ្អ្់ជា Derive ពី Class Mammal តតមួ យានផ្តដរ។

18                                                www.enteritc.com                                        C++ OOP
មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
       ឌ         ៌                                               Enter Information Technology Center


3. Syntax of Derivation:

        Syntax:
              class <derivedClass> : <accessType> <baseClass>

        Ex:
                  class Dog : public Mammal



      ងពលតដលងយើ្ងធវើការ declare នូវ Class មួយ ងយើ្អាចងធវើការបង្ហញពីការ Derive រប្់វាងធវើការ្រង្រ Colon (:)
                                                               ា

      ប ទ ប់ពីង្ោះ Class ប ទ ប់ មក្បងភទនន Access Type (តដលមានដូចជា public ឬ private) និ្ង្ោះរប្់ Base
                ម                                                                         ម

      Class ។

      Ex:

        //Simple Inheritance
        #include<iostream>
        using namespace std;
        enum BREED {GOLDEN,CAIRN,DANDIE,SHETLAND,DOBEMAN,LAB};
        class Mammal
        {
              public:
                     Mammal();
                     ~Mammal();
                     int GetAge() const;
                     void SetAge(int);
                     int GetWeight() const;
                     void SetWeight();
                     void Speak() const;
                     void Slepp() const;
              protected:
                     int itsAge;
                     int itsWeight;
        };
        class Dog:public Mammal
        {
              public:
                     Dog();
                     ~Dog();
                     BREED GetBreed() const;
                     void SetBreed(BREED);
                     void WagTail();
                     void BegForFood();
              protected:
                     BREED itsBreed;
        };

C++ OOP                                     www.enteritc.com                                           19
Enter Information Technology Center                                             មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
                                                                                        ឌ         ៌


4. Private Versus Protected:

      ចាំង   ោះ Private Member គឺមិនអាចង្បើ្ា្់ងៅខា្ង្ៅ Class រប្់វាង        ោះងទ មានន័ យថ្ន្ប្ិនងបើ Member ជា

      Private ង   ោះវាអាចង្បើ ្ា្់ងៅកនុ្តត Class ខ្លួនឯ្បុងណាោះ
                                                             ណ

      ងោយមិនអាចយកងៅង្បើ្ា្់ងៅខា្ង្ៅានង                  ើយ ្ូមបីតត Derive Class ក៏ងោយ។

      ងដើមបីងោោះស្សាយនូ វបញ្ជងនោះ គឺ្តូវងធវើការតកត្ប Member ទាាំ្ង
                            ា                                        ោះងៅជា Protected Member វ ិញមានន័យថ្ន

      Protected Member គឺអនុញ្ជតិឲ Member អាចង្បើ ្ា្់ានជាមួ យនឹ្ Class រប្់ខ្ួលនវា
                              ា

      និ្អាចង្បើ ្ា្់ានជាមួ យនឹ្ Derive Class រប្់វាានផ្តដរ។

      ្រុបមកជាមួ យនឹ្ Members គឺ្តូវានង្បើ ្ា្់ជាមួយនឹ្ Access Specifiers ចាំនួន 3 រួមមាន:

      > Public Member         : គឺជា Member ្ាំរាប់ឲ Class ខ្លួនវាង្បើ ្ា្់ផ្ និ្្ាំរាប់ Class

                              ដនទងទៀតង្បើ ្ា្់ានផ្តដរ។

      > Private Member        : គឺជា Member ្ាំរាប់ឲ Class ខ្លួនង្បើ ្ា្់តតបុងណាោះ ងោយមិនអនុញ្ជតិ Class
                                                                                ណ             ា

                              ដនទងទៀតង្បើ ្ា្់ានង       ើយ។

      > Protected Member      : គឺជា Member ្ាំរាប់ឲ Class ខ្លួនង្បើ ្ា្់តតបុងណាោះ ងហើយនឹ្ Derive Class
                                                                                ណ

                              រប្់វាតតបុ ងណាោះ ងោយមិនអនុញ្ជតិ Class ដនទងទៀតង្បើ្ា្់ានង
                                            ណ             ា                                      ើយ។

      Ex:

        //Using a derived object
        #include<iostream>
        using namespace std;
        enum BREED {GOLDEN,CAIRN,DANDIE,SHETLAND,DOBEMAN,LAB};
        class Mammal
        {
              public:
                   Mammal():itsAge(2),itsWeight(5){}
                   ~Mammal(){}
                      int GetAge() const {return itsAge;}
                      void SetAge(int age){itsAge = age;}
                      int GetWeight() const{return itsWeight;}
                      void SetWeight(int
        weight){itsWeight=weight;}
                      void Speak() const{cout<<"Mammal
        sound!n";}
                      void Sleep() const{cout<<"shhh.I'm
        sleeping.n";}
              protected:
                     int itsAge;
                     int itsWeight;
        };




20                                             www.enteritc.com                                        C++ OOP
មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
       ឌ         ៌                                                 Enter Information Technology Center


        class Dog:public Mammal
        {
              public:
                      Dog():itsBreed(GOLDEN){}
                     ~Dog(){}
                      BREED GetBreed() const{return itsBreed;}
                      void SetBreed(BREED
        breed){itsBreed=breed;}
                      void WagTail()const{cout<<"Tail
        wagging...n";}
                      void BegForFood()const{cout<<"Beggin for
        food...n";}
              protected:
                     BREED itsBreed;
        };
        int main(){
              Dog Fido;
              Fido.Speak();
              Fido.WagTail();
              cout<<"Fido is "<<Fido.GetAge()<<" years
        old"<<endl;
              cin.get();
              return 0;
        }



5. Inheritance with Constructors and Destructors:

      Dog Object គឺជា្បងភទ Mammal Object តដលវាមាន is-a relationship ្ាំរាប់ទាំ   ក់ទាំន្ជាមួយគ្ន
                                                                                               ន

      ងហើយកនុ្ង   ោះ្ប្ិនងបើមាន Constructor ងៅកនុ្ Base Class (Mammal Class) និ្កនុ្ Derive Class (Dog Class)

      ផ្ង   ោះ ងៅងពល Constructor រប្់ Dog ្តូវាន Call ង     ោះវានឹ្ងៅ Call ងៅ Constructor រប្់ Base Class គឺ

      Mammal Class ជាមុន្ិនងទើប ដាំងនើ ការនន Constructor រប្់ Dog តាមង្កាយ។ ផទុយងៅវ ិញងៅងពលពួកវា Call

      ងៅ Destructor វ ិញគឺ ្តូវ Call ងៅ Dog Destructor ជាមុន្ិន ងទើប Call ងៅ Mammal Destructor

      តាមង្កាយវ ិញ។ ្រុបមក Constructor និ្ Destructor រប្់ Class ទាាំ្ឡាយតដលងធវើ Inheritance គឺនឹ្្តូវាន

      Call រាល់ងលើក ងៅងពលតដលវាដាំងនើ ការ។




C++ OOP                                      www.enteritc.com                                             21
Enter Information Technology Center                      មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
                                                                 ឌ         ៌


      Ex:

       //Constructors and destructors called.
       #include<iostream>
       using namespace std;
       enum BREED {GOLDEN,CAIRN,DANDIE,SHETLAND,DOBEMAN,LAB};
       class Mammal
       {
             public:
                  Mammal();
                  ~Mammal();
                     int GetAge() const {return itsAge;}
                     void SetAge(int age){itsAge = age;}
                     int GetWeight() const{return itsWeight;}
                     void SetWeight(int
       weight){itsWeight=weight;}
                     void Speak() const{cout<<"Mammal
       sound!n";}
                     void Sleep() const{cout<<"shhh.I'm
       sleeping.n";}
             protected:
                    int itsAge;
                    int itsWeight;
       };
       class Dog:public Mammal
       {
             public:
                     Dog();
                    ~Dog();
                     BREED GetBreed() const{return itsBreed;}
                     void SetBreed(BREED
       breed){itsBreed=breed;}
                     void WagTail()const{cout<<"Tail
       wagging...n";}
                     void BegForFood()const{cout<<"Beggin for
       food...n";}
             protected:
                    BREED itsBreed;
       };
       Mammal::Mammal():itsAge(3),itsWeight(5){
             cout<<"Mammal constructor..."<<endl;
       }
       Mammal::~Mammal(){
             cout<<"Mammal destructor..."<<endl;
       }



22                                     www.enteritc.com                     C++ OOP
មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
       ឌ         ៌                                               Enter Information Technology Center




        Dog::Dog():itsBreed(GOLDEN){
              cout<<"Dog constructor..."<<endl;
        }
        Dog::~Dog(){
              cout<<"Dog destructor..."<<endl;
        }
        int main(){
              Dog Fido;
              Fido.Speak();
              Fido.WagTail();
              cout<<"Fido is "<<Fido.GetAge()<<" years
        old"<<endl;
              cin.get();
              return 0;
        }


6. Passing Arguments to Base Constructors:

      វាក៏អាចដាំងនើការានផ្តដរ្ប្ិនងបើ ងយើ្្តូវការ initialize values ងៅកនុ្ Base Constructor ។ ឧទាហរណថ្ន
                                                                                                    ៍

      ងយើ្ច្់ ងធវើ Overload Constructor នន Mammal Class ងោយយក age ជាតាំនលជាក់ លាក់ និ្ Overload Dog

      Constructor ងោយយក Breed ។ វ ិធីសាស្រ្តខា្ង្កាមនឹ ្បង្ហញពី ការ ចប់យកនូ វ age និ្ weight parameters
                                                           ា

      ងោយ Pass ងៅកនុ្ Constructor Mammal ។

      Ex:

        //Constructors and destructors called.
        #include<iostream>
        using namespace std;
        enum BREED {GOLDEN,CAIRN,DANDIE,SHETLAND,DOBERMAN,LAB};
        class Mammal
        {
              public:
                   Mammal();
                   Mammal(int age);
                   ~Mammal();
                      int GetAge() const {return itsAge;}
                      void SetAge(int age){itsAge = age;}
                      int GetWeight() const{return itsWeight;}
                      void SetWeight(int
        weight){itsWeight=weight;}
                      void Speak() const{cout<<"Mammal
        sound!n";}
                      void Sleep() const{cout<<"shhh.I'm
        sleeping.n";}

C++ OOP                                     www.enteritc.com                                              23
Enter Information Technology Center                      មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
                                                                 ឌ         ៌


                protected:
                       int itsAge;
                       int itsWeight;
       };
       class Dog:public Mammal
       {      public:
                     Dog();
                     Dog(int age);
                     Dog(int age,int weight);
                     Dog(int age,BREED breed);
                     Dog(int age,int weight,BREED breed);
                    ~Dog();
                     BREED GetBreed() const{return itsBreed;}
                     void SetBreed(BREED
       breed){itsBreed=breed;}
                     void WagTail()const{cout<<"Tail
       wagging...n";}
                     void BegForFood()const{cout<<"Beggin for
       food...n";}
             protected:
                    BREED itsBreed;
       };
       Mammal::Mammal():itsAge(1),itsWeight(5){
             cout<<"Mammal constructor..."<<endl;
       }
       Mammal::Mammal(int age):itsAge(age),itsWeight(5){
             cout<<"Mammal destructor..."<<endl;
       }
       Mammal::~Mammal(){
             cout<<"Mammal destructor..."<<endl;
       }
       Dog::Dog():Mammal(),itsBreed(GOLDEN){
             cout<<"Dog constructor..."<<endl;
       }
       Dog::Dog(int age):Mammal(age),itsBreed(GOLDEN){
             cout<<"Dog(int) constructor..."<<endl;
       }
       Dog::Dog(int age,int
       weight):Mammal(age),itsBreed(GOLDEN){
             itsWeight=weight;
             cout<<"Dog(int,int) constructor..."<<endl;
       }
       Dog::Dog(int age,int weight,BREED
       breed):Mammal(age),itsBreed(breed){
             itsWeight=weight;
             cout<<"Dog(int,int, BREED)constructor..."<<endl;}

24                                     www.enteritc.com                     C++ OOP
មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
       ឌ         ៌                              Enter Information Technology Center



      Dog::Dog(int age,BREED
      breed):Mammal(age),itsBreed(breed){
            cout<<"Dog(int,BREED) constructor..."<<endl;
      }
      Dog::~Dog(){
            cout<<"Dog constructor..."<<endl;
      }
      int main(){
            Dog Fido;
            Dog rover(5);
            Dog buster(6,8);
            Dog yorkie(3,GOLDEN);
            Dog dobbie(4,20,DOBERMAN);
            Fido.Speak();
            rover.WagTail();
            cout<<"Yorki is "<<yorkie.GetAge()<<" years
      old"<<endl;
            cout<<"Dobbie weighs "<<dobbie.GetWeight()<<"
      pounds"<<endl;
            cin.get();
            return 0;
      }




C++ OOP                      www.enteritc.com                                    25

Mais conteúdo relacionado

Destaque

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destaque (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

C++ OOP chapter 3

  • 1. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center មមមរៀនទ្យី 3 : សិក្សាពី Inheritance-I 1.និយមន័យ Inheritance: Inheritance គឺជាវ ិធីសាស្រ្តននការបង្កើតង ើ្នូ វ Class មួយងោយងធវើការទាញយក Properties និ្ Functions រប្់ Class ដនទងទៀតមកង្បើ ្ា្់ងៅកនុ្ Class រប្់ខ្ួលនងោយមិនចាំាច់បង្កើតង ើ្វ ិញង ើយ។ ឧទាហរណ: Dog ៍ គឺជា្បងភទ ឬមានពូជរប្់វាជា Canine ងហើយចាំង ោះ Canine គឺជា្បងភទនន Mammal ជាងដើម។ ដូ ងចនោះជាទូ ងៅ Clasទាាំ្អ្់តត្តត្តូវានតបកជាងៅជា ្កុម, ថ្នក់, លាំោប់ , ្គួសារ, ជាំពូក, ឬ្បងភទជាងដើម។ ន is-a relationship គឺ្តូវានង្បើ្ា្់្ាំរាប់បញ្ជក់អតថន័យងៅឲ Class មួយតដលានងធវើ Inheritance ងចញពី Class ា ដនទងទៀតតដលជា្កុម ឬជា្បងភទរប្់វា។ ឧទាហរណ: ៍ > Camry is a ( is-a ) kind of car, and Car is a ( is-a ) kind of Vehicle. > Burger is a ( is-a ) kind of bread, and Bread is ( is-a ) kind of food. > HTC is a ( is-a ) kind of mobile phone, and Mobile Phone is a ( is-a ) kind of Phone. ងពលតដលមានការបង្ហញឬនិយាយពី Class មួយតដលវាគឺជា្បងភទនន Class ណាមួ យងផេ្ងទៀតងៅកនុ្ OOP ា ងគអាចងៅថ្ន Class ងនោះគឺជា specialization នន Class ដនទងទៀត។ ឧទាហរណ: ៍ > Car is a special kind of Vehicle. > Bread is special kind of food. > Mobile Phone is a special kind of Phone. 2. Inheritance and Derivation: Derivation គឹជាការទាញយក Property និ្ Function ងចញពី Class ងផេ្ងទៀតតដលានងធវើ Inheritance ។ ចាំង ោះ Dog តដលវាមាន្បងភទជា Mammal ដូងចនោះវានឹ្ទទួ ល (Derive) ្គប់ Features ទាាំ្អ្់រប្់ Mammal តដល មាន។ ្គប់ Mammals ទាាំ្អ្់គឺអាចងធវើការ Move និ្ Breathe Air ានផ្តដរ ដូងចនោះ Class Dog គឺអាចងធវើការ Move និ្ Breathe Air ានងោយ្វ័យ្បវតតិងោយមិនចាំាច់បង្កើតង ើ្ងោយខ្លួនឯ្ង ើយ។ កនុ្ង ោះ Class Dog អាចបតនថមនូ វ Features ផ្ទល់ខ្ួលនរប្់វាបតនថមងទៀតដូចជា Barking, Wagging, ងហើយងយើ្អាចងធវើការតប្តចក ទ Class Dog ងៅជា្បងភទងផេ្ៗជាង្ចើនងទៀតដូចជា Working Dogs, Sporting Dogs, និ្ Terriers។ ចាំង ោះ Sporting Dogs ងយើ្អាចងធវើការបាំ តបកងៅជា Retrievers, Spaniels,… ។ រ ីឯ Class Dog ដនទងទៀតងយើ្ក៏ អាច Specialized វាបតនថមងទៀតដូ ចជា Retrievers អាចតប្តចកងៅជា Labradors និ្ Goldens ។ ដូងចនោះ Golden គឺជា្បងភទនន Retriver តដលគឺ ជា្បងភទ Sporting Dog ងហើយគឺជា Dog តដល្ថិតងៅកនុ្្បងភទ Mamal តដលវា្ថិតងៅកនុ្្បងភទ Animal ។ C++ OOP www.enteritc.com 17
  • 2. Enter Information Technology Center មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Animal សត្វ Mammal Reptile ថនិ កសត្វ ថ្នក់ ា ន ូៅ Horse Dog សសេះ ឆ្កែ Working Sporting Terriers ឆ្កែស្វើការ ឆ្កែកីឡា ឆ្កែសដញសត្វ Retrievers Spaniels ឆ្កែពាំរបស់ ឆ្កែបង្ហញមូ ដ ា Labradors Goldens ពូ ជស ឿងឬសមៅ ពូ ជមាស ជាមួយនឹ្ C++ OOP គឺពាយាមងធវើការបង្ហញនូ វទាំ ា ក់ ទាំន្ទាាំ្ងនោះងដើមបីងោយង្ហយស្្ួលកនុ្បង្កើត Class ងោយទាញយក (Derive) Features ងចញពី Class ច្់តដលមានស្សាប់ ។ Derivation គឺជាវ ិធីសាស្រ្តននការបង្ហញពី ា is-a Relationship ងោយងយើ្អាចបង្កើត Class Dog ថមីងហើយ Derive ពី Class Mammal ដូងចនោះងយើ្មិនចាំាច់្ាប់ ឲ Dog ងធវើការ Move ង ោះងទង្ ោះវាអាច Move ងោយ្វ័យ្បវតតិងោយវាាន inherit ងចញពី Mammal ។ Class ថមីតដល្តូវបង្កើតង ើ្ងហើយ Add នូវ Functionality ( Property + Function) រប្់ Class ងផេ្ងទៀតតដលមាន ងហើយគឺអាចនិយាយថ្ន Class ថមីាន Derive ពី Original Class ។ Original Class ្តូវានងៅថ្នជា Base Class នន Class ថមី។ ្ប្ិនងបើ Class Dog ានងធវើការ Derive ងចញពី Class Mammal ង ោះមានន័យថ្ន Class Mammal គឺជា Base Class នន Dog ។ ចាំង ោះ Derive Class វ ិញគឺ ជា Supersets នន Base Class រប្់ពួកវា។ ជាទូងៅ្គប់ Base Class ទាាំ្អ្់អាចមាន Derive Class ងលើ្ពីមួយ ឧទាហរណដូ ចជា Dog, Cat, Horse គឺ្ុទធ ៍ តតជា្បងភទ Mammal ដូងចនោះពួកវាទាាំ្អ្់ជា Derive ពី Class Mammal តតមួ យានផ្តដរ។ 18 www.enteritc.com C++ OOP
  • 3. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center 3. Syntax of Derivation: Syntax: class <derivedClass> : <accessType> <baseClass> Ex: class Dog : public Mammal ងពលតដលងយើ្ងធវើការ declare នូវ Class មួយ ងយើ្អាចងធវើការបង្ហញពីការ Derive រប្់វាងធវើការ្រង្រ Colon (:) ា ប ទ ប់ពីង្ោះ Class ប ទ ប់ មក្បងភទនន Access Type (តដលមានដូចជា public ឬ private) និ្ង្ោះរប្់ Base ម ម Class ។ Ex: //Simple Inheritance #include<iostream> using namespace std; enum BREED {GOLDEN,CAIRN,DANDIE,SHETLAND,DOBEMAN,LAB}; class Mammal { public: Mammal(); ~Mammal(); int GetAge() const; void SetAge(int); int GetWeight() const; void SetWeight(); void Speak() const; void Slepp() const; protected: int itsAge; int itsWeight; }; class Dog:public Mammal { public: Dog(); ~Dog(); BREED GetBreed() const; void SetBreed(BREED); void WagTail(); void BegForFood(); protected: BREED itsBreed; }; C++ OOP www.enteritc.com 19
  • 4. Enter Information Technology Center មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ 4. Private Versus Protected: ចាំង ោះ Private Member គឺមិនអាចង្បើ្ា្់ងៅខា្ង្ៅ Class រប្់វាង ោះងទ មានន័ យថ្ន្ប្ិនងបើ Member ជា Private ង ោះវាអាចង្បើ ្ា្់ងៅកនុ្តត Class ខ្លួនឯ្បុងណាោះ ណ ងោយមិនអាចយកងៅង្បើ្ា្់ងៅខា្ង្ៅានង ើយ ្ូមបីតត Derive Class ក៏ងោយ។ ងដើមបីងោោះស្សាយនូ វបញ្ជងនោះ គឺ្តូវងធវើការតកត្ប Member ទាាំ្ង ា ោះងៅជា Protected Member វ ិញមានន័យថ្ន Protected Member គឺអនុញ្ជតិឲ Member អាចង្បើ ្ា្់ានជាមួ យនឹ្ Class រប្់ខ្ួលនវា ា និ្អាចង្បើ ្ា្់ានជាមួ យនឹ្ Derive Class រប្់វាានផ្តដរ។ ្រុបមកជាមួ យនឹ្ Members គឺ្តូវានង្បើ ្ា្់ជាមួយនឹ្ Access Specifiers ចាំនួន 3 រួមមាន: > Public Member : គឺជា Member ្ាំរាប់ឲ Class ខ្លួនវាង្បើ ្ា្់ផ្ និ្្ាំរាប់ Class ដនទងទៀតង្បើ ្ា្់ានផ្តដរ។ > Private Member : គឺជា Member ្ាំរាប់ឲ Class ខ្លួនង្បើ ្ា្់តតបុងណាោះ ងោយមិនអនុញ្ជតិ Class ណ ា ដនទងទៀតង្បើ ្ា្់ានង ើយ។ > Protected Member : គឺជា Member ្ាំរាប់ឲ Class ខ្លួនង្បើ ្ា្់តតបុងណាោះ ងហើយនឹ្ Derive Class ណ រប្់វាតតបុ ងណាោះ ងោយមិនអនុញ្ជតិ Class ដនទងទៀតង្បើ្ា្់ានង ណ ា ើយ។ Ex: //Using a derived object #include<iostream> using namespace std; enum BREED {GOLDEN,CAIRN,DANDIE,SHETLAND,DOBEMAN,LAB}; class Mammal { public: Mammal():itsAge(2),itsWeight(5){} ~Mammal(){} int GetAge() const {return itsAge;} void SetAge(int age){itsAge = age;} int GetWeight() const{return itsWeight;} void SetWeight(int weight){itsWeight=weight;} void Speak() const{cout<<"Mammal sound!n";} void Sleep() const{cout<<"shhh.I'm sleeping.n";} protected: int itsAge; int itsWeight; }; 20 www.enteritc.com C++ OOP
  • 5. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center class Dog:public Mammal { public: Dog():itsBreed(GOLDEN){} ~Dog(){} BREED GetBreed() const{return itsBreed;} void SetBreed(BREED breed){itsBreed=breed;} void WagTail()const{cout<<"Tail wagging...n";} void BegForFood()const{cout<<"Beggin for food...n";} protected: BREED itsBreed; }; int main(){ Dog Fido; Fido.Speak(); Fido.WagTail(); cout<<"Fido is "<<Fido.GetAge()<<" years old"<<endl; cin.get(); return 0; } 5. Inheritance with Constructors and Destructors: Dog Object គឺជា្បងភទ Mammal Object តដលវាមាន is-a relationship ្ាំរាប់ទាំ ក់ទាំន្ជាមួយគ្ន ន ងហើយកនុ្ង ោះ្ប្ិនងបើមាន Constructor ងៅកនុ្ Base Class (Mammal Class) និ្កនុ្ Derive Class (Dog Class) ផ្ង ោះ ងៅងពល Constructor រប្់ Dog ្តូវាន Call ង ោះវានឹ្ងៅ Call ងៅ Constructor រប្់ Base Class គឺ Mammal Class ជាមុន្ិនងទើប ដាំងនើ ការនន Constructor រប្់ Dog តាមង្កាយ។ ផទុយងៅវ ិញងៅងពលពួកវា Call ងៅ Destructor វ ិញគឺ ្តូវ Call ងៅ Dog Destructor ជាមុន្ិន ងទើប Call ងៅ Mammal Destructor តាមង្កាយវ ិញ។ ្រុបមក Constructor និ្ Destructor រប្់ Class ទាាំ្ឡាយតដលងធវើ Inheritance គឺនឹ្្តូវាន Call រាល់ងលើក ងៅងពលតដលវាដាំងនើ ការ។ C++ OOP www.enteritc.com 21
  • 6. Enter Information Technology Center មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Ex: //Constructors and destructors called. #include<iostream> using namespace std; enum BREED {GOLDEN,CAIRN,DANDIE,SHETLAND,DOBEMAN,LAB}; class Mammal { public: Mammal(); ~Mammal(); int GetAge() const {return itsAge;} void SetAge(int age){itsAge = age;} int GetWeight() const{return itsWeight;} void SetWeight(int weight){itsWeight=weight;} void Speak() const{cout<<"Mammal sound!n";} void Sleep() const{cout<<"shhh.I'm sleeping.n";} protected: int itsAge; int itsWeight; }; class Dog:public Mammal { public: Dog(); ~Dog(); BREED GetBreed() const{return itsBreed;} void SetBreed(BREED breed){itsBreed=breed;} void WagTail()const{cout<<"Tail wagging...n";} void BegForFood()const{cout<<"Beggin for food...n";} protected: BREED itsBreed; }; Mammal::Mammal():itsAge(3),itsWeight(5){ cout<<"Mammal constructor..."<<endl; } Mammal::~Mammal(){ cout<<"Mammal destructor..."<<endl; } 22 www.enteritc.com C++ OOP
  • 7. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center Dog::Dog():itsBreed(GOLDEN){ cout<<"Dog constructor..."<<endl; } Dog::~Dog(){ cout<<"Dog destructor..."<<endl; } int main(){ Dog Fido; Fido.Speak(); Fido.WagTail(); cout<<"Fido is "<<Fido.GetAge()<<" years old"<<endl; cin.get(); return 0; } 6. Passing Arguments to Base Constructors: វាក៏អាចដាំងនើការានផ្តដរ្ប្ិនងបើ ងយើ្្តូវការ initialize values ងៅកនុ្ Base Constructor ។ ឧទាហរណថ្ន ៍ ងយើ្ច្់ ងធវើ Overload Constructor នន Mammal Class ងោយយក age ជាតាំនលជាក់ លាក់ និ្ Overload Dog Constructor ងោយយក Breed ។ វ ិធីសាស្រ្តខា្ង្កាមនឹ ្បង្ហញពី ការ ចប់យកនូ វ age និ្ weight parameters ា ងោយ Pass ងៅកនុ្ Constructor Mammal ។ Ex: //Constructors and destructors called. #include<iostream> using namespace std; enum BREED {GOLDEN,CAIRN,DANDIE,SHETLAND,DOBERMAN,LAB}; class Mammal { public: Mammal(); Mammal(int age); ~Mammal(); int GetAge() const {return itsAge;} void SetAge(int age){itsAge = age;} int GetWeight() const{return itsWeight;} void SetWeight(int weight){itsWeight=weight;} void Speak() const{cout<<"Mammal sound!n";} void Sleep() const{cout<<"shhh.I'm sleeping.n";} C++ OOP www.enteritc.com 23
  • 8. Enter Information Technology Center មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ protected: int itsAge; int itsWeight; }; class Dog:public Mammal { public: Dog(); Dog(int age); Dog(int age,int weight); Dog(int age,BREED breed); Dog(int age,int weight,BREED breed); ~Dog(); BREED GetBreed() const{return itsBreed;} void SetBreed(BREED breed){itsBreed=breed;} void WagTail()const{cout<<"Tail wagging...n";} void BegForFood()const{cout<<"Beggin for food...n";} protected: BREED itsBreed; }; Mammal::Mammal():itsAge(1),itsWeight(5){ cout<<"Mammal constructor..."<<endl; } Mammal::Mammal(int age):itsAge(age),itsWeight(5){ cout<<"Mammal destructor..."<<endl; } Mammal::~Mammal(){ cout<<"Mammal destructor..."<<endl; } Dog::Dog():Mammal(),itsBreed(GOLDEN){ cout<<"Dog constructor..."<<endl; } Dog::Dog(int age):Mammal(age),itsBreed(GOLDEN){ cout<<"Dog(int) constructor..."<<endl; } Dog::Dog(int age,int weight):Mammal(age),itsBreed(GOLDEN){ itsWeight=weight; cout<<"Dog(int,int) constructor..."<<endl; } Dog::Dog(int age,int weight,BREED breed):Mammal(age),itsBreed(breed){ itsWeight=weight; cout<<"Dog(int,int, BREED)constructor..."<<endl;} 24 www.enteritc.com C++ OOP
  • 9. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center Dog::Dog(int age,BREED breed):Mammal(age),itsBreed(breed){ cout<<"Dog(int,BREED) constructor..."<<endl; } Dog::~Dog(){ cout<<"Dog constructor..."<<endl; } int main(){ Dog Fido; Dog rover(5); Dog buster(6,8); Dog yorkie(3,GOLDEN); Dog dobbie(4,20,DOBERMAN); Fido.Speak(); rover.WagTail(); cout<<"Yorki is "<<yorkie.GetAge()<<" years old"<<endl; cout<<"Dobbie weighs "<<dobbie.GetWeight()<<" pounds"<<endl; cin.get(); return 0; } C++ OOP www.enteritc.com 25