Lecture/객체지향프로그래밍및실습/2009/Java와 C++의 비교

Retired DISLab
< Lecture | 객체지향프로그래밍및실습 | 2009
Swpark (토론 | 기여) 사용자의 2014년 1월 20일 (월) 22:46 버전
(비교) ← 이전 판 | 현재 판 (비교) | 다음 판 → (비교)
이동: 둘러보기, 찾기

목차

Class

C++

class DoubleArray
{
    double *arr;
 
protected:
    int size;
 
public:
    DoubleArray(int sz);
    virtual ~DoubleArray() { delete[] arr; }
 
    virtual double get(int idx) const;
    virtual void put(int idx, double data);
 
    void print() const;
};
 
DoubleArray::DoubleArray(int sz)
    : size(sz)
{
    arr = new double[size];
    memcpy(arr, 0x0, sizeof(double) * size);
}
 
double DoubleArray::get(int idx) const
{
    return arr[idx];
}
 
void DoubleArray::put(int idx, double data)
{
    arr[idx] = data;
}
 
void DoubleArray::print() const
{
    for(int i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << endl;
}
 
//------------------------------------------
void f(DoubleArray *pa)
{
    pa->put(0, 3.3);
    pa->put(1, 8.1);
    pa->put(2, 9.0);
    pa->get(1);
    pa->print();
}


Java

public class DoubleArray {
    private double[] arr;
    protected int size;
 
    public DoubleArray(int sz) {
        size = sz;
        arr = new double[size];
    }
 
    public double get(int idx) {
        return arr[idx];
    }
 
    public void put(int idx, double data) {
        arr[idx] = data;
    }
 
    public void print() {
        for(int i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
 
    static void f(DoubleArray pa) {
        pa.put(0, 3.3);
        pa.put(1, 8.1);
        pa.put(2, 9.0);
        pa.get(1);
        pa.print();
    }
 
    public static void main(String[] arg) {
        DoubleArray a = new DoubleArray(10);
        f(a);
    }
}


Inheritance

C++에서의 계승

#include <stdlib.h>
 
class MyArray : public DoubleArray
{
public:
    MyArray(int sz) : DoubleArray(sz) { }
    ~MyArray() { }
 
    double get(int idx) const;
    void print() const;
};
 
double MyArray::get(int idx) const
{
    if (idx < 0 || idx >= size)
        exit(1);
 
    return DoubleArray::get(idx);
}
 
void MyArray::print() const
{
    cout << "Oh My Array" << endl;
    DoubleArray::print();
}
 
int main()
{
    DoubleArray *a = new MyArray(10);
    f(a);
    delete a;
}


Java에서의 계승

public class MyArray extends DoubleArray {
    public MyArray(int sz) {
        super(sz);
    }
 
    public double get(int idx) {
        if (idx < 0 || idx >= size)
            System.exit(1);
        return super.get(idx);
    }
 
    public static void main(String[] arg) {
        DoubleArray a = new MyArray(10);
        f(a);
    }
}


Abstract Class

C++에서의 Abstract Class

#include <iostream>
#include <stdio.h>
 
using namespace std;
 
class DrawObj
{
public:
    virtual ~DrawObj() { }
    virtual void draw() = 0;
};
 
class Line : public DrawObj
{
public:
    Line() { printf("Line()\n"); }
    ~Line() { printf("~Line()\n"); }
 
    virtual void draw();
};
 
class Circle : public DrawObj
{
public:
    Circle() { printf("Circle()\n"); }
    ~Circle() { printf("~Circle()\n"); }
 
    virtual void draw();
};
 
void Line::draw()
{
    cout << "Line::draw()" << endl;
}
 
void Circle::draw()
{
    cout << "Circle::draw()" << endl;
}
 
int main()
{
    DrawObj *p = new Line();
    p->draw();
    p = new Circle();
    p->draw();
    delete p;
    return 0;
}


Java에서의 Abstract Class

// DrawObj.java
public abstract class DrawObj {
    abstract public void draw();
}
 
// Line.java
public class Line extends DrawObj {
    public Line() {
        System.out.println("Line()");
    }
 
    public void draw() {
        System.out.println("Line::draw()");
    }
}
 
// Circle.java
public class Circle extends DrawObj {
    public Circle() {
        System.out.println("Circle()");
    }
 
    public void draw() {
        System.out.println("Circle::Line()");
    }
}
 
// App.java
public class App {
    public static void main(String[] arg) {
        DrawObj p = new Line();
        p.draw();
        p = new Circle();
        p.draw();
    }
}


객체의 복제

C++의 복제 (Copy Constructor)

#include <stdlib.h>
#include <string.h>
 
class DoubleArray
{
    double *arr;
 
protected:
    int size;
 
public:
    DoubleArray(int sz = 10);
    DoubleArray(const DoubleArray&);
    virtual ~DoubleArray() { delete[] arr; }
 
    double get(int idx) const;
    void put(int idx, double data);
};
 
DoubleArray::DoubleArray(int sz)
    : size(sz)
{
    arr = new double[size];
}
 
DoubleArray::DoubleArray(const DoubleArray& o)
    : size(obj.size)
{
    arr = new double[size];
    memcpy(arr, o.arr, sizeof(double)*size);
}
 
int main()
{
    DoubleArray a;
    DoubleArray b(a);    // DoubleArray b = a;
}


Java에서의 복제 (clone)

public class DoubleArray implements Cloneable {
    protected int size;
    private double[] arr;
 
    public DoubleArray(int sz) {
        size = sz;
        arr = new double[size];
    }
 
    public DoubleArray clone()
           throws CloneNotSupportedException {
        DoubleArray obj =
                   (DoubleArray)super.clone();
        obj.arr = arr.clone();
        return obj;
    }
 
    public double get(int idx) {
        return arr[idx];
    }
 
    public void put(int idx, double data) {
        arr[idx] = data;
    }
 
    public void print() {
        for(int i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
 
    public static void main(String[] arg) {
        DoubleArray a = new DoubleArray(10);
        DoubleArray b;
        try {
            b = a.clone();
        } 
        catch(CloneNotSupportedException e) {
            System.out.println("Error");
        }
    }
}
개인 도구
이름공간
변수
행위
둘러보기
구성원
연구
연구실
기타
도구모음
인쇄/내보내기