Download presentation
Presentation is loading. Please wait.
Published byAnnabella Pitts Modified over 9 years ago
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
نحوة دسترسي به اعضاي کلاس را مشخص ميکند نحوة دسترسي به اعضاي کلاس 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 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."; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.