انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Classes and Objects

الكلية كلية العلوم للبنات     القسم قسم الحاسبات     المرحلة 2
أستاذ المادة مهدي عبد سلمان المسلماوي       3/29/2011 10:08:05 AM

Classes and Objects

 

Specifying a Class

 

A class is a way to bind the data and its associated functions together. It allows the data (and functions) to be hidden, if necessary use. When defining a class, we are creating a new abstract data type that can be treated like any other built-in data type. Generally, a class specification has two parts:

1.      Class declaration

2.      Class function definitions

The class declaration describes the type and scope of its members. The class function definitions describe how the class functions are implemented.

The general form of a class declaration is:

class class_name

 

{

 

         private:

 

               variable declarations;

 

               function declarations;

 

        public:

 

               variable declarations;

 

                function declarations;

 

};

 

Notes:

 

- The keyword class specified that what follows is an abstract data of type class_name.

- The body of a class is surrounded by brackets and terminated by a semicolon "{…};".

- The class body contains the declaration of variables and functions, these are collectively called members.

- The functions and variables usually grouped under two sections, private and public to denote which of the members are private and which of them are public. Note that these keywords are followed by a colon.

- The members that have been declared as private can accessed only from within the class while public members can be accessed from outside the class also.

-The data hiding (using private declaration) is the key feature of object oriented programming.

- The use of the keyword private is optional. By default, the members of class are private.

- If both the labels are missing then, all the members are private. Such a class is completely hidden from the outside world and does not serve any purpose.

- Only the member functions can have access to the private data members and private functions.

- The binding of data and functions together into a single class-type variable is referred to as encapsulation.

 

A Simple Class Example

                        class item

 

                        {

 

                                    int number;                            // variables declaration

 

                                    float cost;                                //private by default

 

                          public:

 

                                    void getdata(int a, float b);   //functions declaration

 

                                    void putdata(void);                //using prototype

 

                        };

 

We usually give a class some meaningful name, such as item. This name becomes a new type identifier that can be used to declare instances of  that class type. The functions are declared, not defined. Actual function definitions will appear later in the program. The data members are usually declared as private and the member function as public.

 

Creating Object

 

Once a class has been declared, we can create variables of that type by using the class name ( like other built-in type variable). For example,

                        item x; // memory for x is created

creates a variable x of type item. The class variables are known as objects. Therefore x is called an object of type item. We may also define more than object in one statement. Example:

                        item x,y,z;

 

The necessary memory space is allocated to an object at this stage.

 

Accessing Class members

 

The private data of a class can be accessed only through the member functions of that class. The main (  ) cannot contain statements that access number and cost directly. The following is the format for calling a member function:

                        Object-name.function-name( actual-arguments);

For example, the function call statement

                        x.getdata(100,75.5);

 

is valid and assigns the value 100 to number and 75.5 to cost of the object x by implementing the getdata() function.  Remember, a member function can be invoked only by using an object (of the same class).

The statement

                        x.number=100;

 

is also illegal.

A variable declared as public can be accessed by the objects directly. Example:

                        class xyz

 

                        {

 

                                    int x;

 

                                    int y;

 

                        public:

 

                                    int z;

 

                        };

 

                        …….

 

                        …….

 

                        xyz p;

 

p.x=0;             // error, x is private

 

p.z=10;            // OK, z is public

 

…….

 

…….

 

Note that the use of data in this manner defeats the very idea of data hiding and therefore should be a voided.

 

 


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .