Hello, there is a task:

  1. Create class "one-dimensional vector of dimension 4"

  2. Create the appropriate methods:

    • assignment of matrix elements;
    • output matrix on the screen;
    • finding the maximum element of the matrix;
  3. Describe the 4 X 4 matrix class derived from it with the corresponding overloaded methods. Create objects of the "matrix" and "one-dimensional vector" classes.

  4. Find the maximum elements of each object.

One of the main conditions: there must be a constructor. I implemented the array input in the constructor, but now I ran into a problem when inheriting the constructor, in the class of the heir.

Here is the code:

using System; namespace Laba4 { class Matrix { public Matrix(int[] arr) { for (int i = 0; i < 4; i++) { arr[i] = Int32.Parse(Console.ReadLine()); } } public void Output(int[] arr) { Console.WriteLine("Your vector"); for (int i = 0; i < 4; i++) { System.Console.Write(arr[i] + " "); } } public int Ymova(int[] arr) { int max = 0; for (int i = 0; i < 4; i++) { if (arr[i] > max) max = arr[i]; } return max; } } class Matrix2d : Matrix { public Matrix2d(int[,] array) : base(array) // вот тут проблемка { } } class Program { static void Main(string[] args) { int[] array = new int[4]; Matrix matrix = new Matrix(array); matrix.Output(array); Console.WriteLine("max = " + matrix.Ymova(array)); Console.ReadKey(); } } } 

If you do without a base, it knocks out an error as for arguments, how can you get out of the situation? I cannot transfer a two-dimensional array to base.

  • 2
    Do your job, sorry, stupid condition. From the point of view of OOP, the matrix is not a special case of a vector, so the answer to the question “how to cram the matrix into a vector subclass” will be “normally nothing, the design of the class hierarchy is incorrect”. - VladD
  • and even if the opposite is inherited from a two-dimensional matrix to a vector? - M-Misha-M
  • 2
    See it. Class X is a subclass of class Y not when they have a common code, and when X is a special case of Y and wherever you use Y, you can use X without problems. For example, if you have the Animal and Antelope classes, then the second there is an obvious subclass of the first. - VladD
  • 2
    In your case, a one-dimensional array is not a special case of a two-dimensional matrix. Similarly, on the contrary, a two-dimensional matrix is ​​not a special case of a one-dimensional array. So none of them should properly be a subclass of the other. - VladD
  • Read a good book on the PLO. With your teacher you will not learn anything, I'm afraid. - VladD

1 answer 1

The task is pretty pointless in fact. Within the framework of this task, perhaps it makes sense to make an abstract class with the same abstract methods for outputting the elements of the array and searching for the maximum value, and then inherit two classes from it - a vector and a matrix and write your own implementations for each. If your teacher insists on the inheritance of the matrix from the vector, then he is an idiot - it can be done only with crutches.

Plus, besides, you wrote a very bad shitty implementation with hard-sized array sizes, passing the ready-made array to the constructor and the Console.ReadLine in the constructor, it’s good to get rid of it

UPD

then please enlighten me, maybe I’m really wrong, but how should I do it so that there is no hardcode in my case?

Enlighten:

First, you should not prescribe the size of the array directly in the cycle - this is a govnodische pure water. Your constructor takes a certain array of any length. Suppose an array that you pass to a constructor has not four, but three elements. This means that you will get an exception. In other words, nowhere and in no way do you indicate to a potential user of your class that its constructor expects not just an array, but an array of four elements. This is an extremely bad approach. Since you just need an array of four elements, then you do not need to pass it to the constructor at all, but you need to create it in this constructor. For example:

 public Matrix() { arr = new int[4]; // сам массив arr должен быть объявлен в классе for (int i = 0; i < 4; i++) arr[i] = Int32.Parse(Console.ReadLine()); } 

Secondly, if you really need to have four elements, then you do not need to register it in ten places. Enter a constant, assign it the desired value and use it in cycles. Then you do not need to change this magic number 4 wherever it occurs (and while puzzling, either you change the number itself, or it is some other "four"). For example:

 public class Matrix { private const int Size = 4; private int[] arr = new int[Size]; public Matrix() { for (int i = 0; i < size; i++) arr[i] = Int32.Parse(Console.ReadLine()); } } 

Third, you don't need to use any Console.ReadLine in the constructor. Because console input has no place in the constructor at all, and most importantly, because your class should not depend on the Console class. Moreover, if the Convert method cannot convert a string to a number (and this is quite a frequent case if, say, the user enters what the hell knows), then you will get an exception, which you do not process again. If you need to somehow initialize the array, then either transfer four separate values ​​to the constructor from the outside, or initialize it with random numbers. The array transfer option is not very good, I wrote about it above. It requires checking that the array contains exactly 4 elements, and, most importantly, does not tell the user about it.

Fourth, take a look at your methods - they all accept an array of integers, and in fact do nothing with it. Why? You should find the greatest values ​​of your vector, and not just what array, obtained as a parameter. Remove them from the parameters and use the internal private variable.

  • then please enlighten me, maybe I’m really wrong, but how should I do it so that there is no hardcode in my case? - M-Misha-M
  • ahh, you're talking about length? - M-Misha-M
  • @ M-Misha-M updated the answer - DreamChild