Class is a key feature in C++. It makes programming much easier. To implement the class we need to create objects for that class. So, let's start with 'what is a class and an object?'

Class: Class is basically a template of variables and member functions.
Object: An object is an instance of a class.

How to create a class

To create a class you need to use the 'class' keyword. After that you need to specify the name of the class. The body of the class should be enclosed with curly braces. At the end you have to add a semicolon. For example,

class Circle(){ 
  private: 
  int r; 
  public: 
  getdata(); 
  area();
};

This is a way you create a class. You can write the function in the class and also outside the class. In the above example only the name of the function is written. This means that the function needs to be written outside the class. If you wanted you can write the function there itself like:

class Circle(){
  private: 
  int r; 
  public: 
  getdata(){ 
    cout<<"Enter the radius: "<<endl; 
    cin>>r; 
    } 
  area(){ 
    float a; 
    a=3.14*r*r; 
    cout<<"The area is : "<<a<<endl; 
    }
}; 

If you want to write the function outside the class you have to use the scope resoulion operator (::). Consider the following example:

void Circle::getdata(){ 
  cout<<"Enter the radius: "<<endl; cin>>r; 
}

Access Specifiers

They are used to specify which functions can access the data of a function. Private is an access specifier that is used to secure the data to restrict the other functions from accessing the data in a function. If you use the 'public' access specifier it can exchange the data with other functions and classes. If you don't specify anything, C++ will consider it as private.

Objects

Now to access those functions you need to create an object. To create an object you have to do it in the main function. You have to specify the name of the class. For example,

int main(){ 
  Circle c1; 
  c1.getdata(); 
  c1.area(); 
  getch(); 
  return 0;
}

Advantage of using classes

Suppose you have a large program to write which has to have a whole lot of functions. Then you can use the concept of classes. You can include all the same type of functions in one class. Then create objects for the class in the main function and use the objects. This will make the program more organized and easy to code.

Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Comments (1)

  1. Manoj lamba

Good description of classes in your own words but you have described classes in a very limited way. you have described private and public functions but not defined how one class can be given access to private members of other class. you can do...

Good description of classes in your own words but you have described classes in a very limited way. you have described private and public functions but not defined how one class can be given access to private members of other class. you can do this using 'friend' function.

Read More
  Attachments
 
There are no comments posted here yet