Quick Introduction to class in C++

Aastha Gupta - Jul 7 '21 - - Dev Community

A class is a data type that you define and you can also add some functions that work on this data type. That's it, that is exactly what a class is! Thank you for reading this article and I'll see you in the next one.

wait . . .

Above explanation is simple and covers what a class is in as little words as possible but there are more caveats related to it and I'll go over them in this article.

Declaring a class

class Cat {
   private:

      // data memebers
      string breed;
      int legs;

   public:
      // constructor
      Cat () {  
        breed = "Sphynx";
        legs = 4;
        meow = true;
      }

      // member function
      void setLegs(int num) {
         legs = num;
      }

      // member function
      void setBreed(string breed_name) {
         breed = breed_name;
      }
};
Enter fullscreen mode Exit fullscreen mode

Data members

What you create of classes are objects. These objects have certain properties that can be accessed and these can be defined in the class as data members. For a Table it can be wood type, number of drawers, length, height etc. By creating an object of a table, you can assign values to these properties in accordance with their data types.

A class is a plan, object it's execution

Member functions

You might want your objects to perform some tasks and for that you write member functions. These functions are like regular functions accept they are only accessible to the object of the class; which means the functions can or can not take parameters, it can or cannot or return something back. Member functions are a powerful and you must use them with caution.

Inheritance in C++ can be achieved by deriving a class from another class of which access specifiers are an important part. I'll pick these topics up in my future articles. Stay tuned! If you want to read more about classes, head over here

Thanks for giving this article a read and I'll see you in the next one 😄

PS: This is an article in my series Quick Introduction to a concept in C++. You can find all the articles in this series here. I also answer why I don't use the series feature by dev.to there.

. . . . . . .