Swift Program To Implement Single Inheritance

Swift Program To Implement Single Inheritance

Swift Program To Implement Single Inheritance

// Swift program to implement single inheritance

import Swift

class Employee {
    var empId: Int = 0
    var empName: String = ""

    func setEmp(id: Int, name: String) {
        empId = id
        empName = name
    }

    func printEmp() {
        print("\tEmployee Id  : ", empId)
        print("\tEmployee Name: ", empName)
    }
}

class Accountant: Employee {
    var salary: Float = 0
    var bonus: Float = 0

    func setInfo(id: Int, name: String, s: Float, b: Float) {
        setEmp(id:id, name:name)
        salary = s
        bonus = b
    }

    func printInfo() {
        printEmp()
        print("\tEmployee Salary: ", salary)
        print("\tEmployee Bonus : ", bonus)
    }
}

var acc1 = Accountant()
var acc2 = Accountant()

acc1.setInfo(id:101, name:"Arun", s:12345.23, b:220.34)
acc2.setInfo(id:102, name:"Amit", s:12000.45, b:245.34)

print("Accountant1:")
acc1.printInfo()

print("Accountant2:")
acc2.printInfo()

Output

Accountant1:
        Employee Id  :  101
        Employee Name:  Arun
        Employee Salary:  12345.23
        Employee Bonus :  220.34
Accountant2:
        Employee Id  :  102
        Employee Name:  Amit
        Employee Salary:  12000.45
        Employee Bonus :  245.34

...Program finished with exit code 0
Press ENTER to exit console.
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment