ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Swift program to implement single inheritance

Here, we are going to learn how to implement single inheritance in Swift programming language?
Submitted by Nidhi, on July 14, 2021

Problem Solution:

Here, we will create two classes Employee and Accountant with data members. Then we will inherit the Employee class into the Accountant class to implement single inheritance.

Program/Source Code:

The source code to implement single inheritance is given below. The given program is compiled and executed successfully.

// 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.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift

Here, we created two classes Employee and Accountant. The Employee class contains two data members empId and empName. The Accountant class also contains two data members salary and bonus. Both Employee and Accountant class contains methods to set and print data members. Then we inherited the Employee class into the Accountant class. After that, we created the objects of the Accountant class and set and print Accountant information.

Swift Inheritance Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.