Class



  
Class

A class is a user defined data that contained data as well as function together. It is a way to bind the data and function together in a single unit. When defining a class, we are creating a new abstract data type that can be treated like any other built-in data type,
Generally, a class specification has two parts: -
1.     Class declaration
2.     Class function definitions

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 in:
class class_name
{
private:
Data members;
Member functions;
protected:
Data members;
Member functions;
public:
Data members;
      Member functions:
};

The class declaration is similar to a struct declaration. The keyword class specifies, that what follows is an abstract data of type class_name. The body of a class is enclosed within braces and terminated by a semicolon, the class body contains the declaration of variables and functions. These functions and variables are collectively called class members. They are usually grouped under three sections, namely, private, public and protected to denote which of the members are private, which of them are public and which of them are protected. The keywords private, public and protected are known as visibility labels. Note that these keywords are followed by a colon.

The data members and member functions of class are grouped under three sections.
1.     Private
2.     Protected
3.     Public
These are also known as Access Modifiers.

Private:-
·        By private we mean that members can be accessed only from within the class i.e. member data can be accessed by the member function.
·        The private data members are not accessible to the outside world (i.e. outside the class)
·        By default members of a class are private.
·        Private members are not inheritable.

Protected:-
·        The members which are declared as protected, can only be accessed by member functions and friends function of that class.
·        Protected members are similar to private members, the only difference is that protected members are inheritable.

Public:-
·        The members which are declared as public can be accessed from outside classes also.
·        By default, the members of a class are private, if both the labels, are missing then by default, all the members are private. Such a class is completely hidden from outside world.

The variables declared, inside the class are known as data members and the functions are known as member function. Only the member functions can have access to the private data members and private functions. However, the public members (both functions and data) can be accessed from outside the class. The binding of data, and functions together into a single class type variable is referred to as encapsulation.








A Simple Class Example

A typical class declaration would look like:

class Item
{

int number;                                                    // variable declaration
float cost;                                                        // private by default
                                   
public:
void getdata(int a t float b);             // function declaration
void putdata(void)
};

We usually give a class some meaningful name, such as item. The class item contains two data members and two function members. The data members are private by default while both, the function are public by declaration. The function getdata( ) can be used to assign values to the member variables number and cost, and  putdata( ) for displaying their values. These functions provide the only access to the data 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. The data members are usually declared as private and the member functions as public.

Comments

Popular posts from this blog

ASCII / ISCII / UNICODE

CISC / RISC / EPIC

Evolution of Computers