1 / 12

درس برنامه ‌ سازي کامپيوتر

درس برنامه ‌ سازي کامپيوتر. کلاس ‌ ها در زبان برنامه ‌ سازي C ++. تعريف. در جامعه، افراد ي که موقعيت و مسؤليت خود را مي ‌ دانند از راحتي و امنيت بيشتري برخوردارند هر کلاس از جامعه، مجموعه ‌ اي از امکانات و تواناييهاي مجاز برخوردار است

durin
Download Presentation

درس برنامه ‌ سازي کامپيوتر

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. درس برنامه‌سازي کامپيوتر کلاس‌ها در زبان برنامه‌سازي C++

  2. تعريف • در جامعه، افرادي که موقعيت و مسؤليت خود را مي‌دانند از راحتي و امنيت بيشتري برخوردارند • هر کلاس از جامعه، مجموعه‌اي از امکانات و تواناييهاي مجاز برخوردار است • در C++ امکان ايجاد انواعي از داده‌ها وجود دارد که مي‌توان بکمک آنها هر نوع موضوعي را نمايش داد

  3. چه ابزاري ممکن است بدست آيد • Box Box1; • Box Box2; • if(Box1 > Box2) // Fill the larger box • Box1.Fill(); • else • Box2.Fill();

  4. کلاس، همچون ساختار، يک نوع جديد براي بيان متغيرها فراهم مي‌کند • برنامه‌سازي شيءگرا (Object Oriented Programming - OOPS) روش برنامه‌سازي است که در آن براي انواع جديد دادها متغيرهايي با ساختار کلاس تعريف مي‌شوند • معرفي يک متغير جديد از کلاس ”نمونه“ (instantiation) ناميده مي‌شود • به نمونه‌ها شيء (object) مي‌گوييم • The idea of an object containing the data implicit in its definition, together with the functions that operate on that data, is referred to as encapsulation

  5. Public, private (default), protected • نحوة دسترسي به اعضاي کلاس را مشخص مي‌کند • نحوة دسترسي به اعضاي کلاس • Box2.height = 18.0; • استفاده از اشاره‌گرها • Box* pBox = &aBox • (*pBox).length = 10; • pBox->length = 10;

  6. افزودن عضو تابع به کلاس • مي‌توان تابع را در کلاس افزود • لزومي ندارد کل تابع را در کلاس بياوريم، اما لازم است نمونة تابع (تعريف قالب تابع) را در کلاس بياوريم • class Box // Class definition at global scope • { • public: • double length; // Length of a box in inches • double breadth; // Breadth of a box in inches • double height; // Height of a box in inches • double Volume(void); // Member function prototype • };

  7. در تعريف تابع خارج از کلاس از علامت :: جهت مشخص نمودن اينکه تابع تعريف شده متعلق به کدام کلاس است استفاده مي‌کنيم • // Function to calculate the volume of a box • double Box::Volume(void) • { • return length * breadth * height; • }

  8. سازندة کلاس (class constructor) • تابع خاصي است که هنگام معرفي شيء جديد صدا زده ميشود. بکمک آن مي‌توان مقادير اوليه را به شيء داد و محدودة تغييرات اعضا را کنترل نمود • class Box { // Class definition at global scope • public: • double length; // Length of a box in inches • double breadth; // Breadth of a box in inches • double height; // Height of a box in inches • // Constructor definition • Box(double lv, double bv, double hv){ • cout << endl << "Constructor called."; • length = lv; // Set values of • breadth = bv; // data members • height = hv; }

  9. / Function to calculate the volume of a box • double Volume() • { • return length * breadth * height; • } • };

  10. class Box // Class definition at global scope • { • public: • double length; // Length of a box in inches • double breadth; // Breadth of a box in inches • double height; // Height of a box in inches • // Constructor definition • Box(double lv=1.0, double bv=1.0, double hv=1.0) • { • cout << endl << "Constructor called."; • length = lv; // Set values of • breadth = bv; // data members • height = hv; • }

  11. // Function to calculate the volume of a box • double Volume() • { • return length * breadth * height; • } • };

  12. // Constructor definition using an initialization list • Box(double lv=1.0, double bv=1.0, double hv=1.0): length(lv), breadth(bv), height(hv) • { • cout << endl << "Constructor called."; • }

More Related