Thursday, April 11, 2013

Program to understand concept of Class and Object

After getting the basic difference between syntax of C and C++ we can develop almost all C programs in C++ also. C++ also support same conditional statements, loops and the logic for the code.
Here is the list of C programs that can be developed in C++ also with minor changes in syntax as I described in my last post.
Basic C Programs.

Now we will take a look at new concepts introduced in C++ :

Class
Before reading concept of class you should have basic idea of Structure in C.
Structure allows us to store different data types at one place.
Class also follow the same concept but it is advanced so it enables user to define  function with data members. Means user can define different data members in a class as we do in structure and user can also define functions based on those data members.

Object
Object is a run time entity. Object is a variable of a class, that allows user to access class data members and functions.
It is also known as Instance.

A program to understand class and object concept:



In this program we have class named student which contains s_name, s_address, s_marks[5], s_percentage as data members, and we have two function get() and display(). Here we are using get() function to get value for class data members and display() function to print the value of class data members.
we declared an object like this :

student stud;

Above syntax is to declare an object, student is our class name and stud is our object.
Using this object we are calling class functions get() and display() like this :

stud.get();
stud.display();

So here is all we did with the help of object of the class. We accessed class functions outside the class using objects.

0 comments:

Post a Comment

Powered by Blogger.