程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 第七周-第一部分

第七周-第一部分

編輯:C++入門知識

print?/*       
* 程序的版權和版本聲明部分       
* Copyright (c)2013, 煙台大學計算機學院學生       
* All rightsreserved.       
* 文件名稱: object.cpp       
.* 作者:楊紹寧      
* 完成日期: 2013年  4  月 12 日       
* 版本號: v1.0       
* 輸入描述:無       
* 問題描述: 靜態的數據成員      
* 程序輸出:體積。       
*/               
#include <iostream>  
#include <string>  
using namespace std; 
class Box 

 public: 
 Box(int w,int l):width(w),length(l){} 
 int volume( ){return height*width*length;}; 
 private: 
 static int height;  //靜態的數據成員  
 int width; 
 int length; 
}; 
int Box::height=2; 
int main() 

    Box b(3,4); 
    cout<<"volume is "<<b.volume()<<endl; 
    system("pause"); 

/*      
* 程序的版權和版本聲明部分      
* Copyright (c)2013, 煙台大學計算機學院學生      
* All rightsreserved.      
* 文件名稱: object.cpp      
.* 作者:楊紹寧     
* 完成日期: 2013年  4  月 12 日      
* 版本號: v1.0      
* 輸入描述:無      
* 問題描述: 靜態的數據成員     
* 程序輸出:體積。      
*/             
#include <iostream>
#include <string>
using namespace std;
class Box
{
 public:
 Box(int w,int l):width(w),length(l){}
 int volume( ){return height*width*length;};
 private:
 static int height;  //靜態的數據成員
 int width;
 int length;
};
int Box::height=2;
int main()
{
    Box b(3,4);
    cout<<"volume is "<<b.volume()<<endl;
    system("pause");
}


結果:\

感受:靜態數據成員不能用構造函數初始化表,要在類外賦值。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved