
UNIT 5 OBJECTS AND CLASSES Objects and Classes Structure 5.1 Introduction Objectives 5.2 C++ as Physical Objects 5.2.1 Objects 5.2.2 Classes 5.2.3 A Complete View of a Class 5.2.4 Scope Operator 5.3 Data Hiding and Encapsulation 5.4 Scope of Class and its Members 5.4.1 Scope of Private and Protected Members 5.4.2 Scope of Public Members 5.5 Global Class and Local Class 5.5.1 Global Class 5.5.2 Local Class 5.6 Functions in Class 5.6.1 Inline Functions 5.6.2 Static Member Functions 5.6.3 Const Member Functions 5.6.4 Friend Functions 5.7 Nested Classes 5.8 Summary 5.9 Answers to SAQs 5.1 INTRODUCTION This unit describes about objects and classes. The use of objects in C++ defines the way programs are designed and written. Classes are a software construct that can be used to emulate a real world object. Classes encapsulate data and functions. The fundamental idea behind object-oriented language is to combine into a single program entity, both data and the functions operate on that data. Such an entity is called an object. Till now we have studied different issues, functions related to object oriented programming language like C++. This unit will explain declaration of classes in C++, use of data hiding and encapsulation, scope of classes and its members. Further, different functions are also described with the help of examples in this unit. Objectives After studying this unit, you should be able to • visualize C++ as physical objects, • define objects and classes, • define data members and member functions, • understand scope of the data members and member functions, • understand scope operator for the member function within a class, • use the scope within the classes, • define types of member functions within the class, and 5 Object-oriented • use nested class and communication between classes. Programming Using C++ 5.2 C++ AS PHYSICAL OBJECTS As you know objects are the basic entities in an object-oriented system. They may represent a person, a place, a table, a vehicle, time or any user defined data, any item that the program needs to handle. 5.2.1 Objects Object is defined as an identifiable entity with some characteristic and behaviour. For instance, we can say ‘Orange’ is an object. Its characteristics are : it is spherical shaped, its colour is orange, etc. Its behaviour is: it is juicy and it tastes sweet-sour. While programming using OOP approach, the characteristics of an object are represented by its data and its behaviour is represented by its functions associated. Therefore, in OOP programming object represents an entity that can store data and has its interface through functions. Programming problem is analyzed in terms of objects and the nature of communication between them, so it is necessary for you to understand the nature of communication between objects. 5.2.2 Classes A class is a group of objects that share common properties and relationships as shown in Figure 5.1. This figure shows how these objects communicate with each other. Let us see in Example 5.1 below how we can declare a class. Object Data Member Function Member Function Object Obj ect Data Data Member Function Member Function Member Function Member Function Figure 5.1 : Communication between Objects Example 5.1 A typical class declaration would look like Class item { int number; float cost; public: void getdata(int a, float b); 6 void putdata(void); } Objects and Classes For instance, a software model of a car, a car “class”, might contain data about the type of car and abilities such as accelerate or decelerate. A class is a programmer defined data type that has data, its members, and abilities, its methods. An object is a particular instance of a class. This is best understood by an analogy with a built- in data type such as int (integer). int x; /* declares x to be a variable of type int.*/ car impala; /* declares impala to be an object of class car.*/ 5.2.3 A Complete View of a Class A class is a logical method to organize data and functions in the same structure. They are declared using keyword class, whose functionality is similar to that of the C keyword struct, but with the possibility of including functions with a variety of scope (which will be discussed later in this unit) as members, instead of only data. Now let us have a view of a complete class : class class_name { permission_label_1: member1; permission_label_2: member2; ... } object_name; where class_name is a name for the class (user defined type) and the optional field object_name is one, or several, valid object identifiers. The body of the declaration can contain members, that can be either data or function declarations, and optionally permission labels, that can be any of these three keywords : private, public or protected. They make reference to the permission which the following members acquire : • private members of a class are accessible only from other members of their same class or from their “friend ” classes. • protected members are accessible from members of their same class and friend classes, and also from members of their derived classes. • public members are accessible from anywhere the class is visible. If we declare members of a class before including any permission label, the members are considered private, since it is the default permission that the members of a class declared with the class keyword acquire. For example : class crectangle { int x, y; public: void set_values (int,int); int area (void); } rect; Declares class crectangle and an object called rect of this class (type). This class contains four members: two variables of type int (x and y) in the private section (because private is the default permission) and two functions in the public section: set_values ( ) and area ( ), of which we have only included the prototype. 7 Object-oriented In the previous example, where crectangle was the class name (i.e., the user-defined Programming Using C++ type), and rect was an object of type crectangle. Now consider the following declaration of a variable : int a; What is the difference between object declaration and variable declaration? When you analyse you will find that int is the class name (type) and a is the object name (variable). On successive instructions in the body of the program we can refer to any of the public members of the object rect as if they were normal functions or variables, just by putting the object’s name followed by a point and then the class member. For example : rect.set_value (3,4); myarea = rect.area( ); but we will not be able to refer to x or y since they are private members of the class and they could only be referred from other members of that same class . Example 5.2 # include <iostream.h> class crectangle { int x, y; public: void set_values(int, int); int area (void) {return(x*y);} }; void crectangle:: set_values (int a, int b) { x = a ; y = b ; } int main ( ) { crectangle rect; rect.set_values (3, 4 ); cout << “area: “ << rect.area ( ) ; } Output : rect area : 12 5.2.4 Scope Operator If you have noticed in the previous example we have used the operator : : of scope in the definition of set_values ( ). Notice that we have defined the behaviour of function area ( ) within the definition of the crectangle class – given its extreme simplicity. Whereas set_values( ) has only its prototype declared within the class but its definition is outside. So in this outside declaration we must use the operator of scope “::”. The scope operator (::) specifies the class to which the member being declared belongs, granting exactly the same scope properties as if it was directly defined within the class. The reason why we have made x and y private members (remember that if nothing else is said all members of a class defined with keyword class have private access) it is because we have already defined a function to introduce those values in the object (set_values( )) and therefore the rest of the program does not have a way to directly access them. 8 One of the greater advantages of a class is that we can declare several different objects Objects and Classes from it. For example, following with the previous example of class crectangle, we could have declared the object rectb in addition to the object rect. Example 5.3 # include <iostream.h> class crectangle { int x, y ; public : void set_values (int, int) ; int area (void) {return (x * y) ; } }; void crectangle :: set_values (int a, int b) { x = a; y = b ; } int main ( ) { crectangle rect, rectb; rect.set_values (3,4); rectb.set_values (5,6); cout << "rect area: " << rect.area( ) << endl; cout << "rectb area: " << rectb.area( ) << endl; } Output : rect area: 12 rectb area: 30 Notice that the call to rect.area ( ) does not give the same result as the call to rectb.area ( ). This is because each object of class crectangle has its own variables x and y, and its own functions set_value ( ) and area ( ). On that is based the concept of object and object-oriented programming. In that data and functions are properties of the object, instead of the usual view of objects as function parameters in structured programming. In this and the following sections we will discuss advantages of this methodology. In this concrete case, the class crectangle, of which there are two objects : rect and rectb, each one with its own member variables and member functions. 5.3 DATA HIDING AND ENCAPSULATION Encapsulation refers to wrapping up data and associated functions into one single unit.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages18 Page
-
File Size-