Class:

“A Class is a user defined data type that combines both data and method into a single unit known as class”.

“In other words, a class is a container which includes both data members and member functions and it is used to generate object from it.”

  • We can generate multiple objects from a class.
  • For example ‘Fruit’ is class while ‘Apple’, ‘Mango’, and ‘Banana’ are the objects created from the class ‘Fruit’. We can create object from class as like as built-in data type
Fruit    Apple;

Class Declaration Syntax:

class className
{
   // By default data members are private
   [private|public]:
       datatype variableName1;
       datatype variableName2;
       ....
       ....
            // Member function declaration 
   public:
       returnType functionName1(argument1, argument2, ...) ;
       returnType functionName2(argument1, argument2, ...) ;
};
  • class keyword is used to declare a class
  • className specifies name of the class. It can be any valid name and must follow naming convention rules.
  • Class contains both data members as well as member functions.
  • Variable and function has two scopes: one is private and other is public. By default the scope is private while to use public scope we have to write keyword ‘public’ before the declaration or definition.
  • “private” variable and function cannot be access outside the class so it provides the facility of the data hiding.
  • “public” variable and function can be used anywhere.
  • The variable used inside the class is known as the data member or class variable or instance variable while function inside the class is known as the class function or member function.

Class Structure

  • The definition of functions can be written inside the class or outside class.
  • When we write function definition inside the class by default that function becomes inline function.
  • When we write function definition outside the class we have to use scope resolution operator (::).
  • the following is an example of Book class that contains data members to store book detail as well as member functions to perform operations such as issue, return, search and display:
Example:
class Book
{
   // private Data Members
   private:
       int bookId ;
       char bookTitle[25];
       char author[20];
       float price;
                  // public Member function declaration 
   public:
       void issue(int) ;
       void return(int) ;
       int search(char);
       void display();
};

Object:

“An object is an instance of a Class”.

  • Object contains both data and methods.
  • Memory is allocated only when we create an object of the class and not at the time of class declaration.
  • Objects are created from the class just like simple variable.
  • To create an object of the class we use the following syntax:
Syntax to Create an Object:
className <obj1>[, <obj2>,obj3...] ;
  • Where, className is the name of the class and obj1 is the object we need to create. we can create as many objects from a single class as we want.
  • we can create an object from our Book class as follow:
Example:
Book bookObj1;
  • The public variable or function can be accessed directly by object using simple dot (.) operator. use the following syntax.
Syntax to Access Data Members and Member Functions using Object:
objectName . datamember ;  // to access public data members of a class
objectName . functionName() ; // to access member functions of a class
objectName . functionName (parameterList) ; 

Example:

bookObj1 . bookTitle ;   // assume bookTitle is public member
bookObj1 . display() ;

Characteristics of member function:

  • Member function of class has the following characteristics:
  1. Different classes can use the same function name.
  2. Member function can access public variable and function as well as private variable and function of class without using dot operator.