All about C plus plus algorithms!

Saturday 29 October 2016

What is a Class || C++ (C plus plus)

A Class is like a Struct in many ways but the most distinctive attribute of it is that it allows you to keep your actual data encapsulated and in public many utility functions, constructors and destructors. 

There are two partitions in a Class i.e. Private and Public sections. Everything defined inside Private is not accessible to the outside world e.g. you cannot use dot or arrow operators to access it's elements/members. For example, I'll make a Student class here.

PS : We usually make classes in the Header files and include it in our .cpp files.

class Student
{
   private:
                 string name;
                 int roll_num;
                 float gpa;
                 string faculty;

 public:

     //constructors, destructors and utility functions here!
};

Now, you cannot write something like this in the main() or anywhere outside the class

Student s;
s.roll_num = 1234;     //this gives Syntax error

Well, then the question is how you'll initialize it? That's where constructors come in. 
Basically, there are 2 types of constructors i.e.
  1. Default constructor (without parameters and automatically called when you define the class type object.)
  2. Parameterized constructor (takes parameters/values that are to be assigned.)
A default constructor look something like this

Student()
{
    roll_num = 0;
    gpa = 0;

   //faculty and name are not initialized as String class takes care of it
}

As you can see that it has the same name as that of the class. Furthermore, it is created inside the Public section. When you initialize any object of type class anywhere, it automatically calls the default constructor and things are initialized to 0 or NULL. 

In case of Parameterized Constructor 

Student(string n, int rn, float g, string f)   
{
     name = n;          //n is the parameter which is the name
     roll_num = rn;   //r stands for roll number
     gpa = g;           // g stands for gpa
     faculty = f;     // f stands for faculty
}

To use parameterized constructor, send parameters just where you initialize as :

void main()
{
    string name = "Eugene";
    int roll = 123;
   float gpa = 3.25;
   string faculty = "Computer Science";

    Student s(name, roll, gpa, faculty);  //parameterized constructor is called

}

Don't use something like this :

Student s;
s(name, roll, gpa, faculty);   //it won't work!


Now what can be other utility functions? You can, for instance, make a Print() function that print the info of the student because you have no access to its name or gpa etc. The utility functions are also created inside the Public section so that they are available to the outside world.

void Print()
{
     cout <<endl << "Name : " << name;
     cout <<endl << "Roll number : " << roll_num;
     cout <<endl << "Gpa : " << gpa;
     cout <<endl << "Faculty: " << faculty;

}

Now, here's the point, to access the utility functions, use dot operator. Like;

s.Print();    //the data is displayed on the screen

Similarly, you can make other utility functions like GetName() which returns the name of the students and its return type is String e.g.

String GetName()
{
    return name;
}

The destructors are made in the classes which have Dynamic members. Since, here we haven't used any in this example so I'll be explaining in a later section.

Some Important Instructions:

  •   Make the class in a Header file. You can also make it above main() in the .cpp file but making it as a header makes it more accessible and you can include it in any program.
  • Name the header file as your class name e.g. student.h, you can name it whatever but naming it accordingly is just more organized. :P
   

  • To include it, use inverted commas instead of < >
   

The class will look something like this in basic.

Private members of the class are not accessible. This is Encapsulation.

If you have any questions, comment below.
Thank you for Reading!


Share:

0 comments:

Post a Comment

Popular Posts

Blog Archive

Copyright by progrithms.blogspot.com. All Rights Reserved.. Powered by Blogger.

Copyright © Programs and Algorithms | Powered by Blogger

Design by ThemePacific | Blogger Theme by NewBloggerThemes.com