SlideShare a Scribd company logo
1 of 10
Download to read offline
មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
       ឌ         ៌                                                                Enter Information Technology Center




                                              មមមរៀនទ្យី 2: សិក្សាពី
                                        Classes and Objects-II


1.ការបម្កីត Data Member ជា Private:

      ជាក្បួនទូ ទៅននការបទ្កើត Class គឺទ            ើ្គួ រតតទធវើការក្ំនត់ ឲ Data Members ជា Private

      ទ     ើ   ទ្បើវ ិធីសាស្ត្រមួ    ទផេ្ទទៀតក្នុ្ការ Access ទៅកាន់ Member ទ              ោះ។ វ ិធីសាស្ត្រក្ុន្ការ Access ទៅកាន់

      Private Member គឺទ             ើ្ បទ្កើត Public Function មួ   ្តូវបានទៅថា Accessor Methods

      ទោ        ទធវើការទ្បើ្បា្់វាទ ើមបី Set និ្ Get ទៅកាន់ Private Member ទ               ោះ។ ចំទ   ោះ Accessor Methods ក្៏ជា

      Member Function រប្់ Class ផ្ត រ ត លទ្បើ្ំរាប់ទធវើការ Input (Set) ចូលទៅកាន់ Private Member និ្

      Output (Get) ទចញពី Private Member វ ិញ។


      Accessor Functions អាចឲទ               ើ្ទធវើការតប្តចក្រវា្ How the data is store ជាមួ          នឹ ្ How it is used មានន័     ថា

      ទ     ើ្អាចទធវើការផ្ល្់បូររ How to data is stored ទៅទពលទ្កា
                          ា                                                  បាន ទោ          ពុំ ចំបាច់ទធវើការ

      ្រទ្រទ          ើ្វ ិញនូ វទផេ្ទទៀត្ំរាប់ទ្បើ ្បា្់ជាមួ        នឹ្ data ទ   ោះទ   ើ    ។

      Ex:

          class Cat{
                    public:
                    void SetAge (unsigned int Age);
                    unsigned int GetAge();

                                     void SetWeight(unsigned int Weight);
                                     unsigned int GetWeight();

                                     void Meow();

                                     private:
                                     unsigned int itsAge;
                                     unsigned int itsWeight;
          };

2.Implement Class Methods:

      តាមឧទា          រណខា្ទលើ ្ាន់តតបង្ហញពី រទបៀបក្នុ្ការ declare Member នន Class តតបុទ្ោះ។ ចំទ
                        ៍               ា                                                ណ                               ោះចំនុចទនោះ

      វ ិញទ      ើ្នឹ ្ទធវើការបទ្កើតនូ វ ំទនើការរប្់ Member Functions នីមួ         ៗត លមានទៅក្នុ្ Class ត ល្តូវបានទៅថា

      Implement Class Methods ឬ Function Definition ។




      Ex:

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


          #include<iostream>
          using namespace std;
          class Cat{
                    public:
                            int GetAge();
                            void SetAge(int age);
                            void Meow();
                    private:
                             int itsAge;
          };
          int Cat::GetAge(){
              return itsAge;
          }

          void Cat::SetAge(int age){
               itsAge = age;
          }

          void Cat::Meow(){
               cout<<"Meow.n";
          }

          int main(){
              Cat Frisky;
              Frisky.SetAge(5);
              Frisky.Meow();
              cout<<"Frisky is a cat who is ";
              cout<<Frisky.GetAge()<<"years old.n";
              Frisky.Meow();


                cin.get();
                return 0;
          }




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


