第六週 項目一



<方法一>  常成員函數  常對象  
#include <iostream>
#include <stdlib.h>
using namespace std;
class C
{
private:

            int x;
public:
       C(int x) {this->x=x;}
       int getX() const {return x;}   //常成員函數    

};
int main()
{
    const C c(5);   //定義C是常對象,在定義常對象時必須同時對之初始化,之後不能改變
    cout<<c.getX();
    return 0;
}

* 程序的版權和版本聲明部分    
* Copyright (c)2013, 煙臺大學計算機學院學生    
* All rightsreserved.    
* 文件名稱: object.cpp    
* 作    者: 袁靜    
* 完成日期:2013年4月4日    
* 版本號: v1.0    
* 輸入描述:
* 問題描述:  
  程序輸

<方法二>  普通對象

#include <iostream>
#include <stdlib.h>
using namespace std;
class C
{
private:

            int x;
public:
       C(int x) {this->x=x;}
       int getX()  {return x;}     普通成員函數

};
int main()
{
     C c(5);            c爲類C的一個對象
    cout<<c.getX();
    return 0;
}
          比起方法一與方法二我更喜歡方法二,因爲我覺得在方法二更方便,當然方法一
    
    也有它的好處,對象的值不會改變,也許因爲我是一個懶人,我更喜歡方法二,雖然安全性不高

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章