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