Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

General Form of a Class

class Class_Name
{
DataType instance_variable;
DataType method_name(parameter_list)
{
// body of method
}
}
Declaring Objects
Obtaining objects of a class is a two-step process.
• First, must declare a variable of the class type
• Second, must acquire an actual, physical copy of the object
and assign it to that variable
General form of Objects

Class_Name Object_Name ;
Object_Name = new Class_Name( );

Box mybox; // declare reference to object


mybox = new Box( ); // allocate a Box object
or

Class_Name Object_Name = new Class_Name( )

Box mybox = new Box();


Declaring Objects
Declaring Objects
The new operator dynamically allocates memory for an
object and returns a reference to it.
This reference is, the address in memory of the object
allocated by new.
This reference is then stored in the variable

The dot operator links the name of the object with the
name of an instance variable
Difference b/w Class and Objects
Assigning Object Reference Variables
Box b1 = new Box();
Box b2 = b1;
General form of a method
DataType Method_Name(parameter_list)
{
// body of method

return Value;
}
Constructors
Automatic initialization all of the variables in a class each time
an instance is created.
It has the same name as the class in which it resides.
The constructor is automatically called immediately after the
object is created, before the new operator completes.
Implicit return type of a class constructor is the class type itself

You might also like