C++ Class and Object

C++ Class

A class is the collection of related data and function under a single name. A C++ program can have any number of classes. When related data and functions are kept under a class, it helps to visualize the complex problem efficiently and effectively.

 

A Class is a blueprint for objects

When a class is defined, no memory is allocated. You can imagine like a datatype.
int var;
The above code specifies var is a variable of type integer; int is used for specifying variable var is of integer type. Similarly, class are also just the specification for objects and object bears the property of that class.

Defining the class in C++

Class is defined in C++ programming using keyword class followed by identifier(name of class). Body of class is defined inside curly brackets an terminated by semicolon at the end in similar way as structure.
				 class class_name 
					{ 
						// some data
						// some functions 
					};
					

Example of Class in C++

					class temp 
					{ 
						private: 
							int data1; 
							float data2; 
						public: 
							void func1()
								{ data1=2; } 
							float func2() {
								data2=3.5;
								return data; 
								} 
					}; 
					
Explanation

As mentioned, definition of class starts with keyword class followed by name of class(temp) in this case. The body of that class is inside the curly brackets and terminated by semicolon at the end. There are two keywords: private and public mentioned inside the body of class.

Keywords: private and public

Keyword private makes data and functions private and keyword public makes data and functions public. Private data and functions are accessible inside that class only whereas, public data and functions are accessible both inside and outside the class. This feature in OOP is known as data hiding. If programmer mistakenly tries to access private data outside the class, compiler shows error which prevents the misuse of data. Generally, data are private and functions are public.

C++ Objects

When class is defined, only specification for the object is defined. Object has same relationship to class as variable has with the data type. Objects can be defined in similary way as structure is defined. class_name variable name;

Syntax to Define Object in C++

For the above defined class temp, objects for that class can be defined as: temp obj1,obj2; Here, two objects(obj1 and obj2) of temp class are defined.

Data member and Member functions

The data within the class is known as data member. The function defined within the class is known as member function. These two technical terms are frequently used in explaining OOP. In the above class temp, data1 and data2 are data members and func1() and func2() are member functions.

Accessing Data Members and Member functions

Data members and member functions can be accessed in similar way the member of structure is accessed using member operator(.). For the class and object defined above, func1() for object obj2 can be called using code: obj2.func1(); Similary, the data member can be accessed as: object_name.data_memeber;
Note: You cannot access the data member of the above class temp because both data members are private so it cannot be accessed outside that class.

C# Class and Object

C# Class

A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only access it through the class itself, not an instance variable.

 

Declaring Class in C#

Classes are declared by using the class keyword, as shown in the following example:
C#
public class Customer
{
    //Fields, properties, methods and events go here...
}


Example of Class in C#

				using System;

				class Example
				{
					public Example(int property)
					{
						Property = property;
					}
					public int Property { get; private set; }
				}

				
				Another example:
				class Program
				{
					static void Main()
					{
					Example example = new Example(5);
					Console.WriteLine(example.Property);
					}
				}
					

C# Objects

Although they are sometimes used interchangeably, a class and an object are different things. A class defines a type of object, but it is not an object itself. An object is a concrete entity based on a class, and is sometimes referred to as an instance of a class. Objects can be created by using the new keyword followed by the name of the class that the object will be based on, like this:

C#

Customer object1 = new Customer();

When an instance of a class is created, a reference to the object is passed back to the programmer. In the previous example, object1 is a reference to an object that is based on Customer. This reference refers to the new object but does not contain the object data itself. In fact, you can create an object reference without creating an object at all:

C#

Customer object2;

We do not recommend creating object references such as this one that does not refer to an object because trying to access an object through such a reference will fail at run time. However, such a reference can be made to refer to an object, either by creating a new object, or by assigning it to an existing object, such as this:

C#

Customer object3 = new Customer(); Customer object4 = object3;

This code creates two object references that both refer to the same object. Therefore, any changes to the object made through object3 will be reflected in subsequent uses of object4. Because objects that are based on classes are referred to by reference, classes are known as reference types.

Example of Object in C#

The object type is an alias for Object in the .NET Framework. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. You can assign values of any type to variables of type object. When a variable of a value type is converted to object, it is said to be boxed. When a variable of type object is converted to a value type, it is said to be unboxed. For more information, see Boxing and Unboxing.

The following sample shows how variables of type object can accept values of any data type and how variables of type object can use methods on Object from the .NET Framework.

					
C#
class ObjectTest
{
   public int i = 10;
}

class MainClass2
{
   static void Main()
   {
      object a;
      a = 1;   // an example of boxing
      Console.WriteLine(a);
      Console.WriteLine(a.GetType());
      Console.WriteLine(a.ToString());

      a = new ObjectTest();
      ObjectTest classRef;
      classRef = (ObjectTest)a;
      Console.WriteLine(classRef.i);
   }
}
/* Output
    1
    System.Int32
    1
 * 10
*/
					

JAVA Class and Object

JAVA Class

A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors. Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object.

 

Local variables
Local variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.

Instance variables
Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.

Class variables
Class variables are variables declared with in a class, outside any method, with the static keyword.

Defining the class in JAVA

A class is defined in a class definition. A class definition defines attributes and methods that are members of the class. The form used to define a class is dependent on the programming language used to write the program. Here is a simple class definition using Java:
				
				Class RegistrationForm
				{
				Int studentNumber;
				Int courseNumber;
				};
				
				A class definition has three parts:
				-Keyword class
				-Class name
				-Class body
				

Example of Class in JAVA

class myMath { //Methods public int Add(int i, int j) { return i + j; } }

JAVA Object

The Object Class is the super class for all classes in Java. Some of the object class methods are: equals toString() wait() notify() notifyAll() hashcode() clone()
An object is an instance of a class created using a new operator. The new operator returns a reference to a new 
instance of a class. This reference can be assigned to a reference variable of the class. The process of creating
objects from a class is called instantiation. An object encapsulates state and behavior. An object reference
provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via
references, which can be stored in variables.

Example of Defined Object in JAVA

public class Cube 
{

	int length = 10;
	int breadth = 10;
	int height = 10;
	public int getVolume() 
	{
		return (length * breadth * height);
	}
	public static void main(String[] args) {
		Cube cubeObj; // Creates a Cube Reference
		cubeObj = new Cube(); // Creates an Object of Cube
		System.out.println("Volume of Cube is : " + cubeObj.getVolume());
	}
}

CLASS AND OBJECT VIDEOS

C++ CLASS AND OBJECT



C# CLASS AND OBJECT



JAVA CLASS AND OBJECT