Dr Bhargavi Dept of CS CHRIST 9426669020 Classes
Dr. Bhargavi Dept of CS CHRIST 9426669020
Classes are extension of the idea of structure used in C � What is class? � It is a new way creating and implementing user defined data type. � Is structure similar to class? � Not completely. How? � Eg. struct complex { float x; float y; }; struct complex c 1, c 2, c 3; � Can we assign values to c 1, c 2 and c 3? � Yes. � Can we add two of them and assign to third? Eg. c 3=c 1+c 2; � No. � Means? It is limitation of C. o
One more limitation: C do not permit data hiding. How? � All the structure variables can be accessed by any function for any scope. � Means, structure members are public by default. � Do we want it to be public always? � No. � Solution? � Class members are private by default. � Class provides data hiding and user defined data types. � Class represents OOPs concepts of Encapsulation, Data Hiding, Inheritance and Data Abstraction. �
�A CLASS is a way to bind data and its associated functions together. � It allows data and functions to be hidden from external use. � When defining a class, we are creating a new abstract data type which can be treated as build-in data types. � Two parts: �Class Declaration, describes type and scope of members. �Class Function Definitions, describes how functions are implemented.
� What are the visibility labels? � Ans: Keywords private and public. � Private members are accessible inside the class. � Public members are also accessible outside the class. � When visibility labels are missing in the class, by default everything is private. � Those classes are isolated and hidden from world and serves no purpose. � Binding of data and functions together into single class type variable is referred as encapsulation.
� Private data can be accessed only through member functions. � Main() cannot directly access private members of the class. � Syn: object-name. function-name([optionalarguments]); � Eg. x. getdata(10); � Public data of a class can be accessed through main() function directly. � But, accessing data members directly in main() defeats OOPs concept of data hiding. � So, avoid accessing data members directly through main().
Two ways: � Outside the class � Inside the class � Outside the class definition: � � Needs identity label in header. � What is that? � Identity label tells compiler that function belongs which class. � Syntax: return-type class-name : : function-name (argumentlist) { function body } � Advantage: same function name is permitted in multiple class. � Eg. munna, beta, babu, sweetu, guddy, cutie, etc ; ) � Inside the class definition: � Just replace function declaration with function definition inside the class. � It is treated as inline function. � inline small functions are defined inside the class. � Eg. 5_1 Class. Implementation. cpp
� Functions defined inside class becomes inline. Agree? � Yes. � What if we want the functions defined outside the class to become inline? � Is it possible? � Yes. How? Lets see>
�A member function can be called inside another function of the same class. � This is called nesting of member function. � Eg. 5_2 nesting_member_functions. cpp
� Some situation needs private access to functions also. � Situations: �Delete bank account �Increment to employee � These functions must be private. � How to access these functions? � Using another function of the same class. � Objects cannot directly access these function using dot (. ) operator. � Eg. 5_3 Private. Member. Function. cpp
� Characteristics: � Initialized to zero when first obj is created � Only one copy of member for entire class shared by all objects � Visible only within the class � But, Lifetime is entire program � Note, type and scope of each static member variable must be defined outside the class definition. � Y so? � Because, static data members with class are stored separately. They are not part of an object. Only one copy is stored per class. � Also called class variables.
When are they initialized? � When the objects are created. � Remember: � Static variables are declared in class and defined outside class in the scope of source file. � Also known as non-inline member functions. � It can be initialized other than 0 explicitly. � Use? � Maintain values that are common to entire class. � Eg. maintain counter of objects, records, operations, login failures, etc � Eg. 5_static. Member. cpp � Eg. 5_4 Static. Class. Member. cpp � Eg. static. Member. Of. Class. cpp �
Properties: � Can access only static variables/functions declared in the same class. � Static member function can be called using class name instead of its objects. Class-name: : function-name; � Means, to call static member function, you do not need an object. � Static member functions have a class scope and they do not have access to the this pointer of the class. � You could use a static member function to determine whether some objects of the class have been created or not. � Eg. 5_5 Static. Member. Function. cpp � Eg. static. Member. Functionof. Class. cpp �
� � � � Can array be of any data type? Yes. Then array can be user defined data type also such as struct and class? Yes. how? Array of objects. Definition: array of variables that are of type class are called array of objects. Access method is also same as array. Storage mechanism? Similar to multi-dimensional array. Require memory for only class variables and not for class functions. See next fig. 5_6_array_of_objects. cpp
Can we use obj as function argument? � Yes. � How? � Two ways. � 1. Copy the obj and pass it to function (pass by value) � 2. Only pass address of obj to function (pass by reference) � Which method is Efficient? � Pass by reference. � See next fig. � Eg. 5_7 obj_as_argument. cpp � Obj can also be passed as an argument to non-member function. � But, such function can be passed to only public functions using objects. � Also such functions cannot access private data. � We will see example later in this chapter. �
� If we need common function of multiple class what to do? � Can two classes share same function? � Eg. academics and nonacademic are two classes which needs a common function called income_tax() � Friend function can access private data of other class. � Friend function need not be member of any of these two classes. � How to do that?
� Is not defined in the scope of class where it is declared. � So, it cannot be called using obj of that class. � Like a normal function, it is invoked without help of any object. � It cannot access class data variables directly similar to class functions. � Can be declared in any scope, private or public. � Usually has objects as arguments. � Used for operator overloading.
� Eg. 5_8_friend_function. cpp 5_9_function_friendly_to_two_classes. cpp 5_10_swapping-private-data-of-classes. cpp friend. Function. cpp friend. Class. cpp friend. Function. In. Another. Class. cpp friend. Var. cpp global. Friend. Function. cpp
� Can we return objects same as arguments? � Can we create objects within a function and return to another function? � Yes. � Lets see how. � Eg. 5_11_returning_objects. cpp
� If member function does not alter any data in the class, then we may declare it as a const member function. � Qualifier ‘const’ is applied to both, declaration and definition. � Compiler will give error if such functions carry assignment statements. � Eg. void mul(int, int) const; double balance() const;
END OF CHAPTER 5
- Slides: 33