Shuffle array in a class

This entry is part 33 of 41 in the series Swift

import UIKit

class ArrayObject : NSObject {
    
    var numbers = [1,2,3,4,5]
    
    func shuffleNumbers() {
        numbers.shuffle()
    }
}

extension MutableCollection where Indices.Iterator.Element == Index {
    /// Shuffles the contents of this collection.
    mutating func shuffle() {
        let c = count
        guard c > 1 else { return }
        
        for (unshuffledCount, firstUnshuffled) in zip(stride(from: c, to: 1, by: -1), indices) {
            let d: IndexDistance = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
            guard d != 0 else { continue }
            let i = index(firstUnshuffled, offsetBy: d)
            swap(&self[firstUnshuffled], &self[i])
        }
    }
}

let array = ArrayObject()

array.shuffleNumbers()

print(array.numbers[1])

Swift: Simple inheritance with a method

This entry is part 21 of 41 in the series Swift

This is an inherited class with a call to the parent’s method.

class Person {
    var name = ""
    var age = 0
    
    init(){
        self.name = ""
        self.age = 0
    }
    
    init(name:String,age:Int){
        self.name = name
        self.age = age
    }
    
    func printIntro(){
        print("My name is \(self.name) My age is \(self.age)")
    }

}


class Student : Person {
    
}

var student = Student(name: "Taylor Swift", age: 20)

student.printIntro()

swift

Swift: Class

This entry is part 14 of 41 in the series Swift

In Swift, let let us defines a constant variable.  Notice in the example below, it is used to define an instance of the Music class.

import UIKit

class Music {

    var numRock = 10
    var numCountry = 20
}

let musicGenre = Music()

print(musicGenre.numRock)
print(musicGenre.numCountry)

musicGenre.numCountry = 30
print(musicGenre.numCountry)

swift

C++: Friend functions

This entry is part 59 of 61 in the series C++

Sometimes, you might have an independent function and you might want this function to access the private members of a class.

You can declare this independent function as friend of the class. A friend can access any of the members of a class object, regardless of their access specification.

In the example below, we have an independent function called CurrentWeek.  We have declared CurrentWeek within the Album class that it is a friend function.

With this declaration, the function can access the private variable top10 of the Album class.

#include <iostream>

using namespace std;

class Album {
    
private:
    int top10;

public:

    Album(void) : top10(0) {

    }

    void show(void) {

        cout << top10 << endl;
    }

    friend void CurrentWeek(Album &);

};

void CurrentWeek(Album &alb) {

    alb.top10 = alb.top10 + 5;
}

int main() {

    Album album;
    album.show();

    CurrentWeek(album);
    album.show();

}

friend

C++: Pointers in class

This entry is part 55 of 61 in the series C++

Pointers in C allow dynamic memory allocation.  In a dynamic variable like a pointer, the compiler will not need to pre-allocate a chunk of memory.  Memory is allocated when needed during run time.

An example of a predefined memory allocation is in declaring an array where a fix set of memory will be allocated for the variable.

The same goes to defining pointers for a class.  The memory space will be allocated dynamically in real time when the program is running.  Now you do not have to know the memory needed in advance.

#include <iostream>
#include <string>

using namespace std;

class Rock {
public:
    int Show();
    
};

int Rock::Show() {

    cout << "Wonderwall by Oasis";
    cout << endl;
    return 0;
}
 
int main() {

    Rock *ptrRock; // instance as a pointer declaration
    ptrRock->Show();
    return 0;
}

pointer