簡單說說C++和Java語法上的不同之處

main函數:

  • java 中當前運行的類是主類的時候調用

  • C++:

    自由遊離的函數,不定義在類內

#include<string>
#include<iostream>
using namespace std;
class Student
{
public:
	Student(int n, string nam, char s)
	{
		num = n;
		name = nam;
		sex = s;
		cout << "Constructor called." << endl;
	}
	~Student()
	{
		cout << "Destructor called." << endl;
	}
	void display()
	{
		cout << "num:" << num << endl;
		cout << "name:" << name << endl;
		cout << "sex:" << sex << endl << endl;
	}
private:
	int num;
	string name;
	char sex;
};
int main()
{
	Student stud1(10010, "Wang_li", 'f');
	stud1.display();
	Student stud2(10011, "Zhang_fang", 'm');
	stud2.display();
	system("pause");
	return 0;
}

可以看出C++ 中的main函數相當於是調用函數的起點或者是入口,當然啦Java中也是吧。但是C++ 這個是寫在類的外面,而Java就不是了。

類的聲明

  • Java不需要在結尾的括號出加“;”

  • C++ 需要在結尾“;”

方法的調用

寫法都相同

  • Java是必須在類內部,且方法是以權限修飾符來限定的(public,protect,private),如果Java中方法沒有寫限定符,也就是不寫,那麼其方法是在這個包內可以調用。
  • C++中類中可以寫方法聲明,也可以寫方法實現,但是,方法聲明後,在類外的實現需要用到作用域限定符"  ::  "
class Student
{
    public:
        void display();
    private:
        int num;
        string name;
        char sex;
};

void Student::display()
{
    cout<<"num"<<num<<endl;
    cout<<"name"<<name<<endl;
    cout<<"sex"<<sex<<endl;
}
Student stu1,stu2;

如果函數作用於運算符前沒有類名,或是既沒有類名也沒有作用域運算符如

::display()

display()

那麼這就是一個全局函數,即普通函數。

類函數必須在類體中做原型聲明,在類外定義實現。(C++中建議用這種寫法)

靜態方法和靜態變量

靜態方法寫法都一樣

Java可以用靜態初始化塊來初始化靜態變量,不需要在源文件中聲明

class Foo{
    static private int x;
    {
        x=5;
    }
}

對象的聲明

  • Java中:
    • 總是在堆中聲明的
      MyTest test=new MyTest();
  • C++中:
    • 在棧中
      MyClass test;
    • 在堆中
      MyTest *test=new MyTest;

繼承

  • Java中:
    class Foo : public Bar
    {

    }
  • C++中:
    class Foo extends Bar{

    }

訪問級別(主要是寫法不同)

  • Java:
    public void f1();
    public void f2();
  • C++:
    public :
        void f1();
        void f2();

內存管理

都用new 來分配,但是Java中不用delete,因爲它有自己的垃圾回收機制

常量

  • Java:
    final int x=7;
  • C++
    const int x=7;

數組

  • Java:
int[] x = new int[10];
// 使用 x, 內存有垃圾回收器回收或
//或在程序生命週期盡頭歸還給系統
  • C++:
int x[10];
//或
int *x = new x[10];
// 使用 x,然後歸還內存
delete[] x;

集合和迭代器

  • Java:

    迭代器只是一個接口,範圍的開始是 <集合>.iterator,你必須用itr.hasNext()來查看是否到達集合尾。

    ArrayList myArrayList = new ArrayList();
    Iterator itr = myArrayList.iterator();
    while ( itr.hasNext() )
    {
        System.out.println( itr.next() );
    }
  • C++:

    迭代器是類的成員。範圍的開始是<容器>.begin(), 結束是 <容器>.end()。 用++ 操作符遞增, 用 *操作符訪問。

for (std::vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
{
	*iter = 0; // set element to which iter refers to 0
}

抽象類

  • Java:

    1. abstract修飾符
    abstract class Bar{
        public void test();
    }
    
    1. 接口
    interface Bar{
        public void test();
    }
    
    1. 繼承抽象類或實現接口
    class Wine extends / implements Bar{
        public void test(){
            system.out.println("No beer!");
        }
    }
    
  • C++:

    只需要包含一個純虛函數

    class Bar{
        public:
            virtual void foo()=0;
    }
    

引用與指針

  • Java:
 // 引用是可變的,僅存儲對象地址; 
 // 沒有指針類型
    myClass x;
    x.foo(); // error, x is a null ``pointer''
    
 // 注意你要總是用 . 來訪問域
  • C++:
 // 引用不可改變,通過使用指針來獲得更多的靈活性
    int bar = 7, qux = 6;
    int& foo = bar;

編譯

  • Java:
// 編譯foo.java文件中的類成<classname>.class    javac foo.java 
// 通過調用<classname>中的靜態main方法來運行
    java <classname>
  • C++:
// 編譯
   g++ foo.cc -o outfile
// 運行
   ./outfile
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章