C++ vs Java


C++ and Java may be the two most popular object-oriented programming languages. Below shows the differences between them:

C++
Java
Width =     Height =    

       
Width =     Height =    

       
Rectangle.cpp
Rectangle.java
#include <iostream>
#include <cstdlib>
using namespace std;

class Rectangle {
 int width, height;
public:
 void set_values( int, int );
 int area( ) { return  width * height; }
};

void Rectangle::set_values( int x, int y ) {
 width  = x;
 height = y;
}

int main( int argc, char *argv[ ] ) {
 cout << "Content-type: text/html\n\n";
 cout << "<html><body>";
 Rectangle rect;
 int a = std::atoi( argv[1] );
 int b = std::atoi( argv[2] );
 rect.set_values( a, b );
 cout << "Area = " << rect.area( ) << endl;
 cout << "</body></html>";
 return 0;
}
public class Rectangle {
 public static void main( String args[ ] ) {

  // Creating a Rectangle object
  Rectangle rect = new Rectangle( );

  // Converting a string to an integer
  int w = Integer.parseInt( args[0] );
  int h = Integer.parseInt( args[1] );

  // Calling the methods
  rect.set_values( w, h );
  System.out.println(
   "Area = " + rect.area( ) );
 }

 private int width, height;

 public void set_values( int x, int y ) {
  width  = x;
  height = y;
 }

 public int area( ) {
  return( width * height );
 }
}




      Don’t put all your eggs in one basket.