In: Computer Science
Using*************** C++ **************** explain what Objects and Classes are. Please describe in detail.
CLASS :
C++ is an object-oriented programming language.
A class is a user-defined data type, which contains its own data members and member functions(Methods) , which can only be accessed and operated by creating an instance of that class.
Hence, a class is said as a blueprint for the object.
CREATION OF A CLASS :
class ClassName
{
public : // access specifier
int name ; // attrbutes
void fun(); //member functions
} ;
EXPLANATION OF ABOVE CLASS ;
To use classes we write the keyword class followed by the name of class. The body of class is defined inside the curly brackets where we can include the access specifier and the attributes and the member functions ,and the class is terminated by a semicolon at the end.
OBJECT :
When a class is defined, only the specification for the object is defined; and no memory is allocated for defining that class.
But , to use the data and access member functions present in the class, we need to create objects.
Hence, Object is created by following the below statement :
ClassName ObjectName;
EXAMPLE OF OBJECT :
For example , in above , we created a class named "ClassName" , and if to use the attributes and methods of that class, we need to create the object by writing the below statement :
ClassName ob1;
where,
"ClassName" is the name of the class and ob1 is the name of the object for class "ClassName".
And to use the attributes / methods of class "ClassName" we write :
ob1.name ; //to access name of class "ClassName"
ob1.fun(); //to access method of the class "ClassName"