SPECIFING A CLASS in C++
A class is a way to bind the data and its associated functions together.. Class is a user defined data type, which holds its own data members and member functions.
It allows the data members(and functions) to be hidden, if necessary, from external uses. When we defining a class, we are creating a new abstract data type that can be treat like any other user define data type or built in data type. Basically a class specification has two parts.
1. class declaration
2. class function definitions
In the first part The class declaration describes the types and scope of its members. The class function definitions describe how
the class functions are implemented.
The general form of a class declaration
class_name
{
:
variable declarations;
function declarations;
:
variable declarations;
function declaration;
};
|
The class declaration is similar to a (structure) or struct declaration. The keyword class specifies, that what follows is an abstarct data of type class_name. The body of the class is enclosed within braces and
terminated by a semicolon. The class body contains the declaration of functions and variables. These functions and variables are called class members,
they are usually grouped under two sections, namely, private and public to denote which of the members are public and which of them are private . The keywords public and private are known as visibility labels.
Note that these keywords are followed by colon.
The class members that have been declared with private keyword can be accessed only from within the class. on the other hand, with public keyword members can be accessed from outside the class also
the data hiding(using private declaration) is the key feature of OOPS.
The use of the private keyword is optional by default the members of a class are private.
if both the labels are missing, then by default all the members and functions are private. such a class is completely hidden from the outside scope
student
The variable declared inside the class are known as data members and the functions are known as member functions.
only the member functions can have access to the private data members and private functions. However public members(both functions and data members) can be accessed from outside the class.
Example of class ,class declaration would look like
student
{
fees;
roll;
:
getdata( f, r);
displayData();
};
|
we give class with meaningful name, such that student. This name now becomes a new type identifier that will be used to declare instances or create object of that class type. The class item contains two function members and two data members.
The data members are private by default while both the member functions are public by declaration. so The function getdata() can be used to assign values to the class members from outside the class.
This means that the data cannot be accessed by any function that is not a member of the class item.
Note that the functions are declared, not defined. Actual function definitions definitions will appear later in the program. The data members are usually declared as private and the member functions as public
Represent the class in the oops