class and object


The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class. The variables inside class definition are called as data members and the functions are called member functions. For example : Class of birds, all birds can fly and they all have wings and beaks. So here flying is a behavior and wings and beaks are part of their characteristics. And there are many different birds in this class with different names but they all posses this behavior and characteristics. Similarly, class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.

More about class


1. Class name must start with an uppercase letter(Although this is not mandatory). If class name is made of more than one word, then first letter of each word must be in uppercase. Example, 2. class Study, class StudyTonight etc 3. Classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers (discussed in next section). 4. Class's member functions can be defined inside the class definition or outside the class definition. 5. Class in C++ are similar to structures in C, the only difference being, class defaults to private access control, where as structure defaults to public. 6. All the features of OOPS, revolve around classes in C++. Inheritance, Encapsulation, Abstraction etc. 7. Objects of class holds separate copies of data members. We can create as many objects of a class as we need. 8. Classes do posses more characteristics, like we can create abstract classes, immutable classes, all this we will study later.

Example


Class is mere a blueprint or a template. No storage is assigned when we define a class. Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
                #include 
                        #include
                        using namespace std;
                        
                        // Class Declaration
                        
                        class person {
                            //Access - Specifier
                        public:
                        
                            //Variable Declaration
                            string name;
                            int number;
                        };
                        
                        //Main Function
                        
                        int main() {
                            // Object Creation For Class
                            person obj;
                        
                            //Get Input Values For Object Varibales
                            cout << "Enter the Name :";
                            cin >> obj.name;
                        
                            cout << "Enter the Number :";
                            cin >> obj.number;
                        
                            //Show the Output
                            cout << obj.name << ": " << obj.number << endl;
                        
                            getch();
                            return 0;
                        }
                        
               
            
        
Sample Output
Enter the Name :Byron
Enter the Number :100
Byron: 100

class vs structure


In C++, a structure is same as class except the following differences: 1) Members of a class are private by default and members of struct are public by default. For example program 1 fails in compilation and program 2 works fine.
    // Program 1
    #include 
     
    class Test {
        int x; // x is private
    };
    int main()
    {
      Test t;
      t.x = 20; // compiler error because x is private
      getchar();
      return 0;
    }
    Run on IDE
    // Program 2
    #include 
     
    struct Test {
        int x; // x is public
    };
    int main()
    {
      Test t;
      t.x = 20; // works fine because x is public
      getchar();
      return 0;
    }
    
  
2) When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private. For example program 3 fails in compilation and program 4 works fine.
      // Program 3
      #include 
       
      class Base {
      public:
          int x;
      };
       
      class Derived : Base { }; // is equilalent to class Derived : private Base {}
       
      int main()
      {
        Derived d;
        d.x = 20; // compiler error becuase inheritance is private
        getchar();
        return 0;
      }
      Run on IDE
      // Program 4
      #include 
       
      class Base {
      public:
          int x;
      };
       
      struct Derived : Base { }; // is equilalent to struct Derived : public Base {}
       
      int main()
      {
        Derived d;
        d.x = 20; // works fine becuase inheritance is public
        getchar();
        return 0;
      }