CLASSES in C++ | Declaration, Creating Objects


Class: A class is a user defined data type which binds data and its associated functions together. It allows the data and functions to be hidden, if necessary from external use. Generally, a class specification has two parts. 

i) Class declaration: it describes the type & scope of its members. 

ii) Class function definitions: It describes how the class functions are implemented.

Class declaration. 

The general form of a class is 

class class-name 

        private: variable declaration; 

                     function declaration; 

        public: variable declaration; 

                    function declaration; 

1) The class keyword specifies that what follows is an abstract data of type class name. The body of a class is enclosed within braces & terminated by semicolon. 

2) The class body consists of declaration of variables & functions which are called as members & they are grouped under two sections i.e. private & public. 

3) Private and public are known as visibility labels, where private can be accessed only from within the class where public members can be accessed from outside the class also. By default, members of a class are private. 

4) The variable declared inside the class are known as data members & functions are known as member functions. Only the member function can have access to the private data members & private functions. However the public members can be accessed from outside the class.

Simple class example 

class item 


        int number; //variable declaration
        float cost; //variable declaration 
public : 
        void getdata (int a, float b); // function 
        void putdata (void); //declaration 


In above class class-name is item. These class data members are private by default while both the functions are public by declaration. The function getdata() can be used to assign values to the member variable number & cost, and putdata() for displaying their values. These functions provide the only access to data members of the class.

Creating objects

Object is an instance or variable of the class. Once a class has been declared. We can create variables of that type by using the class name. Ex ñ item x; Here class variables are known as object therefore, x is called an object of type item and necessary memory space is allocated to an object. 

Accessing class Members 

1) Private data of a class can be accessed by the member function. The following is the format for calling member function. 

        Object-name.function-name(actual argument) 

            Ex - x.getdata (10, 20.3); 

This statement assign value 10 to number & 20.3 to cost of the object x. By implementing the getdata() function similarly, the statement. 


would display the value of data member. 

2) If the member variable is declared as private then it cannot be accessible by object. It can only be accessed by a member function. Whereas, if a variable is declared as public it can be accessed by the object directly.

Ex- class abc { 

        int x, y; 

    public : 

        int z; 


main() { 

        abc m ; 

        m.x = 100; // error x is private 

        m.z=50; // ok ,z is public


The above example shows concept of data hiding. 


"classes in C++ example program"

"class syntax in C++"

"C++ class constructor"

"classes and objects in C++"

"classes and objects in C++"

"object in C++"

"object in C++ example"

"syntax of object in C++"