Classes & Object

Classes- A class is a specification, a blueprint, for the object. A class can be thought as a sketch (prototype) of a house which contains all the details about floors, doors, etc. Based on these descriptions, the house is built (where house is the object). 

How to create a Class?

A class is defined in C++ using keyword 'class' followed by the name of the class.

The body of the class is defined inside the curly brackets, terminated by a semicolon at the end.

Syntax:

 class className

 {

    //some data

    //some functions

};

For example :

class Box

{

     public:

              double length;

              double breadth;

              double height;

              double calculateArea()

             {

               return length*breadth;

             }

              double calculateVolume()

            {

              return length*breadth*height;

            }

};

Here, a class named Box is defined.

The variables length, breadth, and height, declared inside the class, are known as data members. And, the functions, calculateArea() and calculateVolume(), are known as member functions of a class.

Objects :

When a class is defined, only the specification for the object is defined; no memory or storage is allocated.An Object is an instance of a class.

To use the data and access functions defined in the class, we need to create objects.

Syntax to Define Object in C++

className objectVariableName;

We can create objects of Box class (defined in the above example) as follows:

className objectVariableName;

We can create objects of  Box class (defined in the above example) as follows:

     // sample function

void sampleFunction() {

    // create objects

    Box b1, b2;

}

int main(){

    // create objects 

    Box b3, b4;

}

Here, two objects b1 and b2 of the Box class are created in sampleFunction(). Similarly, the objects b3 and  b4 are created in main().

As we can see, we can create objects of a class in any function of the program. We can also create objects of a class within the class itself, or in other classes.

Also, we can create as many objects as we want from a single class.

Object and Class in C++ Programming

// Program to illustrate the working of

// objects and class in C++ Programming

#include <iostream.h>  

using namespace std;  

// Creating a Class

class Student 

{  

    public:  

      int id;//data member      

      string name;//data Member

};  

int main() 

{  

//creating an object of Student   

    Student s1; 

// assigning values to data members

    s1.id = 116;    

    s1.name = "smash";   

    cout<<s1.id<<endl;  

    cout<<s1.name<<endl;  

    return 0;  

}  

Output:

116

smash

Comparison Chart between class and object 

 COMPARISON
BASIS
  class OBJECT
  DefinitionA template or blueprint with which objects are created is known as Class.An instance of a class is known as Object.
 Type of entityLogicalPhysical
 CreationClass is declared by using class keyword.Object is invoked by new keyword.
 Memory   allocationThe formation of a class doesn't allocate memory.Creation of object consumes memory.

Introduction to Access Specifiers in C++

Access specifiers in C++ are basically used in OOPs concepts. In classes, we start their use, they are mainly used in inheritance. They set the range for the usage of the variable and the functions of a particular class. Access specifiers are used for data hiding purposes also.

There are 3 types of access specifiers available in C++:

1. Public

2. Private

3. Protected

Note: If we do not specify any access specifiers for the members inside the class then by default the access modifier for the members will be private. 

Syntax of Declaring Access Specifiers in C++

  class

{

  private:

     // private members and function    

  public:

      // public members and function 

  protected:

      // protected members and function

};

 Let us now look at each one these access specifierd in details:

Public Access Specifier in C++

Public class members are accessible out side the class and it is available for every one.

Syntax:

 class Public_Access_Specifier

{

 public:                //public access specifier

 int a;                   // Data Member Declaration 

 void display();   // Member Function declaration

}

Private Access Specifier in C++

Private class members are accessible with the class and it is not accessible out side the class. If some one try to access out side the it gives compile time error. By default class variables and member functions are private.

Syntax:

 class Private_Access_Specifier

{

 private:                // private access specifier

 int a;                    // Data Member Declaration 

 void display();    // Member Function declaration

}

Protected Access Specifier in C++

It is similar to private access specifier. It makes class member inaccessible outside the class. But they can be accessed by any subclass of that class.

Syntax:

 class Protected_Access_Specifier

{

 protected:            // protected access specifier

 int a;                    // Data Member Declaration 

 void display();    // Member Function Declaration

}

In below example I will show you all these access specifier public, private and protected.

Access Specifier Example in C++

#include<iostream.h>

#include<conio.h>

//using namespace std;

class Declaration

{

private:

int a;

public:

int b;

protected:

int c;

public:


void show()

{

a=10;

b=20;

c=30;

//Every members can be access here, same class

cout<<"\nAccessing variable within the class"<<endl;


cout<<"Value of a: "<<a<<endl;

cout<<"Value of b: "<<b<<endl;

cout<<"Value of c: "<<c<<endl;

}

};

class Sub_class:public Declaration

{

public:

void show()

{

b=5;

c=6;

cout<<"\nAccessing variable in sub the class"<<endl;


// a is not accessible here it is private

//cout<<"Value of a: "<<a<<endl;

//b is public so it is accessible any where

cout<<"Value of b: "<<b<<endl;

//'c' is declared as protected, so it is accessible in sub class

cout<<"Value of c: "<<c<<endl;

}

};

void main()

{

clrscr();

Declaration d; // create object

d.show();


Sub_class s; // create object

s.show();    // Sub class show() function


cout<<"\nAccessing variable outside the class"<<endl;

//'a' cannot be accessed as it is private

//cout<<"value of a: "<<d.a<<endl;


//'b' is public as can be accessed from any where

cout<<"value of b: "<<d.b<<endl;


//'c' is protected and cannot be accesed here

//cout<<"value of c: "<<d.c<<endl;

getch();

}

Output:

Accessing variable within the class

value of a: 10

value of b: 20

value of c: 30


Note that In C++ access specification works on per-class basis, not on per-object basis.

Several programming languages don’t have private and protected access, therefore, any user can use it in the ways they want. C++ coders won’t trust users so they are not allowed to use it. As public data members can be a serious potential risk for bugs or hackers.