home > 比較編 > C, C++, Objective-C, Java, C# > objects >

ForNext

Only Do What Only You Can Do

119. constructor

VBScript

JScript

Perl

PHP

Python

Ruby

PowerShell

Scala

F#

C

C++

Rational::Rational(int n, int d) : num(n), denom(d) {
  if (denom == 0) {
    throw "zero denominator";
  }
  int div = gcd(n,d);
  num = num / div;
  denom = denom / div;
}





C++Builder

VC++

C#

public Rational(int n, int d) {
  if (0 == d) {
    throw new System.Exception("zero denominator");
  }
  if (d < 0) {
    this.num = -1 * n;
    this.denom = -1 * d;
  }
  else {
    this.num = n;
    this.denom = d;
  }
}

Java

public Rational(int n, int d) throws Exception {
  if (d == 0) {
    throw new Exception("zero denominator");
  }
  if ( d < 0 ) {
    this.num = -1 * n;
    this.denom = -1 * d;
  }
  else {
    this.num = n;
    this.denom = d;
  }
}

Objective-C

-(Rational*) initWith: (int) n: (int) d {
  self = [super init];
  if (self) {
    self.num = n;
    self.denom = d;
  }
  return self;
}





D

VB

VB.NET

Delphi

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system