មេរៀនទី៩ Class & Object C/C++ for beginner

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Chapter 13 – Introduction to Classes
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
C++ Classes & Data Abstraction
More about classes and objects Classes in Visual Basic.NET.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming circle method draw data C.draw()
Object Oriented Programming: Java Edition By: Samuel Robinson.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 10 Inheritance and Polymorphism
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.
Topics Instance variables, set and get methods Encapsulation
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
1 More About Derived Classes and Inheritance Chapter 9.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Access Functions and Friend Functions
Class and Objects UNIT II.
Review: Two Programming Paradigms
C# - Inheritance and Polymorphism
Chapter 5 Classes.
Presented By: Nazia Hossain Lecturer, Stamford University
This technique is Called “Divide and Conquer”.
Introduction to Object-oriented Program Design
C++ Classes & Object Oriented Programming
Object-oriented Design in Processing
Object Oriented Programming (OOP) LAB # 5
Starting to think about objects...
Inheritance CT1513.
Object oriented programming (OOP) Lecture No. 7
Introduction to Classes and Objects
By Rajanikanth B OOP Concepts By Rajanikanth B
Object-oriented Design in Processing
Classes C++ representation of an object
Final and Abstract Classes
Object Oriented Programming (OOP) Lecture No. 11
Types of Computer Languages
Classes: Arrays group many objects of the same type (in order)
Object-oriented Design in Processing
Computer Science II for Majors
Presentation transcript:

មេរៀនទី៩ Class & Object C/C++ for beginner C# Database C/C++ for beginner មេរៀនទី៩ Class & Object រៀបចំ និងបង្រៀនដោយ៖ អ៊ិន សុភា Tel: 097 200 13 99 /016 270 878 Email: insophea1987@gmail.Com

អ្វីជា OOP? C/C++ for beginner OOP គឺមកពីពាក្យថា Object-Oriented Programming ។ វាជា គោលការណ៍ មួយដែលអាចឱ្យ ភាសា programming ជាច្រើនយកគំរូតាម។ រាល់ភាសា Programming ទាំងអស់ដែលបានចាត់ចូលជា OOP គឺត្រូវផ្អែកទៅលើ ការប្រើប្រាស់ Object ជាចម្បង ហើយក៏ត្រូវមានលទ្ធភាពប្រើប្រាស់ នូវ Data Encapsulation, Inheritance និង Polymorphism ផងដែរ។

អ្វីជា Object ? C/C++ for beginner គ្រប់Object ត្រូវមានរូបរាង(attributes) និង សកម្មភាព(behaviour) ខ្ញុំថា IPhone ជា Object ព្រោះ វាមានរូបរាង និង សកម្មភាពដែលអាចកំណត់បានថាជា “ទូរសព្ទដៃ”។ ចំណែកឯ ពាក្យថា “ទូរសព្ទដៃ” វាជាប្រភេទទិន្នន័យមួយដែលតំណាងឱ្យគ្រប់ ទូរសព្ទដៃទាំងអស់។ការតំណាងនេះហៅថា Class

C/C++ for beginner Class គឺជាពុម្ភ ឬជាការតំណាងឱ្យ ទិន្នន័យណាមួយជាអរូបិយ៍។ មានន័យថា Class គឺជាអ្នកកំណត់ឱ្យបានទៅជា Object ផ្សេងៗ។ Class Object Object Object

