Class In C++ Explain With Program and Example

Explain Class in C++ with Examples and Program



Class:


 Class can be defined as combination of data members and member functions applied on that data. It allows the data to be hidden from the external use. A class can also be called as an abstract data type (ADT).
Abstract Data Type can be defined as the technique of creating new data types whose implementation details are hidden and can only be handled using the publicly accessible member function

It’s a User Defined Data-type.
1.The Data declared in a Class are called Data-Members of the Class.
2.The Functions declared or defined in a Class are called Member-Functions of the Class.
The members of a Class can only be accessed through Objects of the class.
Syntax: class class_name
{
 access specifier:
    member1;
  access specifier:
    member function;
  ...
             } [object_name];       for example
    class a
    {    private;
        int x
        public :
        void show() {cout << a;}
};

Where class_name is the name for a class (user defined type) and the optional field object_name is one, or several, valid object identifiers. The body of the declaration can contain members, which can be either data members or member functions.

The following are the characteristics of a class
•    The keyword class specifies abstract data type of type class name.
•    The body of a class is enclosed with in braces and terminated by a semicolon
•    The functions and variables with in the class are collectively called as members
•    The members that have been declared as private can be accessed only from with in the class.
•    Class definition is only a template and does not create any memory space for a class
•    By default all the members are of type private .

Labels: