To create a class in C++, you use the class keyword followed by the class name and the class definition contained within curly braces:
51 chars4 lines
In Swift, you'll use the class keyword and the class name, but Swift uses two curly braces to define the class body:
main.swift50 chars4 lines
Here's an example of a class definition in C++ that includes a constructor:
92 chars7 lines
And here's an example in Swift that includes a constructor:
main.swift81 chars6 lines
Note that the syntax for defining a constructor is slightly different between the two languages. In C++, you define the constructor as a function with the same name as the class. In Swift, you use the init
keyword followed by any arguments to the constructor.
gistlibby LogSnag