Saturday, February 2, 2019

class and object in c++ programming

in this post, you will learn about the class and object in c++ programming and learn how to declare and define a class and its object. 


class and object in c++ programming, learn c++ programming
learn c++ programming

class:

a class is a collection of related data or function.

object:

an object is a run time entity. it's a variable of class type and a real-world entity.

Note: we have different types of objects related to a class. and a class describes the properties of objects. when we create the object of the class then the memory is assigned to the member function and data member of class related to the object. 

defining a class and creating objects of the class:

declaring a class:

to declaring a class we used class keyword.

syntext:
            <class-keyword> <class-name>;

Example:
              class A;  // create a class name A
              class B;  // create a class name B


defining a class:

syntext:
           <class-keyword> <class-name>
                   {

                     data member or data functions.
                   };

Note: you can always use the [;] semicolon at the end of the class.
                  
Example:
              class class-name
              {
                        
                  private:{ data or function declarations}

                  public: { data or function declarations}

             };

           class student
           {
            private: 

              int roll;        // data member of class
              char name[10];  // data member of class
      
            public:

              void getdata(void);  // member function of the class
              void putdata(void);  // member function of the class

           };


Note: we can access or use the public data of a class from outside the class but we cannot access and use the private data outside the class.

Creating an object of the class:

An object of the class is always creating in the main function of the program.

syntext:
            <class-name> <object-name>;

Example: 
              student s1, s2, s3;

Note: always use the same name of the class while creating an object of the class. By default, all the data member and data function of the class is always private. 

calling a member function of a class using object of the class:

to call a member function of a class using object of the class we use [.] dot product.
           
syntax:
           <object-name>.<calling-function>;

Example: 
in the above example, we declare a function getdata(), so to call the getdata function using the object of the class we call like this.

              obj.getdata();

"Please share this knowledge as much possible as you can and also comment me your queries and questions related to this topic. I will really grateful to help you."

0 comments: