migration C#, Java, C++ (day 2)

logo

 

Day 2. I feel some heat already 😉

 

managed code

public class Dog {
   public string Name { get; set; }

   public Dog() { }
   public Dog(string xName) { Name = xName; }
}

// --------------------------------------------------------------

{
   Dog lDog1 = new Dog();
   Dog lDog2 = new Dog();
   lDog1 = null;  // allows the garbage collector to collect lDog1
} // leaving the scope allows lDog2 to be collected
public class Dog {
  public Dog() {}
  public Dog(String xName) { setName(xName); }

  private String _Name;
  public final String getName() { return _Name; }
  public final void setName(String value) { _Name = value; }
} // class

// --------------------------------------------------------------

{
  Dog lDog1 = new Dog();
  Dog lDog2 = new Dog();
  lDog1 = null; // allows the garbage collector to collect lDog1
} // leaving the scope allows lDog2 to be collected
// .h file
#ifndef DOG_H
#define DOG_H

#include <string>
#include <memory>

using namespace std;

class Dog : public enable_shared_from_this<Dog> {
private:
   string pName;
public:
  const string &getName() const;
  void setName(const string &xName);

  Dog();
  Dog(const string &xName);
};

#endif // DOG_H

// .cpp file

#include "Dog.h"

using namespace std;

Dog::Dog(){}
Dog::Dog(const string &xName) { setName(xName); }

const string &Dog::getName() const { return pName;}
void Dog::setName(const string &value) { pName = value; }


// --------------------------------------------------------------

shared_ptr<Dog> lDog1 (new Dog("Snoopy"));
cout<<lDog1.use_count() << endl; // 1
shared_ptr<Dog> lDog2 = make_shared<Dog>("Lassie");
cout<<lDog2.use_count() << endl; // 1
shared_ptr<Dog> lDog3 = lDog2;
cout<<lDog1.use_count() << endl; // 1
cout<<lDog2.use_count() << endl; // 2
cout<<lDog3.use_count() << endl; // 2

Dictionary

Dictionary<string, Double> lDictionary = new Dictionary<string, double>();
lDictionary.Add("-10-", 10.0);
lDictionary.Add("-20-", 20.0);
lDictionary.Add("-30-", 30.0);
lDictionary.Add("-40-", 40.0);
lDictionary.Remove("-30-");

foreach (var lPair in lDictionary) Console.WriteLine(lPair.Key + " equals " + lPair.Value);

foreach (var lValue in lDictionary.Values) Console.WriteLine(lValue);

Double d;
if (lDictionary.TryGetValue("-10-", out d)) Console.WriteLine("found -10- which is: " + d);
java.util.HashMap<String, Double> lDictionary = new java.util.HashMap<String, Double>();
lDictionary.put("-10-", 10.0);
lDictionary.put("-20-", 20.0);
lDictionary.put("-30-", 30.0);
lDictionary.put("-40-", 40.0);
lDictionary.remove("-30-");
// C# var: There is no such as implicit typing in Java
for (Entry<String, Double> lPair : lDictionary.entrySet()) System.out.println(lPair.getKey() + " equals " + lPair.getValue());
for (Double lValue : lDictionary.values()) System.out.println(lValue);

double d = 0;
if (lDictionary.containsKey("-10-") ? (d = lDictionary.get("-10-")) == d : false) System.out.println("found -10- which is: " + d);
unordered_map<string, double> lMap = unordered_map<string, double>();
lMap.insert(make_pair("-10-", 10.0));
lMap.insert(make_pair("-20-", 20.0));
lMap.insert(make_pair("-30-", 30.0));
lMap.insert(make_pair("-40-", 40.0));
lMap["-40-"] = 41.0;
lMap.erase("-30-");

for (auto &lPair : lMap) {
  cout << lPair.first << " equals " <<  lPair.second << endl;  // 10 20 41
}

double d1 = lMap["-10-"];	
cout << "found -10- which is: " << d1 << endl;	// 10.0
	
double d2 = lMap["-11-"];  // returns the default value
cout << "found -11- which is: " << d2 << endl; // 0.0

unordered_map<string, string> lMap2 = unordered_map<string, string>();	
lMap2["hello"] = "hulla";
string s = lMap2["hola"]; 
if (s == "") s = "EMPTY";
cout << "lMap2[\"hola\"] == " << s << endl; // s == "EMPTY"

Queue

Queue<Dog> lDogs = new Queue<Dog>();
lDogs.Enqueue(new Dog("Boomer"));
lDogs.Enqueue(new Dog("Spike"));
lDogs.Enqueue(new Dog("Lassie"));
Console.WriteLine(lDogs.Peek().Name); // Boomer
Dog lDog = lDogs.Dequeue();
Console.WriteLine(lDogs.Peek().Name); // Spike
// Queue is not a class, just an interface
java.util.Queue<Dog> lDogs = new java.util.LinkedList<Dog>();
lDogs.offer(new Dog("Boomer"));
lDogs.offer(new Dog("Spike"));
lDogs.offer(new Dog("Lassie"));
System.out.println(lDogs.peek().getName()); // Boomer
Dog lDog = lDogs.poll();
System.out.println(lDogs.peek().getName()); // Spike
#include <queue>

queue<Dog*> lDogs = queue<Dog*>();
lDogs.push(new Dog("Boomer"));
lDogs.push(new Dog("Spike"));
lDogs.push(new Dog("Lassie"));
cout << lDogs.front()->getName() << endl; // Boomer
cout << lDogs.back()->getName() << endl;  // Lassie
lDogs.pop(); // takes Boomer from the queue
cout << lDogs.front()->getName() << endl; // Spike
cout << lDogs.back()->getName() << endl;  // Lassie

Stack

Stack<Dog> lDogs = new Stack<Dog>();
lDogs.Push(new Dog("Boomer"));
lDogs.Push(new Dog("Spike"));
lDogs.Push(new Dog("Lassie"));
Console.WriteLine(lDogs.Peek().Name); // Lassie
Dog lDogLassie = lDogs.Pop();
Console.WriteLine(lDogs.Peek().Name); // Spike
java.util.Stack<Dog> lDogs = new java.util.Stack<Dog>();
lDogs.push(new Dog("Boomer"));
lDogs.push(new Dog("Spike"));
lDogs.push(new Dog("Lassie"));
System.out.println(lDogs.peek().getName()); // Lassie
Dog lDogLassie = lDogs.pop();
System.out.println(lDogs.peek().getName()); // Spike
stack<Dog*> lDogs = stack<Dog*>();
lDogs.push(new Dog("Boomer"));
lDogs.push(new Dog("Spike"));
lDogs.push(new Dog("Lassie"));
cout << lDogs.top()->getName() << endl; // Lassie
lDogs.pop(); // takes Lassie from the stack
cout << lDogs.top()->getName() << endl; // Spike

Generics, Templates

T larger<T>(T obj1, T obj2 ) {
   if ((dynamic)obj1 > (dynamic)obj2) return obj1;
   return obj2;
} //

int i = larger<int>(6, 4); // i == 6

bool same<T>(T obj1, T obj2 ) where T : class {
   return (obj1 == obj2);
} //

bool b1 = same<string>("ABC", "abc"); // False
bool b2 = same<string>("abc", "abc"); // True

public class MyClass<T, U> where T : struct where U : class {
   public T MyFunc(T a, U b, T c) {
      return c;
   }
}

MyClass<int, string> lClass = new MyClass<int, string>();
int i = lClass.MyFunc(0, "hello", 1); // i == 1
<T extends Comparable<T>> T larger(T obj1, T obj2) {
  if (obj1.compareTo(obj2) > 0) return obj1;
  return obj2;
}

int i = this.<Integer>larger(6, 4); // i == 6

<T extends Comparable<T>> boolean same(T obj1, T obj2) {
  return (obj1 == obj2);    
}

boolean b1 = this.<String>same("ABC", "abc"); // False
boolean b2 = this.<String>same("abc", "abc"); // True

class MyClass<T, U> {
  public final T MyFunc(T a, U b, T c) { return c; }
}

MyClass<Integer, String> lClass = new MyClass<Integer, String>();
int i = lClass.MyFunc(0, "hello", 1); // i == 1
template <typename T>
T larger(T obj1, T obj2){
    if(obj1 > obj2) return obj1;
    return obj2;
} //

int i = larger(6, 4); // i == 6

template <typename T>
bool same(T obj1, T obj2){
    if(obj1 == obj2) return true;
    return false;
} //

bool b1 = same("ABC", "abc"); // false
bool b2 = same("abc", "abc"); // true
bool b3 = same("abd", "abc"); // false    


template<typename T, typename U>
   class MyClass {
       U MyData;
public:
      T MyFunc(T a, U b, T c) { return c; }
      void MyFunc(int a, U b) { }
   };

MyClass<int, string> lClass;
int i = lClass.MyFunc(0, "hello", 1); // 1

simple Thread

No worries, there will be follow-ups.

private void MyThread(object x) {
  Console.WriteLine(x as string);
}

Thread lThread = new Thread(MyThread);
lThread.IsBackground = true;
lThread.Start("mySupaDupaaParameter");
public class MyThreadClass extends Thread {
  private String _Parameter;

  public MyThreadClass(String xParameter) {
    _Parameter = xParameter;
  } // constructor

  @Override
  public void run() {
    System.out.println(_Parameter);
  } //
} // class

MyThreadClass lInstance = new MyThreadClass("mySupaDupaaParameter");
Thread lThread = new Thread(lInstance);
lThread.setDaemon(true);
lThread.start();
#include <thread>

void MyThread(string xParameter) { cout << xParameter; }

thread lThread(MyThread, "mySupaDupaaParameter");
Advertisements

About Bastian M.K. Ohta

Happiness only real when shared.

Posted on February 25, 2014, in C#, C++, Java and tagged , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: