Classes
A class is a
summary description of a set of objects. A class thus contains the descriptions
of all the behaviours of the objects that it represents. A class is a collection of data and the
various operations that can be performed on that data.
Generally, a class specification has two parts:
1.
Class declaration
2.
Class function definition
The class declaration describes the type and scope of its members. The
class function definitions describe how the class functions are implemented.
The general form of a class declaration is:
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
Private section
A private member variable or function cannot be
accessed, or even viewed from outside the class. Only the class and friend
functions can access private members.
By
default all the members of a class would be private, for example, in the
following class width is a private member, which means until we label a member,
it will be assumed a private member.
class Box
{
double width;
public:
double length;
void setWidth( double wid );
double getWidth( void );
};
Public
section
Public members can be accessed from outside the class. The
variables inside the class are known as data members and functions are known as
member functions. Only the member functions can have access to the private data
members and private functions. However, the public members (both function and
data) can be accessed from outside the class.
All Objects of same class can access public members with or without
using member functions from outside the class.
Data hiding facility in a class
Information hiding is the primary criteria of system and
should be concerned with hiding the critical element in system. Information
hiding separates the end users from illegal access of the requirement. Encapsulation supports
information hiding by making use of the three access specifies of a class.
Accessing
class members:
We can access class members like the following way-
Syntax:
object_name.function_name(argument_list);
example:
ob.getdata();
ob.display();
No comments:
Post a Comment