Ticker

6/recent/ticker-posts

2. Classes and Objects

Class

Class is a way of binding data and its associated functions together. It combines data and functions into a single programming unit.

Syntax:

class class_name
{
  public or private or protected:
          variable declarations;
           function declarations;
};


Encapsulation: The binding of data and functions together into a single class type variable is known as encapsulation.

Object

A object is an instance of class. When a class is defined, no memory is allocated but when an object is created, memory is allocated.
Syntax:

class_object  object_name;


Accessing the members of class
After creating an object of a class, the data member and member functions of a class can be accessed using dot operator( . ) with the object.

Syntax:

object_name.data_member_name;   --> for data member

object_name.member_function_name;   --> for member function



Members of class

i. Data Member: The variables in the class are called data members. They can be of different datatypes.

ii. Member function: The functions inside the class or derived from any class is called member function of that class. These functions manipulates the data members inside the class.

Defining a member function from outside of class

A function can be defined as the member of the class from outside of class and can access the data of that class.
Here is the syntax for how we can define a member function from outside of class,

return_type  class_name : : function_name (arguments)

Characteristics of member function

i. Same function name can be used by different classes.
ii. Member functions can access private data of class. A non-member function cannot do so except  Friend function.
iii. A member function can call another member function directly without using the dot operator.


Member Access Specifier/Modifier

Access specifier provides the accessibility control of the members of class to its outside world. They are used to implement important feature of OOP known as Data Hiding.
Three access specifiers are:

i. Private: When this access specifier is used in class, the data members and methods of class are accessible only within the class. It means the data and functions defined in the particular class cannot be used by another class.
        By default all the members of class will be private if any access specifier is not specified.

ii. Public: It indicates that the data member and member function can be accessed from anywhere outside the class within the program.

iii. Protected: It indicates that the data member and member function are accessible within a class and from derived class.

Friend Function

C++ allows the mechanism in which a non-member function can access private data of class. This can be done by declaring a non-member function as friend to a class whose private data is to be accessed.

Characteristics of Friend function

i. Friend function cannot be called with the help of object of that class.
ii. Friend function cannot access member name directly. It has to use object name with dot operator.
iii. Friend function can be declared either in public or private part of the class without affecting its meaning.
iv. Friend function can access any object of any class in which it has been declared as friend.
v. Usually, friend function has object as argument.




Reactions

Post a Comment

0 Comments