Swift Program To Create A Class With The User-defined Methods

You are currently viewing Swift Program To Create A Class With The User-defined Methods

Swift Program To Create A Class With The User-defined Methods

// Swift program to create a class with 
// the user-defined methods

import Swift

class Sample {
    var num1:Int
    var num2:Int

    init(num1:Int, num2:Int) {
        self.num1 = num1
        self.num2 = num2
    }

    func setvalues(num1:Int, num2:Int) {
        self.num1 = num1
        self.num2 = num2
    }

    func printvalues() {
        print("Num1: ",num1)
        print("Num2: ",num2)
    }
}

let obj1 = Sample(num1:100,num2:200)

print("Object1: ")
obj1.printvalues()

let obj2 = Sample(num1:0,num2:0)
obj2.setvalues(num1:1000,num2:2000)

print("Object2: ")
obj2.printvalues()

Output

Object1: 
Num1:  100
Num2:  200
Object2: 
Num1:  1000
Num2:  2000

...Program finished with exit code 0
Press ENTER to exit console.

Ranjith

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

Leave a Reply