Introduction Of Classes ( OOPS )


Class Definition and Overview

A class is a blueprint, or prototype which defines and describes the member attributes and member functions.

A class is a three-part containing the class name, data attributes (variables) and the member functions.

  • Class Name
  • Member attributes (variables)
  • Member functions

The Class name is an identifier, which is the user-defined data type name.The member attributes hold data just like variables which can be accessed by the member attributes only via particular class/objects.The member functions may have productive behaviors or may be designed to do some operations with member attributes just like normal functions, but the member functions can be accessed only via the corresponding class or its objects.

Object or Instance

An object is an instance of a class.

A class is only a prototype. Which doesn't have any memory for storing value(which is exceptional for static members). Objects are the instances of a class. Simply speaking, It may be defined as a variable of user-defined datatype classes. Through an object or instance, all classes member variables and member functions.

  • The member variables hold the data and the member functions do operations based on these objects of the class.
  • Most of the OOPS concepts are implemented based on classes. Some of them are Inheritance, Polymorphism, Abstraction, Encapsulation.

Class Advantages and Summary

  • A class is a user-defined and reusable entity.
  • A class reduces complicity of holding data. 
  • A class just holds data and do operations with help of member variables and member functions.
  • A class maintains software quality and is well structured.
  • An Object is a realization of the particular class.

General syntax of the Classes

class Classname {
  access_specifier:
  member_varibales;
  member_functions;
}object1, object2;

where 'Classname' is an identifier of the class, 'object1' and 'object2' are objects of this class (which is an optional declaration, we may be declared in the main function).

A class may contain member variables, member functions, and access specifiers. But all these are optional. ie., a class can be created with or without all three of these. For a class or a variable or a function without an access specifier, the compiler itself provides private access by default.

Access Specifiers of the Class

Access specifiers define how a member's variables and member's functions of a class can be accessed from outside the class. However, all members of a class can be accessed from within the class without any restriction. There are three access specifiers. They are

  • Private 
  • Public
  • Protected

Private members: These can be accessed only from within the members of the same class.

Protected members: These can be accessed only from within other members of the same class and its derived classes.

Public members: These can be accessed from anywhere where the object is accessible.

Simple Class Example Program In C++

/*  Simple Class Example Program In C++
    Understanding Class                             */

// Header Files
#include <iostream>
#include<conio.h>

using namespace std;

// Class Declaration

class Person {
   //Access - Specifier
public:
   //Member Variable Declaration
   string name;
   int number;

   //Member Functions read() and print() Declaration

   void read() {
      //Get Input Values For Object Variables
      cout << "Enter the Name :";
      cin >> name;

      cout << "Enter the Number :";
      cin >> number;
   }

   void print() {
      //Show the Output
      cout << "Name : " << name << ",Number : " << number << endl;
   }
};

int main() {
   // Object Creation For Class
   Person obj;

   cout << "Simple Class and Object Example Program In C++\n";

   obj.read();
   obj.print();

   getch();
   return 0;
}

Sample Output

Simple Class and Object Example Program In C++
Enter the Name :Archket
Enter the Number :1000
Name : Archket,Number : 1000

More C++ Class Example Programs

Constructors and Destructors

  • A constructor is a special member function of the class which has the same name as that of the class. It is automatically invoked when we declare/create new objects of the class.
  • The destructor is a special member function which is called automatically when the object goes out of scope.

Read More

Classes with struct and union

Classes can also be defined using struct and union keywords.

Structure in C++

  • It has member functions.
  • It uses inheritance
  • It has the ability to declare public and private members

In C++,  a struct keyword works same as the class keyword and structs can also have member variables and functions. Thus a struct is a class whose members are public by default.

Union in C++

A union class has the ability to store only one data member at a time.The default access for a union class is public.

  • Able to store only one variable
  • Has member functions.
  • Uses inheritance
  • Declares public and private members