3.ការបម្កីត Constructors និ្ Destructors:

      Constructor គឺជា Method ពិ ទ្្មួ                  ត លអាច Initialize តំនលទៅឲ Member Data នីមួ           បានភ្លមៗ
                                                                                                                  ា

      ទៅទពលត លបទ្កើត Object ។ Constructor មានលក្ខណៈ្បហាក្់្បត                            លានឹ្ Function ទូទៅត រ
                                                                                           ន

      ទោ        វាមានទ្ោះ ូច នឹ្ Class ទ
                       ម                            ើ    មានចំនួន Parameters ជាទ្ចើនតាមតំ រវការ បុតនរវាាន Return Value ឬ
                                                                                           ូ            ម

      Void ទ       ោះទទ។


      Constructor អាច្តូវបានបទ្កើតជាទ្ចើនទៅតាមតំរូវការទោ                        មាន Parameters ខុ្ាទៅតាមតំរូវការ
                                                                                                   ន

      ទ     ើ   ក្នុ្ទ    ោះក្៏មាន Constructor មួ       ាន Parameter ត ល្តូវបានទៅថា Default Constructor ។ Default
                                                         ម

      Constructor គឺ្តូវបានបទ្កើតទ           ើ្ទោ        Compiler ទទាោះបី ជាទ     ើ្មិនបទ្កើតក្៏ទោ     ។ Default Constructor

      មិនទធវើការអ្វីទាំ្អ្្់ លុោះ្តា្ទ              ើ្បទ្កើតវាទោ     ខាួនឯ្ ទ ើមបីឲវា ំ ទនើការអ្វីមួ   ។


      ប ា ប់ពីទ          ើ្បាន្បកា្ Constructor រួចរាល់ទ         ើ   ទ   ោះ ទ   ើ្្តូវទធវើការ ្បកា្ Destructor មួ    ទទៀតផ្ត រ

      ទ ើមបី Clean Up ទៅទលើ Object ត លបានទ្បើ្បា្់ទៅក្នុ្ Memory ។


      Ex:

          #include<iostream>
          using namespace std;
          class Cat{
              public:
                     Cat(int initialiAge);
                     ~Cat();
                     int GetAge();
                     void SetAge(int age);
                     void Meow();
              private:
                      int itsAge;
          };
          Cat::Cat(int initialAge){
                       itsAge = initialAge;
          }
          Cat::~Cat(){

          }
          int Cat::GetAge(){
              return itsAge;
          }
          void Cat::SetAge(int age){
               itsAge=age;
          }
          void Cat::Meow(){
               cout<<"Meow.n";
          }


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



          int main(){
              Cat Frisky(5);
              Frisky.Meow();
              cout<<"Frisky is a cat who is ";
              cout<<Frisky.GetAge()<<"years old.n";
              Frisky.Meow();
              Frisky.SetAge(7);
              cout<<"Now Frisky is ";
              cout<<Frisky.GetAge() <<" years old.n";
              cin.get();
              return 0;
          }

4. Including const Member Functions:

      ទ     ើ្អាចទធវើការទ្បើ្បា្់ const Keyword ទ ើមបីោក្់ ទៅខា្ទ្កា           Member Functions

      ទ ើមបីក្ំនត់ វាមិនអាចទធវើការ តក្ត្បបាន      ូចជា const Variable ផ្ត រ។ សារៈ្បទោជន៍រប្់វាគឺមិនអ្នុញ្ញតិឲ
                                                                                                         ា

      Member Function អាចទធវើការ change value នន Member ្មួ                   នន Class ទ   ើ   ។


      ជាទូទៅ const Keyword ្តូវបានបតនែមជាមួ             នឹ្ Accessor Get ទោ       មិនបតនែមជាមួ     នឹ្ Accessor Set ទ   ោះទទ

      ទ្       ោះ Set គឺទ   ើ្តត្តតទធវើការផរល់តំនលថមីៗ ទៅឲវាជានិចច បុតនរចំទ     ោះ Get វ ិញគឺ្ាន់តតទាញ    ក្តំ នលទចញពី

      Member មក្បង្ហញបុ ទ្ោះ ទោ
                   ា      ណ                   មិនមានការ edit តំនលទ   ោះទទ។


      Ex:


           #include<iostream>
           using namespace std;
           class Cat{
               public:
                      Cat(int initialiAge);
                      ~Cat();
                      int GetAge() const;
                      void SetAge(int age);
                      void Meow();
               private:
                       int itsAge;
           };
           Cat::Cat(int initialAge){
                        itsAge = initialAge;
           }
           Cat::~Cat(){

           }




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



           int Cat::GetAge() const{
               //itsAge+=5;
               return itsAge;
           }
           void Cat::SetAge(int age){
                itsAge=age;
           }
           void Cat::Meow(){
                cout<<"Meow.n";
           }
           int main(){
               Cat Frisky(5);
               Frisky.Meow();
               cout<<"Frisky is a cat who is ";
               cout<<Frisky.GetAge()<<"years old.n";
               Frisky.Meow();
               Frisky.SetAge(7);
               cout<<"Now Frisky is ";
               cout<<Frisky.GetAge() <<" years old.n";
               cin.get();
               return 0;
           }

