티스토리 뷰

from Effective C++ by Scott Meyers

- Experiments in C++ and Java. Let's compare the results..



// main.cpp
#include <iostream>
using namespace std;

class Glass {
public:    
    Glass() {
        this->sayHello();   
    }
    
    virtual void sayHello() {
        cout << "Hello~ I'm a glass." << endl;        
    }
    
};

class WineGlass : public Glass {
public:
    WineGlass() {
        this->sayHello();   
    }
    
    virtual void sayHello() {
        cout << "Hello~ I'm a wine glass." << endl;
        
    }
    
};


int main()
{
    WineGlass wg;
    
    system("pause");
    return 0;
}


Hello~ I'm a glass.
Hello~ I'm a wine glass.



// Test.java
class Glass {
	
	{
		System.out.println("initializing variables of Glass..");	
	}
	
	public Glass() {
		System.out.println("calling Glass()..");		
		sayHello();		
	}	
	
	
	public void sayHello() {
		System.out.println("This is a glass.");
	}
	
}


class WineGlass extends Glass {
	
	int a;
	
	{
		System.out.println("initializing variables of WineGlass..");
		a = 7;	
	}
	
	public WineGlass() {
		System.out.println("calling WineGlass()..");		
		sayHello();		
	}
	
	public void sayHello() {
		System.out.println("Hello~ This is a wine glass.");	
		System.out.println(""+a);
	}
	
	
}

public class Test {
	
		
	
	public static void main(String[] args)
	{
		WineGlass wg = new WineGlass();	
	}
	
}



initializing variables of Glass..
calling Glass()..
Hello~ This is a wine glass.
0
initializing variables of WineGlass..
calling WineGlass()..
Hello~ This is a wine glass.
7
댓글