11 February, 2013

C++ Program For Defining A Class [Class Definition] Including It's Members.


This Post Contains A C++ Program For Defining A Class [Class Definition] Including It's Members With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Classes' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



Classes is said to be user-defined data type; which is defined by the user. Users of object-oriented technology usually think of classes as containing the information necessary to create instances, i.e the structure & capabilities of an instances is determined by its corresponding class. Classes serve as 'Templates' for the creation of objects. Class provides entire set of data & related functions to be made user-defined. Thus programmer can build number of classes according to requirements.

'Class' Declaration [ i.e Syntax Of Class Definition ]:~

class class_name
{
       access-specifier :
                                   data members & member functions
        
       access-specifier :
                                   data members & member functions
                                   .....
                                   .....
                                   .....

       access-specifier :
                                   data members & member functions

}object-list;

-----------------------------------------------------------------------------------------------------------------------------                      

C++ Program For Defining A Class [Class Definition] Including It's Members.

# include < iostream.h >
class XYZ
{
       private :  int data;

       public :  void getdata()
                    {
                           cout  <<  "  Enter Number  :  ";
                           cin  >>  data;
                           cout  <<  "  The Number Is  :  "  <<  data;
                     }
};
void main()
{
                XYZ obj;
              
                obj.getdata();
}

Output  :

Enter Number  :  10

The Number Is  :  10

Explanation Of Program  :
In Here we have declared a 'Class' definition having class name XYZ. In this class we have declared one variable as data which is called as data member of the class, while function getdata() is accessing this data member, it is called as member function.
Class definition ends by giving semicolon after closing curly brace. Then we write main() function [which is non-member function], in which you initiate a class by creating a object. Thus a class is loaded in memory, in turn giving memory to its respected members [including data members & member functions]. This Process is known as Instantiation or Object Creation. Then you give call to member function via object.






No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.