Sei sulla pagina 1di 18

Classes and Objects

Pooja Jain Senior Lecturer, JUIT Waknaghat

Class & Objects

C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can declare and define both data and functions that are hidden from the external use. The binding of data and functions together into a single class type variable is known as encapsulation.

The general form of declaration of a class:


class class_name { private: variable declaration ; function declaration ; public: variable declaration ; function declaration ; };

// data members // member functions // data members // member functions

Cont.

The specifier starts with the keyword class, followed by the class_name. The body of the class is delimited by the braces { } and terminated by the semicolon ( ; ).

The variables and functions within the body of the class are collectively known as members.
The data or variables declared within the class are known as data members, while the functions declared/defined within the class are known as member functions.

private and public keywords

The data members and the member functions are usually grouped under two sections: private and public, which are known as the access specifiers. These keywords are followed by the colon ( : ). If no specifier is used, then the members are private, by default. The members (data and functions) declared in the private mode can be accessed only from within the class, while the members declared in the public mode can be accessed from outside the class also. The private data member and the private member functions can only be accessed by the member functions of the class. Generally, it is common practice to declare the data members in the private mode and member functions in the public mode. Thus, the data is hidden, and is safe from the accidental manipulations. On other hand, the functions that operate on the data are declared in the public mode, so that they can be accessed from outside the class. However, there may arise some situations when one needs to use private functions and public data

Accessibility of public and private members


private:
Data member1 Not accessible from outside the class Member function1

public:
Data member2 Accessible from outside the class

Member function2

Notations used:
Indicates that the member (data or function) is not accessible Indicates that the member (data or function) is accessible

#include<iostream.h> class Rectangle { float length, breadth ; // Data members within the class rectangle, // which are private by default public: void enterdata(void) ; // Member function declarations within the void disparea(void) ; // class rectangle, which are declared as public }; //******************Member Function Definitions************** void Rectangle :: enterdata(void) { cout << "\n Enter the length of the rectangle: " ; cin >> length ; cout << "\n Enter the breadth of the rectangle: " ; cin >> breadth ; } void Rectangle :: disparea(void) { float area = length * breadth ; cout << "\n The area of the rectangle is = " << area << endl ; }

Example:

Cont.
//*****************Main Function***************************** void main(void) { Rectangle r1, r2 ; // a1 and a2 are objects of the // user-defined type rectangle cout << "\n First rectangle-->\n" ; r1.enterdata( ) ; // Call Member Functions r1.disparea( ) ; cout << "\n Second rectangle-->\n" ; r2.enterdata( ) ; // Call Member Functions r2.disparea( ) ; }

Defining member functions


There are two ways to define the member functions: Outside the class. Inside the class. Normally, only small functions are defined inside the class definition. However, it is good programming practice to define the member functions outside the class.

Defining member function outside the class:

Member functions that are declared inside the class have to be defined separately outside the class.
The syntax of a member function definition is: return_type class_name :: function_name(arguments) { Body of the function ; }

Here, double colon (::), is the scope resolution operator. The combination of class_name and :: tells the compiler that the member function function_name belongs to the class class_name. For example, in the previous program, the member functions are defined outside the class as: void Rectangle :: enterdata(void) { cout << "\n Enter the length of the rectangle: " ; cin >> length ; cout << "\n Enter the breadth of the rectangle: " ; cin >> bredth ; }

Defining the member functions inside the class:

The general form of defining the member function inside the class is:
return-type function-name(arguments) { Function body ; }

Example:
# include<iostream.h> class Rectangle // Name of the class { float length, breadth ; // Data members which are private by default // Definition of the member functions within the class*********** public: void enterdata(void) { cout << "\n Enter the length of the rectangle: " ; cin >> length; cout << "\n Enter the bredth of the rectangle: " ; cin >> breadth; } void disparea(void) { float area = length * breadth ; cout << "\n The area of the rectangle is: " << area << endl ; } } ; // Class definition terminated by the semicolon ( ; )

Creating Objects

After defining the class, one can declare objects of that class in the main( ) function. The syntax for declaring an object is: class_name obj-1, obj-2, -------,obj-N ;

For example, in the previous programs, the first statement in the main( ) i.e. Rectangle a1, a2 ; defines two objects of class Rectangle. Defining an object is similar to defining a variable of any data type. The space is also set aside for the object in the memory, like other variables.

Cont.

Objects can also be created by placing their names immediately after the closing brace, as in the case of structures. For example, the class definition class Rectangle { private: int length, breadth ; public: void enterdata( ) ; void disparea( ) ; } a1, a2 ;

would create objects a1 , a2 of type Rectangle. Generally, the objects are declared close to the place where they are used.

Calling members

Only an object of a class can access the members of that class. The syntax for calling a member of the class is:

object_name . data_member ;

// For calling the data member // of the class

object_name . member function(actual argumemnts ) ; // For calling // the member function of the class

The dot operator ( . ) is known as class member access operator. For example, in the program, the statements that call the member functions are given as: a1.enterdata( ) ; a1.disparea( ) ;

Cont.

Note that the objects of a class can access only those members of the class that are declared in public mode.

The private members of a class cannot be accessed by the objects of that class.
Thus, one cannot access the data members length and breadth, of the above programs: using the notation: a1.length ; // Not valid a2.breadth ; // Not valid because they have been declared in the private mode. Since, the member functions enterdata( ) and disparea( ) are declared in the public mode, they can be accessed by the objects of that class.

Extension to Structures in C++

So far it has been discussed that structures are used to group data and classes are used to group both data and functions. In C++, the structures can also be used to group both data and functions, like the classes. The only difference between structure and class is that in a structure the members (data and function) are public by default, while in a class the members are private by default.

The following declarations of the class and structure accomplish the same task.
class Rectangle { // private by default float length, breadth ; public: void enterdata(void) ; void disparea(void) ; }; struct Rectangle { // public by default void enterdata(void) ; void disparea(void) ; private: float length, breadth ; };

Thus in C++, a structure can be thought of as a class. However, it is general practice to use structure to group data, and classes to group both data and functions.