Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Tech-prog(theory).docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
114 Кб
Скачать
☆

11) Creation and using one-dimensional and multi-dimensional arrays in the programming language Java.

Java, as with most languages, supports multi-dimensional arrays - 1-dimensional, 2-dimensional, 3-dimensional, ... In practice most arrays are one-dimensional, and two-dimensional (rows and columns) are also quite common. Higher dimensional arrays are less common so they aren't used in the examples, but there's nothing mysterious about them and the same principles apply.

Two-dimensional arrays are used whenever the model data is best represented with rows and columns, or has two varying aspects (eg, gender and age, weight and height, ...). It's also the idea, altho not the implementation, of graphics that specifies a two (or three) dimensional position with an x and y (and z) coordinate.

Java builds multi-dimensional arrays from many one-dimensional arrays, the so-called "arrays of arrays" approach.

There are a couple of interesting consequences of this: Rows may be different sizes. Also, each row is an object (an array) that can be used independently.

Multi-dimensional arrays are built from multiple one-dimensional arrays

As with all arrays, the new keyword must be used to allocate memory for an array. For example,

int[][] a = new int[2][4];

This two-dimensional array will have two rows and four columns.

This actually allocates 3 objects: a one-dimensional array of 2 elements to hold each of the actual row arrays, and a two one-dimensional arrays of 4 elements to represent the contents of the rows.

In Java two-dimensional arrays are implemented is a one-dimensional array of one-dimensional arrays -- like this.

12) Inheritance in the programming language C++

inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.

Inheritance Example:

Sample Code

#include <iostream>

using namespace std; 

class exforsys

{

public:

        exforsys(void) { x=0; }

        void f(int n1)

        {

                x= n1*5;

        } 

 

        void output(void) { cout << "n" << "x=" << x; } 

 

private:

        int x;

}; 

 

class sample: public exforsys

{

public:

        sample(void) { s1=0; } 

 

        void f1(int n1)

        {

                s1=n1*10;

        } 

 

        void output(void)

        {

                exforsys::output();

                cout << "n" << "s1=" << s1;

        }private:

        int s1;

}; int main(void)

{      sample s;

        s.f(10);

        s.f1(20);

        s.output();

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]