class className{ //access modifier //class members }OjectName; C/C++ for beginner ការបង្កើត Class Syntax: class className{ //access modifier //class members }OjectName; Access modifier ជាដែនកំណត់នៃការប្រើប្រាស់ អាចជា private, protected ឬ public Class member ជាធាតុរបស់ class អាចជា variable ឬ function ជាដើម។

C/C++ for beginner Example: Class Laptop{ private: int code;//1 string model;//Dell.. int RAM;//4GB int HDD;//500GB }; រាល់ members របស់ class ដែលបានកំណត់ជា private នោះវាមានលទ្ធភាពប្រើបានតែក្នុង class ខ្លួនឯងប៉ុណ្ណោះ។ Access Modifier Class member

class objectName; ការយក Class ទៅប្រើ Syntax: C/C++ for beginner ការលើកយក Class មកប្រើគឺមានន័យថាជាការបង្កើត នូវ Object តំណាងឱ្យ Class នោះ។

ការកំណត់ពត៌មានទៅកាន់ Object C/C++ for beginner ការកំណត់ពត៌មានទៅកាន់ Object int main(){ ​​​ Laptop pc1; Laptop pc2; pc1.code=1; pc1.model="Dell Inspiron 14z"; pc1.RAM=4; pc1.HDD=500; pc2.code=2; pc2.model="Dell XPS"; pc2.RAM=2; pc2.HDD=320; }

C/C++ for beginner តាម កូដខាងលើឃើញថា រាល់ការទាញយក member របស់ class មកប្រើគឺតាមរយៈ សញ្ញា dot បន្ទាប់ពី ObjectName ក៏ប៉ុន្តែអ្វីដែល Error នោះគឺ មិនអាចទាញមកប្រើបានទេ ពីព្រោះ រាល់ member ទាំងអស់នោះសុទ្ធតែបានប្រើ Private ជា access modifier ។ ដូច្នេះអាចប្តូរពី private ទៅជា public វិញ ។ រាល់ class member ដែលបានកំណត់ជា public វាអាចយកទៅប្រើបានទាំងក្នុង class ខ្លួនឯង និងតាមរយៈ object ។

C/C++ for beginner Class Laptop{ public: int code;//1 string model;//Dell.. int RAM;//4GB int HDD;//500GB }; ប៉ុន្តែ OOP មិនតម្រូវឱ្យកំណត់ class members ទាំងអស់ជា public នោះទេ ព្រោះវាធ្វើឱ្យកូដគ្មានរបៀប ពោលគឺ មាន members ខ្លះត្រូវបានបង្ហាញ និងខ្លះមិនត្រូវបានបង្ហាញ ដែលទាំងនេះគេហៅថា Data Encapsulation។

C/C++ for beginner កែប្រែ Class ដដែលទៅជា Class Laptop{ private: int code; string model; int RAM; int HDD; public: void setLaptop(int code,string model,int ram,int hdd){ //code=code; this->code=code; this->model=model; RAM=ram; HDD=hdd; } string getLaptop(){ return code+”,”+model+”,”+RAM+”,”+HDD; };

C/C++ for beginner this keyword & -> arrow operator Class Laptop{ private: int code; string model; int RAM; int HDD; public: void setLaptop(int code,string model,int ram,int hdd){ //code=code; this->code=code; this->model=model; RAM=ram; HDD=hdd; } string getLaptop(){ return code+”,”+model+”,”+RAM+”​GB,”+HDD+”GB”; }; this ជា keyword តំណាងឱ្យ Current object នៃ class។ គេប្រើវាសម្រាប់ access ទៅកាន់ member ផ្ទាល់របស់ class

C/C++ for beginner int main(){ ​​​ Laptop pc1; Laptop pc2; pc1.code=1; pc1.model=“Dell Inspiron 14z”; pc1.RAM=4; pc1.HDD=500; pc2.code=2; pc2.model=“Dell XPS”; pc2.RAM=2; pc2.HDD=320; } int main(){ ​​​ Laptop pc1; Laptop pc2; pc1.setLaptop(1,"Dell",4,500); pc2.setLaptop(2,"Asus",2,320); cout<<pc1.getLaptop()<<endl; cout<<pc2.getLaptop(); getch(); }

C/C++ for beginner Constructor ជាទូទៅ រាល់ function ដែលបានបង្កើតនៅក្នុង class ត្រូវបានគេហៅថា method និងរាល់ method ដែលមានឈ្មោះដូចទៅនឹង class របស់វាគេហៅថា Constructor។ មិនមាន return type អនុវត្តដំបូងបំផុតនៅពេល class ត្រូវបាន instance ជា object ត្រូវមាន access modifier ជា​ public អាចមានលក្ខណៈជា Overloading

C/C++ for beginner Example: class BOX{ private: int width,height; public: BOX(){ width=height=10; } BOX(int width,int height){ this->width=width; this->height=height; int getArea(){ return width*height; }; 1 2

BOX() និង BOX(20,30) ជា Constructor របស់ class BOX C/C++ for beginner Example: int main(){ BOX box1=BOX(); BOX box2=BOX(20,30); cout<<"The area of box1 is "<<box1.getArea()<<endl; cout<<"The area of box2 is "<<box2.getArea(); getch(); } BOX() និង BOX(20,30) ជា Constructor របស់ class BOX

Inheritance C/C++ for beginner វាជាលទ្ធភាពដែលអាចផ្តល់ឱ្យ class មួយទទួល member ពី class ដទៃទៀត ក្នុងគោលបំណងបំពេញបន្ថែម ដោយកាត់បន្ថយការបង្កើតឡើងវិញ និងមិនប៉ះពាល់ដល់ class ដែលមានស្រាប់។ Class ដែលផ្តល់ member ទៅគេប្រើ ជា based class ឬ supper class Class ដែលទទួល member ពីគេ ជា derived class ឬ​ sub class

នៅក្នុង ភាសា C++ ក៏អាចប្រើនូវ​ multi-Inheritance C/C++ for beginner Syntax: class subClassName:public supperClassName{ //members } នៅក្នុង ភាសា C++ ក៏អាចប្រើនូវ​ multi-Inheritance class subClassName:public supperClassName1,...{

C/C++ for beginner Example: class Shape { protected: float width, height; public: void set_data(float a, float b){ width = a; height = b; } }; class Rectangle:public Shape { public: float area () { return (width * height); class Triangle:public Shape { return (width * height / 2); រាល់ class members ដែលប្រើ access modifier ជា protected វាមានលទ្ធភាព ទាញទៅប្រើប្រាស់បានទាំងនៅក្នុង classខ្លួន ឯង និង class ដែលបាន inherit ពីវាតែប៉ុណ្ណោះ។

C/C++ for beginner int main () { Rectangle rect; Triangle tri; rect.set_data (5,3); tri.set_data (2,5); cout << rect.area()<<endl; cout << tri.area() <<endl; return 0; }

C/C++ for beginner Polymorphism ជា ការបង្កើតនូវអ្វីមួយដោយអាចយកទៅប្រើបានច្រើនទិសដៅ ក្នុងនោះមាន Overloading method ជាដើម ឬក៏អាចនិយាយយ៉ាងខ្លីថាវាគឺជា template

C/C++ for beginner Example: int main () { Shape *pshape;//pshape is a pointer to an object class Shape. Rectangle rec;//creatge rec object Triangle tri; // create tri object pshape=&rec; pshape->set_data(10,20); cout<<"The area of Rec is "<<rec.area(); pshape=&tri; cout<<endl<<"The area of Tri is"<<tri.area(); cin.get(); return 0; }