Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Bhargavi Dept of CS CHRIST

Similar presentations


Presentation on theme: "Dr. Bhargavi Dept of CS CHRIST"— Presentation transcript:

1 Dr. Bhargavi Dept of CS CHRIST 9426669020
Classes and objects Dr. Bhargavi Dept of CS CHRIST

2 Structures v/s classes
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 c1,c2,c3; Can we assign values to c1,c2 and c3? Yes. Can we add two of them and assign to third? Eg. c3=c1+c2; No. Means? It is limitation of C.

3 Structures v/s classes
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. 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.

4 SPECIFYING A CLASS 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.

5 General form of class basic syntax

6 Visibility labels & encapsulation
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.

7 Data hiding

8 Class example

9 Accessing class members
Private data can be accessed only through member functions. Main() cannot directly access private members of the class. Syn: object-name.function-name([optional- arguments]); 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().

10 Defining member functions
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 (argument- list) { 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_1ClassImplementation.cpp

11 Making outside function inline
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->

12 Nesting of member functions
A member function can be called inside another function of the same class. This is called nesting of member function. Eg. 5_2nesting_member_functions.cpp

13 Private member function
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_3PrivateMemberFunction.cpp

14 Classes and arrays

15 Lab exercise: shopping list: Output

16 Memory allocation for objects and class. Done How?

17 Static data members 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.

18 Static data members 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_staticMember.cpp Eg. 5_4StaticClassMember.cpp Eg. staticMemberOfClass.cpp

19

20 Static member functions
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_5StaticMemberFunction.cpp Eg. staticMemberFunctionofClass.cpp

21 Array of objects Can array be of any data type? Yes.
Then array can be user defined data type also such as struct and class? 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

22 Array of objects

23 Object as functional requirement
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_7obj_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.

24 Object as functional requirement

25 Friendly functions 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?

26 Friendly functions

27 Friendly functions Characteristics
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.

28 Function of one class is friend function of another class

29 All function of one class can be friend function of another class

30 Friend function Eg. 5_8_friend_function.cpp
Eg. 5_9_function_friendly_to_two_classes.cpp Eg. 5_10_swapping-private-data-of-classes.cpp Eg. friendFunction.cpp Eg. friendClass.cpp Eg. friendFunctionInAnotherClass.cpp Eg. friendVar.cpp Eg. globalFriendFunction.cpp

31 Returning objects 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

32 Const member function 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;

33 END OF CHAPTER 5 THANK YOU


Download ppt "Dr. Bhargavi Dept of CS CHRIST"

Similar presentations


Ads by Google