Class and Objects

Limitation of C structure

• A class is an extension of the idea of structure in C.
• It is a new way of creating and implementing a user defined data type.
• We know that structures provide a method for packing together data of different types.
• A structure is a convenient tool for handling a group of logically related data items.
• Once structure has been defined, we can create variables of that type using declaration that are similar to built in data type declarations.

Creating Structure in C/C++

Ponits to be remember:
Struct keyword to begin definition of structure.
Structure Tag(name) is a valid identifier.
Once structure is created, variables of the structures can be created taking data type as defined structure.

Limitation of C Structures

➢ The standard C does not allow the struct data type to be treated like built-in data types. For example
struct complex
{
float real;
float imag;
};
struct complex c1,c2,c3; // in C
➢ The complex numbers c1,c2,c3 can easily be assigned values using the dot operator.
➢ But we cannot add two complex numbers or subtract one from the other. That is c3=c1 + c2; is illegal in C.

Enhancement in C++ Structures


• Also data hiding is not permitted in C, that is structure members can be directly accessed by the structure variable by any function anywhere in their scope.
• C++ supports all the features of structures as defined in C. But C++ has expanded its capabilities further to suit its OOP philosophy.
• C++ attempts to bring the user defined types as close as possible to the built in data types, and also provides a facility to hide the data.
• In C++, a structure can have both variables and functions as members.
• It can also declare some of its members as private so that cannot be accessed directly by the external functions.

Encapsulation with Structure in C++


• In C++, there may be two types of members in Structure.
1. Data members and
2. Member functions.
• Functions defined within structure can operate any member of structures.
• By default all members of structure are public so can be accessed directly from outside the structure.
• The following simple program shows the use of structures with member data and function both.

Encapsulation with Structure in C++


Encapsulation with Structure in C++


Output of above Program

Enter co-ordinates of 1st point:
x coordinate :10
y coordinate :20
Enter co-ordinate of points p2 :
x coordinate :15
y coordinate :25
P1= (10,20)
P2= (15,25)
P3(P1+P2)= (25,45)

Extension of Structure to Class

• C++ incorporates all these extension of structures in another user defined type called as class.
• There is very little syntactical difference between structure and classes in C++, so they can be used interchangeably with minor modifications.
• However, in most cases class is used for holding data and function in C++.
• The only difference between a structure and a class in C++ is that, by default, the member of a class is private while, by default, the members of a structure are public.

Class

• A class is user-defined data type that includes different member data and associated member functions to access on those data.
• Object-oriented programming (OOP) encapsulates data (attributes) and functions (behavior) into packages called classes.
• The data components of the class are called data members and the function components are called member functions.
• The data and functions of a class are intimately tied together.
• A class is like a blueprint. A programmer can create any number of objects of the same class. Classes have the property of information hiding.
• It allows data and functions to be hidden, if necessary, from external use.
• When defining a class, we are creating a new Abstract Data Type that can be treated like a built in data type.

Object

• An object is an instance of the class just like as variable of a structure.
• The class describes the attributes and behavior of the object. A class is considered as description of similar types
of objects.
• The member function needs to be defined within the class, just as an inline functions.
• Function definition within class is automatically considered to be the inline so there is no need of associated keyword inline inside class for function definition.
• Space in memory is allocated at the time when object are defined from class and not at the time of class declaration.
• Member functions are accessible only through the object of that class using the class member access operator dot (.).

Declaration of Class : Syntax

Accessing members of class
• Use dot operator (.) to access the member function of class by its objects.
• The members of a class to be accessed by its objects must be public members.
• The general syntax for accessing member function is
Object_name.Member_fun_name();
▪ Follow the example in next slide.
Output
Input Object1:Input an int, float and char:10 2.5 @
Input Object2:Input an int, float and char:20 4.5 #
Object 1:
Int=10
Float num =2.5
Char =@
Object2:
Int=20
Float num =4.5
Char =#

Class and Object: An Example

Class and Object : An example

Output of above Program
Enter co-ordinates of 1st point:
x coordinate :10
y coordinate :20
Enter co-ordinate of points p2 :
x coordinate :15
y coordinate :25
P1= (10,20)
P2= (15,25)
P3(P1+P2)= (25,45)

Defining member function

• Member functions of any class can be defined as follows:
– Inside the class definition
– Outside the class definition
• Inside the class definition
– The member function defines inside the class are considered as an inline automatically and no need of keyword inline.
– Entire function definition is placed inside the class.

Outside the class definition

– The member functions declared inside a class can be defined outside the class.
– The function definition outside a class consists of the function header with associated class label followed by scope resolution operator to represent the membership of that class and contains the body of the function.
• The general syntax of the function definition outside class definition will be as:
return-type classname::functionname(arguments..)
{
//function Body;
}

Example: Member Function Definition outside class

Nested Member Functions

• Calling a member function inside another member function- Nested member function: Example below
//nesting member function

Nested Member Functions

Private Member Function and Its Access

• In general, data members of a class are made private(for hiding) but we also can make member function as private if required.
• Such private member function are not accessible outside the class as private data.
• Private member function (if any) are only accessible to the other member functions of the class.

Private Member function :Example

Private Member function :Example

More about Member Function

• The member functions have some special characteristics that are often used in program development.
• Several different classes can use the same function name.
• The “membership label” will resolve their scope.
• Member functions can access the private data of the class. A non member function cannot do so. Exception-friend function
• A member function calls another member function directly without using the dot operator(as above read() inside update()
• The private member functions and data cannot be accessed by the object of the class directly.
• A function definition inside class definition as default behaves as inline but if it is defined outside class definition, we should use keyword inline to make it inline function.

Object as function Argument

• Like other variables, the object can be passed as function arguments. This can be done similarly in three ways as discussed earlier.
• A copy of entire object is passed to the function (pass by value)
• The object name is passed as reference (pass by reference)
• Only address of the object can be transferred to the function (pass by address)

Object as Function Arguments

Object as Function Arguments

Access of private data

• The member data are directly accessed by the objects of class defined in member functions( t1,t2 below) of the class using dot operator.
• From outside class, object can’t access the private data member directly

Pass by Reference: Look at defn and call

Pass by Address

Returning Object from Function

In above program, the prototype should be
time sum(time, time); // notice the return type as time
Function Should be defined like below:
Alternatively, We can write like below time sum(time); // notice the argument here
Function Should be defined like below:
• Notice the function call below:
time t3;
t3=t1.sum(t2); // Notice the difference here
• Here Object t1 is calling function sum() with only one arguement which is explicit argument in function.
• When object calls the member function, the data member accessed inside calling function are of the implicit argument( the object which calls function)
• In above function call, t1 is called implicit argument where as t2 is explicit argument.
• Notice the function definition below:
In T3.min= min+t2.min;
• The data members min and hrs accessed without dot operator represents the data member of object by which this function is called ( here it is t1 )
• The data members of explicit argument( here it is t2) are accessed by using dot operator with object name ( Above, t2.min, t2.hrs) Classes, Objects, and Memory
• When a class is declared, memory is not allocated to the data member of the class. So there exists a template, but data members cannot be manipulated unless an instance of this class is created by defining an object.
• When an object is created, memory is allocated only to its data members but not to member functions.
• Member functions are created and stored in memory only once when a class specification is declared.
• All objects of that class have access to the same area in the memory where the member functions are stored.
• The separate storage is allocated for every object’s data members since they contain their own values.

Class, Objects and Memory

0 Comments