What Is The Difference Between Class & Objects?

5

5 Answers

Naureen Khan Profile
Naureen Khan answered
The easiest way to explain the difference between an object and a class would be through an example.

Suppose that you created a class called Counter. This class is very simple and only contains one PRIVATE DATA MEMBER called Current Count of type unsigned int. Also, suppose that this class has two PUBLIC MEMBER FUNCTIONS. One, called Increment Count, simply increments the current value of Current Count by one. The other member function is called Return Current Count and simply returns the value of Current Count data member.

Now that you have created this class you wish to use it in your program, so that let's suppose when a button INCREMENT is pressed value of CurrentCount is incremented and when button SHOW VALUE is pressed the current value of CurrentCount is displayed.

In order to use this class you must create an object of the class in the button click function. Once an object of the class is made you can access the public functions (IncrementCount and ReturnCurrentCount ) using the dot operator.

An example of this would be:

Counter myCounter;

myCounter.IncrementCount ();

Response.write (ReturnCurrentCount());

Hence, a class is a user-defined data type. You can create your own class with data members and member functions that cater to your programming need.

But, in order to use this data type that you have created you must declare (instantiate) its object, and use the DOT OPERATOR to access its public functions.
ghazal gi Profile
ghazal gi answered
Class and object both are the terminologies which are used in many programming languages.

About the class, so class is the main thing with the help of which a program is made ,if once the class is created it remains till the end of the program, we can't made another class with the same name of the previous class.

We made functions within the class and within the function we use attributes. Function name and the class name should not be the same.While the object are made with in the class and the objects time span is very less as the class finish the object is also destroyed.
Every object belongs to a class and every class contains one or more related objects.
Simply in one sentence "we made objects within the class".

To differentiate the class and object there is an example.We consider a class "car", which have many attributes like seats, wheels and engine and also have the members of the class, so these members are the objects of that class.

Class is static. The attributes of a class always remain unchanged. Class and object both are related to each other, without class, object is of no use and without object, class is useless. So both are important for each other.

Answer Question

Anonymous