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

לפניכם המחלקות First ו־Second, והמחלקה Tester:

public class First
{
    private static int count = 0;
    protected int x;
    protected int y;
    public First (int num) {
        this.x = num; this.y = num; count ++;
        Console.WriteLine ("First 1");
    }
    public First (int num1, int num2) {
        this.x = num1; this.y = num2; count ++;
        Console.WriteLine ("First 2");
    }
    public static int GetCount() { return count; }
    public int GetX() { return x; }
    public int GetY() { return y; }
    public virtual int Sum() { return this.x + this.y; }
    public virtual void Add (First other) {
        this.x += other.x; this.y += other.y;
        Console.WriteLine ("x = " +this.x + " y = " +this.y);
    }
}

public class Second : First
{
    private int z;
    public Second (int num) : base (num) {
        this.z = num;
        Console.WriteLine ("Second");
    }
    public override int Sum() { return base.Sum() + this.z; }
    public override void Add (First other) {
        this.x += other.GetX(); this.y += other.GetY();
        if (other is Second)
            this.z += ((Second)other).z;
        Console.WriteLine("x = "+ this.x + " y =" + this.y+ " z =" +this.z);
    }
}

class Tester
{
    static void Main(string[] args)
    {
        First f1 = new First (40);
        First f2 = new First (40, 50);
        First f3 = new Second (100);
        Second s1 = new Second (100);
        Second s2 = new Second (100);
        // ***
    }
}
סעיף א

ציירו את העצמים שנוצרו בפעולה main, וכתבו את הפלט של הפעולה.

סעיף ב

הציבו כל אחת מן הפקודות 1–10 שלהלן בפעולה Main במקום המצוין לעיל ב־***. כתבו את מספר הפקודה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין – כתבו את הפלט, ואם הוא אינו תקין, הסבירו מדוע. הערה: אין קשר בין הפקודות. כלומר, יש להתייחס לכל פקודה כאילו היא היחידה בפעולה Main.

  1. Console.WriteLine ("Total = " + First.GetCount());
  2. Console.WriteLine ("Total = " + Second.GetCount());
  3. Console.WriteLine ("sum = " + s1.Sum());
  4. Console.WriteLine ("sum = " + f3.Sum());
  5. s1 = new First (100);
  6. f1.Add (s2);
  7. s1.Add (s2);
  8. s2.Add (f3);
  9. ((First)s1).Add (f1);
  10. s1 = new Second (100, 100);
שאלות ותגובות על השאלה

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

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

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