Question

In: Computer Science

(3) Briefly (in just a few words), describe the difference between an instance and a class.

(3) Briefly (in just a few words), describe the difference between

an instance and a class.

Solutions

Expert Solution

For better understanding of the concepts, We will first look on class and then an instance.

The Concepts are explained in  C++ Language!

Class:

In Object Oriented Programming, Class is a Building block and it acts as a container contains set of variables and functions.

Syntax of a Class:

class class_name

{

Set of variables;

Set of functions;

}

Example:

class sum // class named 'sum; is created.

{

public:// Elements of the class can be accessed outside class.

int a,b,c;// the class 'sum' contains three variables 'a,b,c' of type integer.

a=10,b=5;// 'a' is assigned with value 10 and 'b' is assigned with value 5

void add()// function is created with name 'sum'

{

c=a+b;// value in the variables a and b are added ans the result is stores in variable c

cout<<c;// value in variable'c' is printed.

}

};// end of the class

An Instance :

An instance is otherwise called as Objects in Object Oriented Programming.

An object is an instance(copy) of a class. When the object is created, memory will be allocated to all the elements of a class.

In the above example, we have created a class called 'sum' , but no process will takes place in it.

We have to activate the class with the help of object.( An instance).

Syntax for cretating object:

class_name object_name;

Example:

sum obj; // Here 'obj' is the instance of the class 'sum'.

We can use the instance 'obj' to access the members of the class.

Syntax to access function of a class with object:

object_name.function_name();

Example:

obj.add();// Now, the instance(obj) of the class 'sum' will access the function 'add'.

So, the instance is using the copy of a class and necessary process will takes place.

Output:

15

Differences:

Class:

1. A class is a template.

2. A class does not allocate memory space to its element, when it is created.

3. You can declare class only once.

An instance(Object):

1. The object is an instance of a class.

2. Object allocates memory space for the class elements whenever they are created.

3. we can create more than one object for one class

Hope you find it useful!! Thnak you!!! Happy learning!!!


Related Solutions

1. Describe the difference between instance methods and class methods in Java and give an example...
1. Describe the difference between instance methods and class methods in Java and give an example of each. 2. A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application? 3. Describe the difference between abstract classes and concrete classes, giving an example of each. 4. Explain how data are encapsulated and information is hidden in Java? 5. Explain the difference between a class and an interface...
in a few words, what is the difference between mutual and standard fund?
in a few words, what is the difference between mutual and standard fund?
Briefly describe the difference between recursive and iterative DNS query?
Briefly describe the difference between recursive and iterative DNS query?
3) What is the difference between selling and marketing? Explain briefly.
3) What is the difference between selling and marketing? Explain briefly.
Briefly describe the tradeoff between equity and efficiency of tax systems using a few examples.
Briefly describe the tradeoff between equity and efficiency of tax systems using a few examples.
Describe the difference between making a class a member of another class (object composition) and making...
Describe the difference between making a class a member of another class (object composition) and making a class a friend of another class. Explain why memberwise assignment can cause problems with a class that contains a pointer member. Explain why the parameter of a copy constructor must be a reference.
In a few paragraphs - Discuss the difference between complicity and vicarious liability. Describe which crimes...
In a few paragraphs - Discuss the difference between complicity and vicarious liability. Describe which crimes might involve complicity and which crimes might involve Vicarious liability.
Briefly describe the difference between temporary and chronic poverty. Give examples
Briefly describe the difference between temporary and chronic poverty. Give examples
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have a constructor Sentence(int size), where size determines the length of the sentence field of a given Sentence. The class should have an instance method public boolean isValid() that determines the validity of a sentence according to the rules detailed below. Also create a public nested class Word, with instance variables String value and Type type. Within this class, you must create: A public enum...
With a minimum of 300 words, describe the difference between the AutoFilter and the Advanced Filter...
With a minimum of 300 words, describe the difference between the AutoFilter and the Advanced Filter features. Provide an example of a situation when it would be beneficial to use one feature over the other.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT