in

CPP – Classes & Objects

As from previous lessons we learned CPP has a concept called OOP and we will talk in detail about OOP later. In this lesson you’ll be learning how to create classes and instances of classes.

Classes : A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

class MyClass{
  int a;
  int b;
public:
  void function()
  {
    //some code
  }
};

Objects : Objects are the instances of Classes and objects has all properties of a class.

MyClass MyObject;
MyClass obj;

So here, MyClass is the name of the class with some data members a & b also with member function().

But What is that public in MyClass definition?

Okay, it demonstrate an important concept of Data Hiding. When working on a big projects like creating a program for ATM using C++, it’s is important to consider the security consequences or we can take example of the software running on server side. In these cases, we don’t want anybody to access our data members directly. That’s why a new concept called Access Modifier or Access Label or Access Specifier. 

We have three Access Labels:

  1. Public
  2. Protected
  3. Private

And I want to create another post on these because there is lot to talk about these 3 buddies. But still, let me give little info about these keywords.

Public:

Data Members and Member Functions are accessible outside of the class.

Protected:

Data Members and Member Functions are accessible in the derived class (Derived class is a class which inherits propertied from another class usually called a base class).

Private:

Data Members and Member Functions are only accessible inside class.

Example 1:

#include <iostream>
using namespace std;

class Rectangle{ //Class Definiton
public://Access Label
  int length; // Data Member declared with public accessibility
  int breadth;
};

int main()
{
    Rectangle obj;
    obj.length = 10; //Accessing and assigning value to the Data Member using object
    obj.breadth = 20;
    cout << "Area of the rectangle is : " << obj.length * obj.breadth << endl;
    return 0;
}

See, the code written above clearly; isn’t it like a structure if we replace class keyword with struct and remove public access label. Yeah it is a structure!

Now you got the concept, classes are nothing but advancement to structures.

Example 2:

#include <iostream>
using namespace std;

class Rectangle{ //Class Definiton
private://Access Label
  int length; // Data Member declared with public accessibility
  int breadth;
public:
  void GetData(int l, int b){ //Receiving the data from the user
    length = l;
    breadth = b;
  }
  int GetArea(){ //Calculating the area from length and breadth
    return length * breadth; 
  }
};

int main()
{
    Rectangle obj;
    obj.GetData(10, 20);
    cout << "Area of the rectangle is :" << obj.GetArea() << endl;
    return 0;
}

Now i’ve changed access label from public to private that means now we can’t access the data members directly outside of the class as we did in previous example. So, solution to this is to provide functionality to class itself. By doing this we are restricting direct access to our data members and now nobody can manipulate the data members.

I know it’s confusing but we should consider the security consequences as well. 🙂

Written by Anandesh

I am a #Linux lover and very much excited about technology and new thing. Love to #read_books. Curiosity is my best teacher. I am still☺️ understanding myself.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CPP – Structures

CPP – Access Labels