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


a class is a collection of related data or function.


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.

            <class-keyword> <class-name>;

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

defining a class:

           <class-keyword> <class-name>

                     data member or data functions.

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

                  public: { data or function declarations}


           class student

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

              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.

            <class-name> <object-name>;

              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.

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.


"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."
Previous Post
Next Post

Yash is a Full Stack web developer. he always will to help others. and this approach takes him to write this page.