5. Where to Put Class Declaration and Method Definitions:

      ទ     ើ្អាចទធវើការបទ្កើត Class ទៅក្នុ្ File តតមួ   ជាមួ   នឹ្ Source Code ក្៏បាន ឬក្៏ទ   ើ្អាចទធវើការបទ្កើតជា File

      មួ    ោច់ពីាតត្តូវបានទៅថា Header File ត លមាន Extension *.h, *.hp, *.hpp ។
                  ន

      Ex:

      > MainFile.cpp

           #include<iostream>
           using namespace std;
           #include "Cat.h"

           int main(){
               Cat Frisky(5);
               Frisky.Meow();
               cout<<"Frisky is a cat who is ";
               cout<<Frisky.GetAge()<<"years old.n";
               Frisky.Meow();
               Frisky.SetAge(7);
               cout<<"Now Frisky is ";
               cout<<Frisky.GetAge() <<" years old.n";
               cin.get();
               return 0;
           }

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


      > Cat.h

          class Cat{
              public:
                     Cat(int initialiAge);
                     ~Cat();
                     int GetAge() const;
                     void SetAge(int age);
                     void Meow();
              private:
                      int itsAge;
          };
          Cat::Cat(int initialAge){
                       itsAge = initialAge;
          }
          Cat::~Cat(){

          }
          int Cat::GetAge() const{
              //itsAge+=5;
              return itsAge;
          }
          void Cat::SetAge(int age){
               itsAge=age;
          }
          void Cat::Meow(){
               cout<<"Meow.n";
          }

6. Inline Implementation:

      ទ     ើ្អាចទធវើការក្ំនត់ Definition នន Member Function ទៅទពល្បកា្វាទៅក្នុ្ Class បានផ្ត រ

      ត លវ ិធីសាស្ត្រទនោះ្តូវបានទៅថា Inline Implementation ។

      Ex:

          #include<iostream>
          using namespace std;
          class Cat{
              public:
                     Cat(int initialiAge);
                     ~Cat();
                     int GetAge() const {return itsAge;}
                     void SetAge(int age){itsAge=age;}
                     void Meow()const {cout<<"Meow.n";}
              private:
                      int itsAge;
          };

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




        Cat::Cat(int initialAge){
                     itsAge = initialAge;
        }
        Cat::~Cat(){
        }
        int main(){
            Cat Frisky(5);
            Frisky.Meow();
            cout<<"Frisky is a cat who is ";
            cout<<Frisky.GetAge()<<"years old.n";
            Frisky.Meow();
            Frisky.SetAge(7);
            cout<<"Now Frisky is ";
            cout<<Frisky.GetAge() <<" years old.n";
            cin.get();
            return 0;
        }

7.ការបម្កីត Constructors និ្ Classes with Other Classes as Member Data :

      ជាមួ        នឹ្ OOP Technique គឺតត្តតបទ្កើត Class មួ     ត ល្តូវបានចូលរួមពីប្ Class
                                                                                   រ                        នទទទៀត ជាទ្ចើន

      រួមបញ្លាតតមួ
            ូច ន            ។ ឧទា     រណ ទ
                                        ៍      ើ្មាន Class Wheel, Class Machine, Class Tire

        ូទចនោះទ    ើ្អាច   ក្ពួក្វាទាំ្ ទ   ោះទៅរួមបញ្លាទ ើមបីបទ្កើតបានជា Class Car ថមីមួ
                                                      ូច ន                                          ។

      ក្នុ្ការ្បកា្ទនោះ្តូវបានទៅថា has-a relationship មានន័             ថា Class Car មាន Wheel, Machine, និ្ Tire ។

      តាមឧទា         រណខា្ទ្កាមទនោះនឹ្ទធវើការបទ្កើត Class ចំនួន 3 ត លមាន ូចជា Class Point, Class Rectangle
                       ៍

      ត លក្នុ្ទ       ោះ Class Rectangle គឺ្តូវបានបទ្កើតទ   ើ្ទោ      Lines      ទ   ើ   ក្នុ្ទ   ោះ Line

      នីមួ    ៗ្តូវបានបទ្កើតទ      ើ្ទោ     2 Points ត ល Point នីមួ    ៗ គឺ ្តូវបានក្ំនត់ ទោ      X-Coordinate និ្ Y-

      Coordinate ។




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


      Ex:

       #include <iostream>
       using namespace std;
       class Point{
             public:
                    void SetX(int x){itsX = x;}
                    void SetY(int y){itsY = y;}
                    int GetX() const {return itsX;}
                    int GetY() const {return itsY;}
             private:
                      int itsX;
                      int itsY;
       };




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



      class Rectangle{
            public:
                   Rectangle(int top, int left,int bottom,
      int right);
                   ~Rectangle(){}

                         int GetTop() const {return itsTop;}
                         int GetLeft() const {return itsLeft;}
                         int GetBottom() const {return itsRight;}

                   Point      GetUpperLeft() const {return
      itsUpperLeft;}
                   Point      GetLowerLeft() const {return
      itsLowerLeft;}
                   Point      GetUpperRight() const {return
      itsUpperRight;}
                   Point      GetLowerRight() const {return
      itsLowerRight;}

                   void SetUpperLeft(Point
      Location){itsUpperLeft=Location;}
                   void SetLowerLeft(Point
      Location){itsLowerLeft=Location;}
                   void SetUpperRight(Point
      Location){itsUpperRight=Location;}
                   void SetLowerRight(Point
      Location){itsLowerRight=Location;}

                         void SetTop(int top){itsTop = top;}
                         void SetLeft(int left){itsLeft = left;}
                         void SetBottom(int bottom){itsBottom =
      bottom;}
                         void SetRight(int right){itsRight =
      right;}

                         int GetArea() const;
                                      private:
                                 Point itsUpperLeft;
                                 Point itsUpperRight;
                                 Point itsLowerLeft;
                                 Point itsLowerRight;

                                 int   itsTop;
                                 int   itsLeft;
                                 int   itsBottom;
                                 int   itsRight;           };
C++ OOP                          www.enteritc.com                                    15
Enter Information Technology Center                      មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា
                                                                 ឌ         ៌


       Rectangle::Rectangle(int top,int left,int bottom,int
       right){
              itsTop = top;
              itsLeft=left;
              itsBottom = bottom;
              itsRight = right;

                  itsUpperLeft.SetX(left);
                  itsUpperLeft.SetY(top);

                  itsUpperRight.SetX(right);
                  itsUpperRight.SetY(top);

                  itsLowerLeft.SetX(left);
                  itsLowerLeft.SetY(bottom);

                  itsLowerRight.SetX(right);
                  itsLowerRight.SetY(bottom);
       }
       int Rectangle::GetArea() const{
        int Width = itsRight - itsLeft;
        int Height = itsTop - itsBottom;
        return (Width * Height);
       }
       int main(){
           Rectangle MyRectangle(100,20,50,80);
           int Area = MyRectangle.GetArea();
           cout<<"Area:"<<Area<<endl;
           cout<<"Upper Left X Coordinate:";
           cout<<MyRectangle.GetUpperLeft().GetX();
           cin.get();
           return 0;
       }




16                                     www.enteritc.com                     C++ OOP

More Related Content

Featured

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
 

