Classes and Objects....cont(3)

Scope Rules of Classes at a Glance
 ELEMENT 
TYPE 
 SCOPE
 DESCRIPTION
 CLASS
 GLOBAL 
 GLOBAL
 Such class type is avaialble globally to all the functions within a program ie, objects of this class can be created and accessed from anywhere in the program.
 
 LOCAL
 LOCAL
 Such class type is available locally to the functin in which the clss definition occurs. The objects of this class type can be created only within the function that defines the class.
 OBJECT
 GLOBAL
 LOCAL 
 GLOBAL
 LOCAL 
 Such object can be used anywhere in the program by any functions.
 Such object can be used only within the function that declares it.
  CLASS MEMBERS
 PRIVATE 
 CLASS SCOPE 
 Such members can be used accessed only by member functions of the class. These cannot be accessed diretly using the objects of the class.
 
 PROTECTED
 CLASS SCOPE
 Same as for private members 
 
 PUBLIc
 
GLOBAL
LOCAL
 Scope of public members depends on referencing objects.
 If the referencing object is global, the scope of a public member is global.
 If the referencing member is local, the scope of a public member is local.
Objects
The declaration of a class does not include objects of that class. It only specifies the type of information the objects of this class type will hold, when created. Objects of a class can be created using the class tag-name as type specifier. The syntax used to declare objects is:
      class-name object1, object2, .....objectn;
For example:
        class book;
        {
            public;
            int bookno;
            float cost;
        void getdata(int i, float j)
        {            
             bookno=1;
            cost=j;
        }
        void putdata(void)
        {
            cout <<"\nBook no:"<<bookno;
            cout<<"\nCost:"<<cost;
        };
        book r1, r2;    // objects of type book
        int main()
        {
            r1.getdata(101, 123.50);    // Assign values to data members of object r1
            r2.getdata(102, 145.50);    // Assign values to data members of object r2
            r1.putdata();                    // display values of r1
            r2.putdata();                    // display values of r2
        }
The public members of an object can be accessed directly by using the objects name and dot(.) operator.
 
    HOME            LEARN C++              PREVIOUS                  NEXT