0% found this document useful (0 votes)
2 views

classes and objects

This chapter introduces the fundamental concepts of classes and objects in object-oriented programming using C++. A class combines data and functions into a single entity, with access specifiers determining the visibility of class members. It also explains the syntax for defining classes, member functions, and the differences between structures and classes.

Uploaded by

purvaj059
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

classes and objects

This chapter introduces the fundamental concepts of classes and objects in object-oriented programming using C++. A class combines data and functions into a single entity, with access specifiers determining the visibility of class members. It also explains the syntax for defining classes, member functions, and the differences between structures and classes.

Uploaded by

purvaj059
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

𝑪𝑯𝑨𝑷𝑻𝑬𝑹 − 𝟕

CLASSES AND OBJECTS

INTRODUCTION
The elementary concept of object oriented programming begins with the use of classes and
objects. A class is a very powerful keyword in C++. A class declaration defines new user-defined
data types that link data and the code that manipulates the data. In other words, a class
combines data elements and functions (operations) into a single entity.
Let us look at the terminologies in procedural programming language and object
oriented programming.

Procedural Programming Object oriented programming


Variables Objects Classes
User-defined data types Structure Instance variables Methods Massage passing
members function
Function call

The data elements in a class are called member data and the functions in a class are
called member functions.
Definition and declaration of classes and objects:
A class definition is a process of naming a class and data variables, and interface operations of
the class.
A class declaration specifies the representation of objects of the class and set of operations that
can be applied to such objects.
The definition and declaration of a class indicates the following:
➢The data variables known as member data of a class, describe the characteristics of a
class.
➢Member functions are the set of operations that are performed on the objects of the class.
There may be zero or more member functions in a class. This is also known as interface.
➢The access control specifiers to the class members within a program are specified.
➢Class tag name for external operations for accessing and manipulating the instance of a class.
The General syntax for defining a class is as follows:
class user_defined_name
{
private:
Member data Member functions
protected:
Member data Member functions
public:
Member data Member functions
};
classes and objects

Keyword class is used to declare a class. User_defined_name is the name of the class.
Class body is enclosed in a pair of flower brackets. Class body contains the declaration of its
members (data and functions). There are generally three types of members namely
private, public and protected. These are discussed in the following section.
The class function definition describes how the class functions are implemented.
Example : Let us declare a class for representation of bank account.

class account
{
private: //implicit by default int accno;
char name[20[; char acctype[4]; int bal _amt;
public: //member functions
void get_data(); void displaydata();
}

In the above example, class name account is a new type identifier that is used to
declare instance of that class type. The class account contains four member data and two
methods or member functions. Both of these member data are private by default, while
both member functions are public by default.

The functions get_data( ) and put_data( ) are used to write instructions to perform operations
on member data. These two functions only can provide access to get member data from outside
the class. Therefore, the data cannot be accessed by any other function that is not a member of
the class account.

Access specifiers
An object is defined to be an instance of a class. Every data member of a class is specified by
three levels of access protection for hiding data and function members internal to the class. The
access specifiers define the scope of data. The access specifiers are indicated using
the following keywords.

private access means a member data can only be accessed by the member function. Members
declared under private are accessible only within the class. If no access specifier is mentioned,
then by default, members are private.

Example : private:
int x;
float y;
public
public access means that members can be accessed by any function outside the class also.
Example: public:
Void getdata();

Thus keyword public identifies both class data and functions that constitute the public interface
for the class.

Protected
The members which are declared using protected can be accessed only by the member
functions, friends of the class and also by the member functions derived from this class. The
members cannot be accessed from outside. The protected access specifier is therefore similar to
private specifier.

Members of the class


The members of a class can be data or functions. Private and protected members of a class can
be accessed only through the member functions of that class. No function outside a class can
include statements to access data directly. The public data members of objects of a class can be
accessed using direct member access operator (.)

The syntax for accessing class member is class-object. member-data class-object.member-


function(arguments);
Example : class rectangle
{
int l;
int b; public:
}

Member functions
Member functions can be defined in two places:
• Inside class definition
• Outside class definition

Inside class definition


To define member function inside a class the function declaration within the class is replaced
by actual function definition inside the class. A function defined in a class is treated as inline
function. Only small functions are defined inside class definition.

Syntax: return_type classname(member function) Example :


class rectangle

{
int length;
int breadth; public: void get_data ()
{
cin>>length; cin>>breadth;
}
void put_data (void)
{
cout <<length ; cout <<breadth;
}
};

Outside class definition


A function declared as a member of a class is known as member function. Member functions
declared within a class must be defined separately outside the class. The definition of
member function is similar to normal function. But a member function has an ‘identity label’ in
the header. This label tells the compiler which class the function belongs to. The scope of the
member function is limited to the class mentioned in the header.

Scope resolution operator :: is used to define the member function.


Syntax: For member function outside a class
return_type classname :: memberfunction(arg1, arg2, …, argn)

{
function body;
}

The member functions have following characteristics:

• Type and number of arguments in member function must be same as types and number
of data declared in class definition.
• The symbol :: is known as scope resolution operator. Usage of :: along with class name is
the header of function definition. The scope resolution operator identifies the function as
a member of particular class.
▪ Several classes can use same function name. Membership label will resolve their scope.
• Member function can access private data of a class. A non-member function cannot.

Example : The following program segment shows how a member function is defined
outside class.
Class operation
{
private:
int a; int b; public:
int sum();
int product();
};
int operation::sum()
{
return(a+b);
}
int operation(product)
{
return(a*b);
}

Defining objects of a class


When a class is defined, it specifies the type information the objects of the class store. Once the
class is defined an object is created from that class.
The objects of a class are declared in the same manner like any other variable declaration.

Syntx: class user_defined_name


{

private: //members
public: //methods
};
User_defined_name objec1, oject2,….;
Example: class num
{
private:
private:
int x; int y;
public:
int sum(int p, int q); int diff(int p, int q);
};
void main()
{
num s1,s2; s1.sum(200,300); s2.diff(600,500);
}
Note that an object is an instance of a class template.

Arrays as members of classes


Example : class marks
{
int m[5]; int I;
}
public:
void setval(void);
voi
the array variable m is a private member of class marks.

Array of objects
An array having class type elements is known as array of objects. An array of objects is declared
after class definition and is defined in the same way as any other array.

Example : class employee


{
{
char name[10] ; int age; public:
void getdata();
void dispdata(void);
};
employee supervisor[3] ;
employee sales_executive[5] ; employee team_leader[10] ;
In the above example, the array survisor contains 3 objects namely supervisor[0], supervisor[1],
supervisor[2].

Objects as function arguments


A function can receive an object as a function argument. This is similar to any other data type
being sent as function argument. An object can be passed to a function in two ways:
□Copy of entire object is passed to function (pass-by-value).
□Only address of the object is transferred to the function (pass-by-reference).

In pass by value, a copy of the object is passed to the function. The function creates its own
copy of the object and uses it. Therefore changes made to the object inside the function do not
affect the original object.

In pass by reference, when an address of an object is passed to the function, the


function directly works on the original object used in function call. This means changes
made to the object inside the function will reflect in the original object, because the
function is making changes in the original object itself.
Pass-by-reference is more efficient, since it requires only to pass the address of the object and
not the entire object.

Differences between structure and class:


In C++, a structure is a class defined with the keyword struct. Its members and base classes are
public by default.
A class is defined with the class keyword.
Its members and base classes are private or/and public.

∗∗∗∗∗

You might also like