0% found this document useful (0 votes)
2 views

Function Definitions 1

Uploaded by

lysarith09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Function Definitions 1

Uploaded by

lysarith09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Function Definitions

- ជាការបង្កត
ើ Function។
- ងេចែកងែញជា ២េឺ Inside Class Definition និ្ Outside Class Definition។
Inside Class Definition Outside Class Definition
ចែនាំ - បង្កើត Function ងៅកនុ្ Class - ប្បកាស Function(Function Declaration) ងៅកនុ្ Class។
- បង្កត
ើ Function(Function Definition) ងៅខា្ងប្ៅ Class។
ទាំរ្់ទូងៅ class class-name{ class class-name{
//1.Attributes //1.Attributes
private: private:
data-type attribute-name; data-type attribute-name;
//2.Method //2.Method
public: public:
return-type func-name(){ //Function Declaration
Statements; return-type func-name();
} return-type func-name(data-type parmeter);
return-type func-name(data-type parmeter){ };
Statements; //Function Definition
} return-type class-name::func-name(){
}; Statements;
}
return-type class-name::func-name(data-type
parmeter){
Statements;
}
Example:
Create Class: student
- Attributes: private
o stu_id
o stu_name
o gender
o score
- Methods: public
o set(…)
o get()
C++ #include <iostream> #include <iostream>
using namespace std; using namespace std;
Code //Create Class //Create Class
class student{ class student{
//1. Attributes - Properties //1. Attributes - Properties
//មិនអនុញ្ញាតតិឲ្យ Access ពើពិភពខា្ងប្ៅងទ //មិនអនុញ្ញាតតិឲ្យ Access ពើពិភពខា្ងប្ៅងទ
private: private:
//data-type attribute; //data-type attribute;
int stu_id; int stu_id;
string stu_name; string stu_name;
float score; float score;
string gender; string gender;
//2. Methods //2. Methods
//អនុញ្ញាតតឲ្
ិ យ Access ពព ើ ភ
ិ ពខា្ងប្ៅ //អនុញ្ញាតតឲ្
ិ យ Access ពពើ ភ
ិ ពខា្ងប្ៅ
public: public:
//ងទេរតាំលៃឲ្យ Attributes ពើ Parameters //Function Declaration: ប្បកាស Function
void set(int par_id, string par_name, //ងទេរតាំលៃឲ្យ Attributes ពើ Parameters
string par_gender, float par_score){
void set(int, string, string, float);
//attribute = parameter;
stu_id = par_id;//1
stu_name = par_name;//Dara //បង្ហាញតាំលៃរបស់ Attributes មកងៃើ Screen
gender = par_gender;//Male void get();
score = par_score;//60 };
} //Function Definition: បង្កើត Function

//បង្ហាញតាំលៃរបស់ Attributes មកងៃើ Screen //ងទេរតាំលៃឲ្យ Attributes ពើ Parameters


void get(){ void student::set(int par_id, string par_name,
cout << "----------------------" << string par_gender, float par_score){
endl; //attribute = parameter;
//cout << "Message: " << attribute << stu_id = par_id;//1
endl; stu_name = par_name;//Dara
cout << "Student ID: " << stu_id << gender = par_gender;//Male
endl;//Student ID: 1 score = par_score;//60
cout << "Student Name: " << stu_name << }
endl;//Student Name: Dara
cout << "Gender: " << gender << //បង្ហាញតាំលៃរបស់ Attributes មកងៃើ Screen
endl;//Gender: Male
void student::get(){
cout << "Score: " << score <<
cout << "----------------------" << endl;
endl;//Score: 60
//cout << "Message: " << attribute << endl;
cout << "----------------------" <<
cout << "Student ID: " << stu_id <<
endl;
endl;//Student ID: 1
}
cout << "Student Name: " << stu_name <<
};
endl;//Student Name: Dara
int main() {
cout << "Gender: " << gender << endl;//Gender:
//Instance Object: បង្កើត Object Male
//class-name object-name; cout << "Score: " << score << endl;//Score: 60
student obj; cout << "----------------------" << endl;
//Method }
//object.method(...) int main() {
obj.set(1,"Dara","Male",60); //Instance Object: បង្កើត Object
obj.get();
//class-name object-name;
return 0;
student obj;
}
//Method
//object.method(...)
obj.set(1,"Dara","Male",60);
obj.get();
return 0;
}
Output

You might also like