All Products
Search
Document Center

OpenSearch:Classes and objects

Last Updated:Feb 20, 2023

Overview

Cava allows you to define classes and create objects. This programming language supports the following concepts that are used in object-oriented programming:

  • Encapsulation

  • Abstraction

  • Class

  • Object

  • Instance

  • Method

In terms of object-oriented programming, the syntax of Cava is similar to that of Java or C#.

Example

In the following sample code, a class named Example is defined, an object named example is created by using the handler function, and a member variable named PI of the object is to be accessed:

class Example {  // <==
    double PI;
    Example() {
        PI = 3.1415926;
    }
    static int main() {
        Example example = new Example(); 
        double a = example.PI; // The value of a is 3.1415926.
        return 0;
    }
}

Features

Define a class

To define a class, you must use the class keyword. The defining of a class involves the following operations:

  • Define a member variable: A member variable carries the data of the relevant object.

  • Define a constructor: A constructor is used to initialize a created object.

  • Define a member function: A member function is a method to access the relevant object.

  • Define a class function: A class function is shared by all objects and can be accessed by using the class name.

class Example {        // Define a class named Example.
    public double PI;  // Define a member variable.
    Example() {        // Define a constructor.
        PI = 3.1415926;
    }
    double getPI() {   // Define a member function.
        return PI;
    }
    static int main() {
        Example example = new Example();
        double a = example.getPI(); // The value of a is 3.1415926.
        return 0;
    }
}

Create an object

After you define a class, you can use the class to create an object. The creation of an object involves the following operations:

  • Declaration: Declare a variable and specify the class of the variable.

  • Instantiation: Use the new keyword to create an instance of an object.

  • Initialization: Specify a constructor next to the new keyword. The constructor is used to initialize the object.

The following sample code provides an example on how to create an object. The first line of the main function is used to create an object of the Example class and store the object in the example variable. After the object is created, you can use the example variable to access the data members of the object.

class Example {
    double PI;
    Example() {
        PI = 3.1415926;
    }
    static int main() {
        Example example = new Example(); 
        return 0;
    }
}

Define a member variable

The following sample code provides an example on how to define a member variable named PI of the DOUBLE type:

class Example {
    double PI; // <==
    Example() {
        PI = 3.1415926;
    }
}

Note: When you define a member variable, you cannot specify an initial value for the variable. You must use a constructor to initialize a member variable.

class Example {
    double PI = 3.1415926; // When you define a member variable, you cannot initialize the variable.
}

Define a constructor

A constructor is a special member function in Cava. The following limits are imposed on constructors:

  • The name and class name of a constructor must be the same.

  • A constructor does not have a return value. You cannot specify the output parameters.

A constructor is automatically called after the relevant object is created. The commands in the constructor are run to initialize the object. Cava allows you to define multiple constructors. The specified syntax of the new command determines the constructor that is used to create and initialize an object. The following sample code provides an example on how to create two objects by using the main handler function. The first object is initialized by using Constructor A. After the initialization, the value of the PI member variable is 3.1415926. The second object is initialized by using Constructor B. After the initialization, the value of the PI member variable is 3.14.

class Example {
    public double PI;
    Example() { // <== A
        this.PI = 3.1415926;
    }
    Example(double customPI) { // <== B
        this.PI = customPI;
    }
    double getPI() {
        return PI;
    }
    static int main() {
        Example example1 = new Example(); // Use Constructor A to initialize the created object.
        double a = example1.getPI();
        Example example2 = new Example(3.14); // Use Constructor B to initialize the created object.
        double b = example2.getPI()
        return 0;
    }
}

Note: Constructors support function overloading.

Define a member function

A member function encapsulates the operations related to member variables. A consumer of an object can call a member function to read and modify the relevant member variable. The member variable defined in a member function can be accessed by using the member function. If a variable in the scope of the member function shares the same name with the member variable, the variable in the scope takes precedence when you access variables by specifying the variable name. In this case, you can add the this prefix to the variable name to access the member variable.

class Example {
    public double PI;
    Example() {
        this.PI = 3.1415926;
    }
    void getPI() {
        return PI;
    }
    static int main() {
        Example example = new Example();
        double a = example.getPI();
        return 0;
    }
}

Note: Member functions support function overloading. You can define multiple member functions with the same name in a class. The compiler chooses an optimal function based on the type and number of input parameters that are specified in the code.

Define a class function

A class function belongs to the relevant class and is shared by all the data members of the class. You can use the static keyword to define a class function for a class. Then, you can use the class to access the class function without the need to create an object. However, you must use an object to access a member function.

class Example {
    static double getPI() { // <==
        return 3.1415926;
    };
    static int main() {
        double a = Example.getPI();
        return 0;
    }
}

Inheritance and polymorphism

Cava does not support class inheritance.