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

לפניכם המחלקות First ו-Second שהמחלקה Second יורשת מהמחלקה First, והמחלקה Tester עם הפעולה main ובה חמש הצהרות עצמים ולאחריהן הערה // ***, המסמנת מיקום להוספת פקודה.

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. Total=First.getCount(). 2. Total=Second.getCount(). 3. sum=s1.sum(). 4. 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).
שאלות ותגובות על השאלה

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

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

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