Classes and Objects
If you have some basic knowledge of C programming language (Hello World C Program), then you must be familiar with the concept of structures. Here is how a structure looks in C:
struct random{ int num; char ch; float marks; }obj;
You’ll observe that a structure is just a collection of various types of data in C. In the above example, all these variables can be accessed through the structure object obj.
Just the way structure encapsulates only data, classes can encapsulate data as well as functions. Also, in the same way, object refers to instantiation of the class.
Here is an example of a class declaration in C++ :
class example{ int var1, var2; public: void set_request(int a, int b); int sum(){return (var1+var2);} }class_obj;
So you see that the above example contains data (var1 and var2) and also a couple of functions. Also, you will see a string ‘public:’ before the function declarations. This is nothing but an access specifier that specifies the access permissions of the data and functions defined after it.
There can be three access specifiers in C++ :
- Private : This access specifier makes sure that the private members can be accessed from other members of the class only or from the friends of class (this is an advanced concept, will learn it later).
- Protected : This access specifier makes sure that the protected members can be accessed from members of the same class ,from the friend classes and also from members of the derived classes.
- Public : This access specifier makes sure that the public members can be accessed from anywhere through the object of the class.
Coming back to the example program, it’s time now to expand it further and define the function set_request.
Here is the example :
class example{ int var1, var2; public: void set_request(int a, int b); int sum(){return (var1+var2);} }class_obj; void example::set_request(int a, int b) { var1 = a; var2 = b; }
In the above example, you will observe that the function set_request() is defined outside the class. The :: operator used in the definition is the syntax for defining class functions outside of the class. Basically, the :: operator is known as scope resolution operator, so this operator tells the compiler about the class scope of the function.
If you observe closely, you will find that the function sum() is defined within the class but the function set_request() is defined outside of the class. There is only one difference between the two, the functions declared inside the class are treated as Inline functions while those defined outside the class are treated as normal functions.
Now, lets complete our example program by defining a main() function and calling the functions of the class from it.
Here is the example :
#include<iostream> class example{ int var1, var2; public: void set_request(int a, int b); int sum(){return (var1+var2);} }class_obj; void example::set_request(int a, int b) { var1 = a; var2 = b; } int main(void) { class_obj.set_request(1,4); std::cout<<"n The sum is "<<class_obj.sum()<<"n"; return 0; }
Here is the output :
$ ./example The sum is 5
So we can see that inside the main() function, the class object class_obj was used to call the function set_request() with parameters 1 and 4. Next, a call to sum() was done with similar style to return the sum of numbers. The basic header file used for C++ programs in iostream.h as compared to stdio.h which is used in case of C programs.
NOTE : The members var1 and var2 are private members despite no access specifier was defined before declaring them. This is because private is the default access specifier and there is no requirement of mentioning it separately. Though it is advisable to do so to improve the code readability. Also, as the variables var1 and var2 are private members of the class so they can only be accessed within class functions (and friends of class) but not inside the main() function directly.
You must be logged in to post a comment.