Class

CLASS

A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.

Classes are generally declared using the keyword class, with the following format:

class class_name {

  access_specifier_1:

    member1;

  access_specifier_2:

    member2;

  ...

  } object_names;

Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.

An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire:

•           private members of a class are accessible only from within other members of the same class or from their friends.

•           protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.

•           Finally, public members are accessible from anywhere where the object is visible.

 

By default, all members of a class declared with the class keyword have private access for all its members.

Example :

            class A{

                        int x;

                        int y;                              //by default x and y are private

                        public:

                                    void set(int i,int j){

                                                x=i;

                                                y=j;

                                    }

                                    void display(){

                                                cout<<"Value of x = " <<x<<"\nValue of y = "<<y;

                                    }

            };

            void main(){

                        A obj;

                        obj.set(5,6);

                        obj.display();

                        getch();

            }

Programming
Design & Developed By :