Featured (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
 

Enter center c++ oop chapter 2

  • 1. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center មមមរៀនទ្យី 2: សិក្សាពី Classes and Objects-II 1.ការបម្កីត Data Member ជា Private: ជាក្បួនទូ ទៅននការបទ្កើត Class គឺទ ើ្គួ រតតទធវើការក្ំនត់ ឲ Data Members ជា Private ទ ើ ទ្បើវ ិធីសាស្ត្រមួ ទផេ្ទទៀតក្នុ្ការ Access ទៅកាន់ Member ទ ោះ។ វ ិធីសាស្ត្រក្ុន្ការ Access ទៅកាន់ Private Member គឺទ ើ្ បទ្កើត Public Function មួ ្តូវបានទៅថា Accessor Methods ទោ ទធវើការទ្បើ្បា្់វាទ ើមបី Set និ្ Get ទៅកាន់ Private Member ទ ោះ។ ចំទ ោះ Accessor Methods ក្៏ជា Member Function រប្់ Class ផ្ត រ ត លទ្បើ្ំរាប់ទធវើការ Input (Set) ចូលទៅកាន់ Private Member និ្ Output (Get) ទចញពី Private Member វ ិញ។ Accessor Functions អាចឲទ ើ្ទធវើការតប្តចក្រវា្ How the data is store ជាមួ នឹ ្ How it is used មានន័ ថា ទ ើ្អាចទធវើការផ្ល្់បូររ How to data is stored ទៅទពលទ្កា ា បាន ទោ ពុំ ចំបាច់ទធវើការ ្រទ្រទ ើ្វ ិញនូ វទផេ្ទទៀត្ំរាប់ទ្បើ ្បា្់ជាមួ នឹ្ data ទ ោះទ ើ ។ Ex: class Cat{ public: void SetAge (unsigned int Age); unsigned int GetAge(); void SetWeight(unsigned int Weight); unsigned int GetWeight(); void Meow(); private: unsigned int itsAge; unsigned int itsWeight; }; 2.Implement Class Methods: តាមឧទា រណខា្ទលើ ្ាន់តតបង្ហញពី រទបៀបក្នុ្ការ declare Member នន Class តតបុទ្ោះ។ ចំទ ៍ ា ណ ោះចំនុចទនោះ វ ិញទ ើ្នឹ ្ទធវើការបទ្កើតនូ វ ំទនើការរប្់ Member Functions នីមួ ៗត លមានទៅក្នុ្ Class ត ល្តូវបានទៅថា Implement Class Methods ឬ Function Definition ។ Ex: C++ OOP www.enteritc.com 7
  • 2. Enter Information Technology Center មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ #include<iostream> using namespace std; class Cat{ public: int GetAge(); void SetAge(int age); void Meow(); private: int itsAge; }; int Cat::GetAge(){ return itsAge; } void Cat::SetAge(int age){ itsAge = age; } void Cat::Meow(){ cout<<"Meow.n"; } int main(){ Cat Frisky; Frisky.SetAge(5); Frisky.Meow(); cout<<"Frisky is a cat who is "; cout<<Frisky.GetAge()<<"years old.n"; Frisky.Meow(); cin.get(); return 0; } 8 www.enteritc.com C++ OOP
  • 3. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center 3.ការបម្កីត Constructors និ្ Destructors: Constructor គឺជា Method ពិ ទ្្មួ ត លអាច Initialize តំនលទៅឲ Member Data នីមួ បានភ្លមៗ ា ទៅទពលត លបទ្កើត Object ។ Constructor មានលក្ខណៈ្បហាក្់្បត លានឹ្ Function ទូទៅត រ ន ទោ វាមានទ្ោះ ូច នឹ្ Class ទ ម ើ មានចំនួន Parameters ជាទ្ចើនតាមតំ រវការ បុតនរវាាន Return Value ឬ ូ ម Void ទ ោះទទ។ Constructor អាច្តូវបានបទ្កើតជាទ្ចើនទៅតាមតំរូវការទោ មាន Parameters ខុ្ាទៅតាមតំរូវការ ន ទ ើ ក្នុ្ទ ោះក្៏មាន Constructor មួ ាន Parameter ត ល្តូវបានទៅថា Default Constructor ។ Default ម Constructor គឺ្តូវបានបទ្កើតទ ើ្ទោ Compiler ទទាោះបី ជាទ ើ្មិនបទ្កើតក្៏ទោ ។ Default Constructor មិនទធវើការអ្វីទាំ្អ្្់ លុោះ្តា្ទ ើ្បទ្កើតវាទោ ខាួនឯ្ ទ ើមបីឲវា ំ ទនើការអ្វីមួ ។ ប ា ប់ពីទ ើ្បាន្បកា្ Constructor រួចរាល់ទ ើ ទ ោះ ទ ើ្្តូវទធវើការ ្បកា្ Destructor មួ ទទៀតផ្ត រ ទ ើមបី Clean Up ទៅទលើ Object ត លបានទ្បើ្បា្់ទៅក្នុ្ Memory ។ Ex: #include<iostream> using namespace std; class Cat{ public: Cat(int initialiAge); ~Cat(); int GetAge(); void SetAge(int age); void Meow(); private: int itsAge; }; Cat::Cat(int initialAge){ itsAge = initialAge; } Cat::~Cat(){ } int Cat::GetAge(){ return itsAge; } void Cat::SetAge(int age){ itsAge=age; } void Cat::Meow(){ cout<<"Meow.n"; } C++ OOP www.enteritc.com 9
  • 4. Enter Information Technology Center មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ int main(){ Cat Frisky(5); Frisky.Meow(); cout<<"Frisky is a cat who is "; cout<<Frisky.GetAge()<<"years old.n"; Frisky.Meow(); Frisky.SetAge(7); cout<<"Now Frisky is "; cout<<Frisky.GetAge() <<" years old.n"; cin.get(); return 0; } 4. Including const Member Functions: ទ ើ្អាចទធវើការទ្បើ្បា្់ const Keyword ទ ើមបីោក្់ ទៅខា្ទ្កា Member Functions ទ ើមបីក្ំនត់ វាមិនអាចទធវើការ តក្ត្បបាន ូចជា const Variable ផ្ត រ។ សារៈ្បទោជន៍រប្់វាគឺមិនអ្នុញ្ញតិឲ ា Member Function អាចទធវើការ change value នន Member ្មួ នន Class ទ ើ ។ ជាទូទៅ const Keyword ្តូវបានបតនែមជាមួ នឹ្ Accessor Get ទោ មិនបតនែមជាមួ នឹ្ Accessor Set ទ ោះទទ ទ្ ោះ Set គឺទ ើ្តត្តតទធវើការផរល់តំនលថមីៗ ទៅឲវាជានិចច បុតនរចំទ ោះ Get វ ិញគឺ្ាន់តតទាញ ក្តំ នលទចញពី Member មក្បង្ហញបុ ទ្ោះ ទោ ា ណ មិនមានការ edit តំនលទ ោះទទ។ Ex: #include<iostream> using namespace std; class Cat{ public: Cat(int initialiAge); ~Cat(); int GetAge() const; void SetAge(int age); void Meow(); private: int itsAge; }; Cat::Cat(int initialAge){ itsAge = initialAge; } Cat::~Cat(){ } 10 www.enteritc.com C++ OOP
  • 5. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center int Cat::GetAge() const{ //itsAge+=5; return itsAge; } void Cat::SetAge(int age){ itsAge=age; } void Cat::Meow(){ cout<<"Meow.n"; } int main(){ Cat Frisky(5); Frisky.Meow(); cout<<"Frisky is a cat who is "; cout<<Frisky.GetAge()<<"years old.n"; Frisky.Meow(); Frisky.SetAge(7); cout<<"Now Frisky is "; cout<<Frisky.GetAge() <<" years old.n"; cin.get(); return 0; } 5. Where to Put Class Declaration and Method Definitions: ទ ើ្អាចទធវើការបទ្កើត Class ទៅក្នុ្ File តតមួ ជាមួ នឹ្ Source Code ក្៏បាន ឬក្៏ទ ើ្អាចទធវើការបទ្កើតជា File មួ ោច់ពីាតត្តូវបានទៅថា Header File ត លមាន Extension *.h, *.hp, *.hpp ។ ន Ex: > MainFile.cpp #include<iostream> using namespace std; #include "Cat.h" int main(){ Cat Frisky(5); Frisky.Meow(); cout<<"Frisky is a cat who is "; cout<<Frisky.GetAge()<<"years old.n"; Frisky.Meow(); Frisky.SetAge(7); cout<<"Now Frisky is "; cout<<Frisky.GetAge() <<" years old.n"; cin.get(); return 0; } C++ OOP www.enteritc.com 11
  • 6. Enter Information Technology Center មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ > Cat.h class Cat{ public: Cat(int initialiAge); ~Cat(); int GetAge() const; void SetAge(int age); void Meow(); private: int itsAge; }; Cat::Cat(int initialAge){ itsAge = initialAge; } Cat::~Cat(){ } int Cat::GetAge() const{ //itsAge+=5; return itsAge; } void Cat::SetAge(int age){ itsAge=age; } void Cat::Meow(){ cout<<"Meow.n"; } 6. Inline Implementation: ទ ើ្អាចទធវើការក្ំនត់ Definition នន Member Function ទៅទពល្បកា្វាទៅក្នុ្ Class បានផ្ត រ ត លវ ិធីសាស្ត្រទនោះ្តូវបានទៅថា Inline Implementation ។ Ex: #include<iostream> using namespace std; class Cat{ public: Cat(int initialiAge); ~Cat(); int GetAge() const {return itsAge;} void SetAge(int age){itsAge=age;} void Meow()const {cout<<"Meow.n";} private: int itsAge; }; 12 www.enteritc.com C++ OOP
  • 7. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center Cat::Cat(int initialAge){ itsAge = initialAge; } Cat::~Cat(){ } int main(){ Cat Frisky(5); Frisky.Meow(); cout<<"Frisky is a cat who is "; cout<<Frisky.GetAge()<<"years old.n"; Frisky.Meow(); Frisky.SetAge(7); cout<<"Now Frisky is "; cout<<Frisky.GetAge() <<" years old.n"; cin.get(); return 0; } 7.ការបម្កីត Constructors និ្ Classes with Other Classes as Member Data : ជាមួ នឹ្ OOP Technique គឺតត្តតបទ្កើត Class មួ ត ល្តូវបានចូលរួមពីប្ Class រ នទទទៀត ជាទ្ចើន រួមបញ្លាតតមួ ូច ន ។ ឧទា រណ ទ ៍ ើ្មាន Class Wheel, Class Machine, Class Tire ូទចនោះទ ើ្អាច ក្ពួក្វាទាំ្ ទ ោះទៅរួមបញ្លាទ ើមបីបទ្កើតបានជា Class Car ថមីមួ ូច ន ។ ក្នុ្ការ្បកា្ទនោះ្តូវបានទៅថា has-a relationship មានន័ ថា Class Car មាន Wheel, Machine, និ្ Tire ។ តាមឧទា រណខា្ទ្កាមទនោះនឹ្ទធវើការបទ្កើត Class ចំនួន 3 ត លមាន ូចជា Class Point, Class Rectangle ៍ ត លក្នុ្ទ ោះ Class Rectangle គឺ្តូវបានបទ្កើតទ ើ្ទោ Lines ទ ើ ក្នុ្ទ ោះ Line នីមួ ៗ្តូវបានបទ្កើតទ ើ្ទោ 2 Points ត ល Point នីមួ ៗ គឺ ្តូវបានក្ំនត់ ទោ X-Coordinate និ្ Y- Coordinate ។ C++ OOP www.enteritc.com 13
  • 8. Enter Information Technology Center មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Ex: #include <iostream> using namespace std; class Point{ public: void SetX(int x){itsX = x;} void SetY(int y){itsY = y;} int GetX() const {return itsX;} int GetY() const {return itsY;} private: int itsX; int itsY; }; 14 www.enteritc.com C++ OOP
  • 9. មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Enter Information Technology Center class Rectangle{ public: Rectangle(int top, int left,int bottom, int right); ~Rectangle(){} int GetTop() const {return itsTop;} int GetLeft() const {return itsLeft;} int GetBottom() const {return itsRight;} Point GetUpperLeft() const {return itsUpperLeft;} Point GetLowerLeft() const {return itsLowerLeft;} Point GetUpperRight() const {return itsUpperRight;} Point GetLowerRight() const {return itsLowerRight;} void SetUpperLeft(Point Location){itsUpperLeft=Location;} void SetLowerLeft(Point Location){itsLowerLeft=Location;} void SetUpperRight(Point Location){itsUpperRight=Location;} void SetLowerRight(Point Location){itsLowerRight=Location;} void SetTop(int top){itsTop = top;} void SetLeft(int left){itsLeft = left;} void SetBottom(int bottom){itsBottom = bottom;} void SetRight(int right){itsRight = right;} int GetArea() const; private: Point itsUpperLeft; Point itsUpperRight; Point itsLowerLeft; Point itsLowerRight; int itsTop; int itsLeft; int itsBottom; int itsRight; }; C++ OOP www.enteritc.com 15
  • 10. Enter Information Technology Center មជ្ឈមណ្លអិនធ័រព័តមានវិទ្យា ឌ ៌ Rectangle::Rectangle(int top,int left,int bottom,int right){ itsTop = top; itsLeft=left; itsBottom = bottom; itsRight = right; itsUpperLeft.SetX(left); itsUpperLeft.SetY(top); itsUpperRight.SetX(right); itsUpperRight.SetY(top); itsLowerLeft.SetX(left); itsLowerLeft.SetY(bottom); itsLowerRight.SetX(right); itsLowerRight.SetY(bottom); } int Rectangle::GetArea() const{ int Width = itsRight - itsLeft; int Height = itsTop - itsBottom; return (Width * Height); } int main(){ Rectangle MyRectangle(100,20,50,80); int Area = MyRectangle.GetArea(); cout<<"Area:"<<Area<<endl; cout<<"Upper Left X Coordinate:"; cout<<MyRectangle.GetUpperLeft().GetX(); cin.get(); return 0; } 16 www.enteritc.com C++ OOP