Classes with applications in C++

0
83
Linked Lists with applications in C++
constructors and destructors in C++

Classes are defined by using the keyword “ class ”  and have a body closed in braces {}  just like structures and ends with a semicolon( ; ). We cannot initialize any member inside the class, we need constructors for this, which will be studied further.  We differentiate classes from structures by using a term “Member Access Specifiers ”, means that we can keep limitations in the accessing of different data members of the class. The Member Access Specifiers are “Public, Private and Protected”. We will study Public and Private only.

Public:

Presents clients with a view of the services the classes provides(interface) and Data and member functions are accessible.

Private:

It is the Default access mode inside the classes. Here Data only accessible to member functions and friends. Private members are only accessible through the public class interface using public member functions.
Lets have a look at the example:

  1. // Classes in C++
  2. //Objec oriented programming
  3.  
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. class point
  8. {
  9. private:
  10.     int x;
  11.     int y;
  12. public:
  13.     point();
  14. };
  15.  
  16. point::point()
  17. {
  18.     x= 3;
  19.     y = 4;
  20.     cout<<“the values are :”<<“x = “<<x<<” and y = “<<y<<endl;
  21. }
  22.  
  23. int main()
  24. {
  25.     point p;
  26.         return 0;
  27. }

Now we see the need of access specifiers inside the classes, we cannot call for private members in our main() as we do in structures, Similarly we cannot initialize any data member inside our classes as we do in structures, we need a function for initialization, which is called a constructor, we will study constructors in details later. And these Access Specifiers and Constructors etc. make classes different from structures.

Linked Lists with applications in C++
constructors and destructors in C++

Leave a Reply