Anda di halaman 1dari 20

1

Features of OOP
Emphasis on data rather than procedure. Programs are divided into objects. Data structures are designed such that they characterize the objects. Functions that operate on the data of an object are tied together in the data structure. Objects may communicate with each other through functions. Data is hidden & can not be accessed by external function. New data and functions can be easily added whenever necessary. Follows bottom up approach in program design.

Concepts used in OOP

Objects Classes Data Abstraction Data Encapsulation Inheritance Polymorphism Dynamic binding Message Passing

Objects
Runtime entities in the Object Oriented System (smith, circle). It contains actual values and methods. All objects have identity and are distinguishable. They are created just like variables in any programming language.

Classes and Objects


A class is an extension of the idea of structure used in C. New way of creating and implementing a user defined data type. Struct data type can not be treated like built-in data type ( which has operations). struct complex { float x; float y; } c1, c2, c3 c3= c1 +c2 is not possible. Another important limitation of structures is that they do not permit data hiding.

Members of a structure can be directly accessed anywhere within their scope. C++ attempts to bring user defined data types as close as possible, to the built-in data_types and also provides a facility to hide the data which is an important concept of OOP.

Class
It is a data type in C++ which holds data and functions. It has two parts: Class declaration
(same as structure declaration)

Class function definition


(describes how the functions are implemented)

General Form of class

class class_name { private: variable_declarations; function_declarations;


data members member functions

public: variable_declarations; function_declarations; }


class item { private: int num; float cost ; public: getdata(int a, float b); void putdata(void);

Class is usually grouped under two sections Private & Public. Private members can be accessed only within the class (data hiding) and not visible / accessible outside the class. o Public members can be accessed from outside the class also. Only member functions (private/public) can have access to the private members (data & functions). Binding of data and functions together into a single class_type variable is called Encapsulation

General convention for class is to make the value part (variable members) of the data type private and make the functions public. Actual function definitions can appear within the class or outside the class, anywhere in the program. Objects can be created by simple Declaration such as: item x, y, z; (item is a class) Defining member functions

Inside the class Outside the class class item {int num; float cost; (default private)
//declaration//

public:void getdata (int a, float b);


void putdata (void); { cout << num << \n; cout << cost << \n; } }

//definition//

Outside the class return_type class_name :: func_name (arg_list) { body } tells compiler that fu-name belongs to which class class item { int num; float cost; public: void getdata (int a, float b); //declarations void putdata (void); } void item :: getdata (int a, float b); { num = a; Can access cost = b; private data }; (num & cost) void item :: putdata (void); { cout << num << \n; cout << cost << \n; }

10

Making an outside function inline


One of the objectives of OOP is to separate the details of implementation from the class definition. Good practice is to define the member functions outside the class. We can define such functions as inline by just using qualifier inline. When a function is defined inside a class, it is treated as an inline function that means only small functions are defined insides the class.
class item { : public void getdata (int a, float b); }; inline void item :: getdata (int a, float b) { num = a; cost = b; }

11

Accessing class members

Note that objects communicate by sending and receiving messages through the member functions.

item X; X . getdata (10,7.5) X . putdata ( ) ;


Objects of the class can call only public member functions by using dot operator. In public section, if we define a variable val, then it can be accessed directly outside in the main program.

X .val + 10;
If val is defined in private section, then
above statement is illegal.

12

Nesting of member functions


Member function can be called by using its name inside another member function of the same class. This is called nesting of member function.

Private member function


Some situations may require to hide member functions as well. For examples: o Deletion of an account in customer file, o Providing increment etc.

class sample { int m; void get (void); public: void update (void); }

13

void sample :: update (void) { get ( ) }


private member function

sample S; S . update ( ) legal S . get ( ) illegal

Memory Allocation for Object


When a class is declared, member functions are created and placed in the memory space only once. But when object is declared as a specific class type, same member functions are used and no separate allocation done. But space for member variables is allocated for each object.

14

Static member variables

Used to maintain values common to entire class. Type and scope of each static member variable must be declared in the beginning of the class definition and defined outside. All objects share same copy of static variable. They are associated with class rather than object, therefore, called class variables. They are visible only within the class but its lifetime is the entire program. It is initialized to zero when the first object of its class is created.

15

class item { static int count; int num; public : void getdata (int a ) { num = a; count ++; } void getcount (void ) { cout << count:<< \n<<count; } } main ( ) { item a, b, c; int item :: count; // count of item is defined a . getdata (10); b . getdata (20); c . getdata (30); a.getcount ; b.getcount ; c.getcount ; } Output: count: 3 count: 3 count: 3

16

Static member functions


Static function can have access to static members (functions / variables ) declared in the same class. Static member function can be called using the class name (instead of its objects) Static function call item :: static_fun; Non static function call item T; T. fun_name;

Arrays of objects
item a[10]; a[i] . getdata; a[i] . getcount;

17

Objects as Arguments in the Function


There are two ways of passing object as a parameter. call by value call by reference

A copy of the entire object is passed to the function

Only the address of the object is transferred to the function

Object can be passed to any function (member functions of the same class or to a non_member function).
class item { int num; public : void getdata (int a ) { num = a; } void add (item S1, item &S2 ) { cout << S1 . num + S2 . num ; }

18

Returning objects
Functions can return objects.
class complex { private: float x; float y; public : void inputdata (float r , float i) { x=r; y=i; } void show (complex c) { cout << c.x << +i << c.y << \n; } complex sum (complex c1, complex c2) { complex c3; c3 . x = c1 . x + c2 . x; c3 . y = c1 . y + c2 . y; return (c3); } } main ( ) { complex a, b, c; a . inputdata (3.1, 2.1); b . inputdata (2.8, 3.4); c = sum (a, b) ; cout << c . show ( c) ; }

19

Constant Member Function


If member function does not alter any data in the class, we may declare it as const member function by appending a function declaration with the qualifier const. void mult (int, int) const ; Error message is generated if such function tries to alter the data.

Pointers to Members
Address of a member (data / function) can be obtained by applying & operator to a fully qualified class member name. A class using pointer can be declared using the operator ::* with the class name.

20

class item { int m; public : void show ( ) ; }

Pointer to the members of a class item is defined by item ::* Example: Pointer ptr to member m is defined as int item :: * ptr = &item :: m; int *ptr = & m
(m is a member of class item) Invalid

Here &item :: m means address of m. ptr defined above can be used to access the member m inside the member functions and not outside the class. item a; // inside a member function
cout << a . * ptr Or cout << a . m

Anda mungkin juga menyukai