Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
68 views11 pages

Unit IV Notes Part I

The document discusses classes and objects in C++. It defines a class as combining related data and functions together to create a data type used to make objects of that type. A class defines properties (data members) and behaviors (member functions) of real-world entities. The class syntax includes access specifiers like private and public that determine whether data/functions can be accessed within or outside the class. Member functions can be defined inside or outside the class. Objects are declared as instances of a class and access class members and functions. Static class members include data that is shared among all objects and functions that can only access static data.

Uploaded by

atul211988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views11 pages

Unit IV Notes Part I

The document discusses classes and objects in C++. It defines a class as combining related data and functions together to create a data type used to make objects of that type. A class defines properties (data members) and behaviors (member functions) of real-world entities. The class syntax includes access specifiers like private and public that determine whether data/functions can be accessed within or outside the class. Member functions can be defined inside or outside the class. Objects are declared as instances of a class and access class members and functions. Static class members include data that is shared among all objects and functions that can only access static data.

Uploaded by

atul211988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit IV

Classes and Objects


DEFINITION AND DECLARATION OF A CLASS

A class in C++ combines related data and functions together. It makes a data type
which is used for creating objects of this type.

Classes represent real world entities that have both data type properties
(characteristics) and associated operations (behavior).

The syntax of a class definition is shown below :

Class name_of _class

private : variable declaration; // data member

Function declaration; // Member Function (Method)

protected: Variable declaration;

Function declaration;

public : variable declaration;

Function declaration;

};
Here, the keyword class specifies that we are using a new data type and is followed by the class
name.

The body of the class has two keywords namely :

(i) private (ii) public

In C++, the keywords private and public are called access specifiers. The data
hiding concept in C++ is achieved by using the keyword private. Private data and functions can
only be accessed from within the class itself. Public data and functions are accessible outside
the class also. This is shown below :

Class

Private

Can only be accessed from


data members

and
within the class
member functions

Public

Can only be accessed from


data members

and
outside the class
member functions

Data hiding not mean the security technique used for protecting computer databases.

The security measure is used to protect unauthorized users from performing any operation
(read/write or modify) on the data.

The data declared under Private section are hidden and safe from accidental
manipulation. Though the user can use the private data but not by accident.
The functions that operate on the data are generally public so that they can be
accessed from outside the class but this is not a rule that we must follow.

MEMBER FUNCTION DEFINITION

The class specification can be done in two part :

(i) Class definition. It describes both data members and member functions.
(ii) Class method definitions. It describes how certain class member functions
are coded.
We have already seen the class definition syntax as well as an example.

In C++, the member functions can be coded in two ways :

(a) Inside class definition


(b) Outside class definition using scope resolution operator (::)
The code of the function is same in both the cases, but the function header is
different as explained below :

Inside Class Definition:

When a member function is defined inside a class, we do not require to place a


membership label along with the function name. We use only small functions inside the class
definition and such functions are known as inline functions.

In case of inline function the compiler inserts the code of the body of the function at
the place where it is invoked (called) and in doing so the program execution is faster but
memory penalty is there.

Outside Class Definition Using Scope Resolution Operator (::) :

In this case the function’s full name (qualified_name) is written as shown:

Name_of_the_class :: function_name

The syntax for a member function definition outside the class definition is :

return_type name_of_the_class::function_name (argument list)

body of function

}
Here the operator::known as scope resolution operator helps in defining the member
function outside the class. Earlier the scope resolution operator(::)was ised om situations where
a global variable exists with the same name as a local variable and it identifies the global
variable.

DECLARATION OF OBJECTS AS INSTANCES OF A CLASS

The objects of a class are declared after the class definition. One must remember
that a class definition does not define any objects of its type, but it defines the properties of a
class. For utilizing the defined class, we need variables of the class type. For example,

Largest ob1,ob2; //object declaration

will create two objects ob1 and ob2 of largest class type. As mentioned earlier, in
C++ the variables of a class are known as objects. These are declared like a simple variable
i.e., like fundamental data types.

In C++, all the member functions of a class are created and stored when the class is
defined and this memory space can be accessed by all the objects related to that class.

Memory space is allocated separately to each object for their data members. Member
variables store different values for different objects of a class.

The figure shows this concept


Common for all objects

Member Member Member function3

Memory allocated when

member functions are defined


Object 1 Object 2

data member data member 1

data member data member 2

Memory allocated when

objects declared

A class, its member functions and objects in memory.

ACCESSING MEMBERS FROM OBJECT(S)

After defining a class and creating a class variable i.e., object we can access the data
members and member functions of the class. Because the data members and member
functions are parts of the class, we must access these using the variables we created. For
functions are parts of the class, we must access these using the variable we created. For
Example,

Class student

private:

char reg_no[10];

` char name[30];

int age;

char address[25];
public :

void init_data()

- - - - - //body of function

- - - - -

void display_data()

};

student ob; //class variable (object) created

- - - - -

- - - - -

Ob.init_data(); //Access the member function

ob.display_data(); //Access the member function

- - - - -
- - - - -

Here, the data members can be accessed in the member functions as these have private
scope, and the member functions can be accessed outside the class i.e., before or after the
main() function.

STATIC CLASS MEMBERS

Data members and member functions of a class in C++, may be qualified as static.

We can have static data members and static member function in a class.

Static Data Member: It is generally used to store value common

to the whole class. The static data member differs from an ordinary data member
in the following ways :

(i) Only a single copy of the static data member is used by all the objects.
(ii) It can be used within the class but its lifetime is the whole program.
For making a data member static, we require :

(a) Declare it within the class.


(b) Define it outside the class.
For example

Class student

Static int count; //declaration within class

-----------------

-----------------

-----------------

};

The static data member is defined outside the class as :

int student :: count; //definition outside class

The definition outside the class is a must.

We can also initialize the static data member at the time of its definition as:

int student :: count = 0;

If we define three objects as : sudent obj1, obj2, obj3;

Static Member Function: A static member function can access only


the static members of a class. We can do so by putting the keyword static before the name of
the function while declaring it for example,

Class student

Static int count;

-----------------

public :

-----------------

-----------------

static void showcount (void) //static member function


{

Cout<<”count=”<<count<<”\n”;
}

};

int student ::count=0;

Here we have put the keyword static before the name of the function shwocount ().

In C++, a static member function fifers from the other member functions in the following
ways:

(i) Only static members (functions or variables) of the same class can be
accessed by a static member function.
(ii) It is called by using the name of the class rather than an object as given
below:

Name_of_the_class :: function_name

For example,

student::showcount();

FRIEND CLASSES
In C++ , a class can be made a friend to another class. For example,

class TWO; // forward declaration of the class TWO

class ONE

………………………

…………….

public:
……………..
……………..
friend class TWO; // class TWO declared as friend of class ONE
};

Now from class TWO , all the member of class ONE can be accessed.

You might also like