• Classes

    A class in programming is like a group of people. For example, if you have a group of friends, each person in the group is called a "friend". In programming, we call each person in the group a "class". Just like your friends might all have different names and personalities, classes can also have different names and qualities. So when you write code, you can create different classes with different names and abilities to do things.

      • Object Classes

      • D

        Example of object class in d.
        import std.stdio : write, writeln;
        import std.string;
        
        class Person {
        	string name;
        	int age;
        
        	this(string name, int age) {
        		this.name = name;
        		this.age = age;
        	}
        
        	void greet() {
        		write("Hello my name is ", this.name);
        	}
        }
        
        void main()
        {
        	Person human = new Person("John", 36);
        
        	writeln(human.name); // John
        	writeln(human.age); // 36
        	human.greet(); // Hello my name is John
        }
      • Trait Classes

      • D

        Example of trait class in d.
        import std.stdio : writeln;
        import std.string;
        
        mixin template HasFlippers() {
        	string swim() {
        		return "can swim";
        	}
        }
        
        mixin template HasFeathers() {
        	string fly() {
        		return "can fly";
        	}
        }
        
        class Duck {
        	mixin HasFlippers;
        	mixin HasFeathers;
        }
        
        class Seal {
        	mixin HasFlippers;
        }
        
        void main() {
        	Duck a = new Duck();
        	writeln(a.swim()); // can swim
        	writeln(a.fly()); // can fly
        
        	Seal b = new Seal();
        	writeln(b.swim()); // can swim
        }
      • Interface Classes

      • D

        Example of interface class in d.
        import std.stdio : writefln;
        import std.math.constants;
        
        interface Shape {
        	double area();
        }
        
        class Rectangle : Shape {
        	double width;
        	double height;
        
        	this(double width, double height) {
        		this.width = width;
        		this.height = height;
        	}
        
        	double area() {
        		return this.width * this.height;
        	}
        }
        
        class Circle : Shape {
        	double radius;
        
        	this(double radius) {
        		this.radius = radius;
        	}
        
        	double area() {
        		return PI * this.radius * this.radius;
        	}
        }
        
        void main()
        {
        	Rectangle a = new Rectangle(3, 5);
        	Circle b = new Circle(4);
        
        	writefln("area of a is %f", a.area());  // area of a is 15.000000
        	writefln("area of b is %f", b.area());  // area of b is 50.265482
        }
      • Struct Classes

      • D

        Example of struct class in d.
        import std.stdio : writeln;
        import std.string;
        
        struct Person {
        	string name;
        	int age;
        
        	this(string name, int age) {
        		this.name = name;
        		this.age = age;
        	}
        }
        
        void main()
        {
        	Person human = Person("John", 36);
        
        	writeln(human.name); // John
        	writeln(human.age); // 36
        }
      • Enum Classes

      • D

        Example of enum class in d.
        import std.stdio : write;
        
        enum Days {
        	Sunday,
        	Monday,
        	Tuesday,
        	Wednesday,
        	Thursday,
        	Friday,
        	Saturday
        }
        
        void main()
        {
        	int day = 4;
        
        	if (day == Days.Saturday || day == Days.Sunday) {
        		write("a weekend");
        	} else {
        		write("a weekday"); // a weekday
        	}
        }