אתם מתרגלים שאלה מתוך מה"ט מבני נתונים ותכנות מונחה עצמים — הנדסאי תוכנהמבחן 2025 · אביב מועד ב · שאלה 10כל שאלות המבחן ←
תכנות מונחה עצמיםירושה

בבית החולים, לכל רופא מתמחה הוצמד רופא ותיק מנוסה. כדי לנהל את תהליך ההתמחות פותחו המחלקות האלה: Doctor, Intern, Test.

המחלקה Doctor מתארת רופא:

public class Doctor {
    protected string name;// שם
    protected string specialization;// התמחות
    protected int numOfPatients; // מספר חולים
    public Doctor(string name, string spec) {
        this.name = name;
        this.specialization = spec;
        this.numOfPatients = 0;
    }
    public Doctor(string name, string spec, int num) {
        this.name = name;
        this.specialization = spec;
        this.numOfPatients = num;
    }
    public Doctor(Doctor other) {
        this.name = other.name;
        this.specialization = other.specialization;
        this.numOfPatients = other.numOfPatients;
    }
    public void AddPatients(int num)
    {
        if(numOfPatients + num >=0)
            this.numOfPatients += num;
    }
    public override string ToString(){
        return "Doctor:" + name + ", " + specialization+ ","
            +numOfPatients;
    }
}

במחלקה הוגדרו גם פעולות set/Get לכל התכונות.

המחלקה Intern מתארת מתמחה:

public class Intern : Doctor {
    private Doctor mentor;
    public Intern(string name, string spec, Doctor mentor):
        base(name, spec){
        this.mentor = mentor;
        this.numOfPatients = mentor.numOfPatients/2;
    }
    public Doctor GetMentor(){return mentor;}
    public override string ToString() {
        return "Intern: " + name + ", " + specialization +
               ", Mentor: " + mentor.name +", " +
               mentor.numOfPatients +"," + numOfPatients;
    }
}

הפעולה הראשית main (במחלקה Test):

public static void Main(string[] args) {
    Doctor[] d = new Doctor[9];
    d[0] = new Doctor("Dr. Cohen", "Cardiology",12);
    d[1] = new Doctor("Dr. Levy", "Neurology");
    d[2] = new Doctor("Dr. Sharon", "Pediatrics",8);
    d[3] = new Intern("Dani", "Cardiology", new Doctor(d[0]));
    d[4] = new Intern("Yael", "Surgery", d[0]);
    d[5] = new Intern("Avi", "Pediatrics", new Doctor(d[2]));
    d[6] = new Intern("Ruth", "Oncology", d[2]);
    d[7] = new Intern("Noam", "Cardiology", new Doctor(d[1]));
    d[8] = new Intern("Maya", "Neurology", new Doctor(d[0]));
    for (int i=0; I < d.Length; i++) {
        Console.WriteLine(d[i]);
    }
    d[0] = new Doctor("Dr. Goldman","Neurology",20);
    d[2].SetName("Dr. Galper");
    d[2].AddPatients(100);
    d[3].AddPatients(200);
    d[5].AddPatients(100);
    Console.WriteLine("After change:");
    for (int i=0; i < d.Length; i++) {
        Console.WriteLine(d[i]);
    }
}
סעיף א

עקבו אחרי הביצוע של הפעולה main ורשמו מה יהיו ערכי התכונות של כל עצם שנוצר במהלך הביצוע.

סעיף ב

מהו הפלט של הפעולה main?

סעיף ג

כתבו פעולה חיצונית המקבלת מערך רופאים ומדפיסה את שמותיהם של כל המתמחים אשר ההתמחות שלהם שונה מההתמחות של המנחים שלהם.

כותרת הפעולה:

public static void PrintNotSameSpec(Doctor[] d)
שאלות ותגובות על השאלה

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

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

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