c++

Objects and Classes

In this tutorial we are going to learn about classes and object in C++ with the help of some examples.

In this course, till now we have created functions to do a specific task or related task. we also need to create different variables separately to access the function

However, in C++ we don’t need to create functions and variables (or data) separately, we can wrap them into single a entities called object, which contains members to perform tasks and variables to access them. This unique feature pioneered a new kind of computer programming called object oriented programming.

This provided more security and neat packaging to code. Also they provide real-life touch to programming. we will learn more about it later in this chapter.


Classes

a class is a blueprint of objects

let’s see how

consider an example of bicycles, each and every bicycle in this world have same characteristics like two wheels , paddle , handle etc. And also have same functions like moving, running etc. Thus a bicycle is an object of class of bicycles, which defines its characteristics and functions.

Hence class of bicycle act as blueprint for bicycles.

photo1_co

Creating a class in C++

Lets see how to create classes in c++

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 and terminated by a semicolon at the end.

class className {
 // some data
 // some functions
};

Lets understand this with example of class of students. Students can have data like name, roll_no., grade etc. And can do functions like taking exams, submit fees, ask doubts etc.

class student{
    //data

    char name[20];
    int roll_no.;
    char grade

    //functions
    Take_exam();
    Submit_fees;
    Ask_doubts();
};

From above example, it may be seen that class contains data in form of variables( eg. Name, roll_no. and grade),known as data members

And functions(eg. Take_exam(), Submit_fees() and Ask_doubts()), known as member functions

Let’s take one more better understanding .

class Room {
    public:
         double length;
         double breadth;
         double height;

         double calculateArea(){
            return length * breadth;
        }

         double calculateVolume(){
            return length * breadth * height;
         }

};

Here, in the above example, length, breadth and height are data members. And calculateArea() and calculateVolume() are member functions .


Objects

Again consider the example of class of students, each and every student of this world would belong to this class, as they have same characteristics and functions.

Thus we can say that objects are instances of their class.


Creating objects in C++

When we define a class, only the blue print of object is defined, hence no memory allocation takes place. The memory allocation takes place when we create object of that particular class.

Syntax of creating object is given below.

<class_name><object_variable-name>;

We can create object of class students as follows:

class students{
    public:
    //data
    char name[20];
    int roll on.;
    char grade
    //functions
    Take_exam();
    Submit_fees;
    Ask_doubts();
};
int main(){
    students s1,s2,s3;
    ... .. ...
    ... .. ...
    ... .. ...
     return 0;
}

In above example you can see the piece of code students s1,s2,s3;

It is nothing but creation of three objects s1,s2 and s3 of class students. you can create as many objects as you wish.


Accessing member functions of a class

you can access member functions of a class after creating its object.
The syntax of code to access class is given below.

<object_variable_name>.<member_function>;

For example, to access member function Take_exam()of class students, we can write

s1.Take_exam();

you may have noticed the dot(.) between s1 and Take_exam(). This dot is called access operator. We use this operator along with object to access member functions

Similary, we can access data members

s1.roll_no.=55;

Access Specifiers in C++

Access specifiers are used to implement an important feature of Object-Oriented Programming known as Data hiding.

Again consider the example of students, from the previous topic it is clear that any function can access and modify the data members and member functions of a class, which is a serious drawback regarding data security. Hence, C++ provides various access specifiers to ensure data security. These are

  1. Private access specifier
  2. Public access specifier
  3. Protected access specifier

for now we will only discuss first two

  1. Private access specifier : The class members (data members and functions) declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class.

    Suppose, we want to hide the member functions of class students. We will do the following
    class students{
        private:
        //data
        char name[20];
        int roll on.;
        char grade
        //functions
        Take_exam();
        Submit_fees;
        Ask_doubts();
    };
    This will hide all class members from outside functions, so they can’t access them easily.

  2. Public access specifier: All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.
    class students{
        public:
        //data
        char name[20];
        int roll on.;
        char grade
        //functions
        Take_exam();
        Submit_fees;
        Ask_doubts();
    };
    Now each class member can be easily accessed by any function.

Note: by default each and every member of a class is private. So private is the default access specifier of a class.