אתם מתרגלים שאלה מתוך בגרות מדעי המחשב — מבני נתונים (שאלון 899271)מבחן 2019 · קיץ מועד א · שאלה 16כל שאלות המבחן ←
תכנות מונחה עצמיםירושה

נתונות המחלקות First, Second (יורשת מ-First), Third, ומחלקת Driver עם הפעולה main שלפניך.

public class First {
    public static int count=0;
    private string str;
    public First (string str) {
        count ++ ;
        this.str = str;
    }
    public First (First g) {
        count ++ ;
        this.str = "Copy" + g.str;
    }
    public virtual void SetStr (string str) {
        this.str = str;
    }
    public virtual void Print () {
        Console.WriteLine ("First" + this.str) ;
    }
}

public class Second : First {
    private First f;
    public Second (First fx, First fy)
        : base (fx) {
        this.f = fy;
    }
    public override void SetStr (string str) {
        base.SetStr (str);
        this.f = new First (str);
    }
    public override void Print () {
        Console.WriteLine ("Second");
        base.Print ();
        this.f.Print ();
    }
}

public class Third {
    private int curr;
    private First[] arr;
    public Third (int count) {
        curr = 0;
        arr = new First [count];
    }
    public void Print () {
        for (int i = 0 ; i < curr ; i++)
            arr [i].Print ();
    }
    public void Add (First s) {
        if (curr < arr.Length) {
            arr [curr] = s;
            curr++;
        }
    }
}

public class Driver {
    public static void Main (string[] args) {
        First f1 = new First ("One") ;
        First f2 = new First ("Two") ;
        First f3 = new First (f2) ;
        Second s1 = new Second (f1, f3) ;
        Third t = new Third (First.count) ;
        t.Add (f1) ;
        t.Add (f2) ;
        t.Add (s1) ;
        Console.WriteLine (First.count) ;
        t.Print() ;
        Console.WriteLine (" ----------") ;
        f1.SetStr ("Five") ;
        Console.WriteLine (First.count) ;
        t.Print() ;
    }
}
סעיף א

צייר תרשים המראה את היחסים ההיררכיים בין המחלקות First, Second, Third. יש לסמן ירושה באמצעות חץ בעל ראש-חץ משולש-חלול, והרכבה (has-a) באמצעות סימון יהלום, כולל ריבוי היחס.

סעיף ב

עקוב אחר ביצוע הפעולה main של המחלקה Driver: (1) הצג את התוכן (השדות) של כל העצמים שנוצרו. (2) רשום את הפלט שיודפס.

שאלות ותגובות על השאלה

🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות

שאלו כאן — ותקבלו מענה מוסמך.

🎓 מרצה לתכנות עונה כאן — תקבלו מענה מקצועי