欧美色欧美亚洲高清在线观看,国产特黄特色a级在线视频,国产一区视频一区欧美,亚洲成a 人在线观看中文

  1. <ul id="fwlom"></ul>

    <object id="fwlom"></object>

    <span id="fwlom"></span><dfn id="fwlom"></dfn>

      <object id="fwlom"></object>

      C++課程上機(jī)實驗常見錯誤匯集

      時間:2019-05-13 02:22:25下載本文作者:會員上傳
      簡介:寫寫幫文庫小編為你整理了多篇相關(guān)的《C++課程上機(jī)實驗常見錯誤匯集》,但愿對你工作學(xué)習(xí)有幫助,當(dāng)然你在寫寫幫文庫還可以找到更多《C++課程上機(jī)實驗常見錯誤匯集》。

      第一篇:C++課程上機(jī)實驗常見錯誤匯集

      C++課程上機(jī)實驗常見錯誤匯集

      1. 調(diào)試器錯誤信息:syntax error : missing ';'

      原因:在源碼中遺失“;”

      2.調(diào)試器錯誤信息:例:error C2065: 'cout' : undeclared identifier.原因:例如cout/cin/endl/<>等在命名空間中定義的符號和標(biāo)示符無法使用。缺少命名空間使用定義:即缺少“using namespace std;”

      3. 調(diào)試器錯誤信息:例:error C2065: 'i' : undeclared identifier原因:變量未定義就直接使用.C++語言中,變量的使用必需遵循先聲明定義,后使用的原則。

      4.調(diào)試器錯誤信息:error C2018: unknown character '0xa3'

      原因:在程序中使用中文標(biāo)示符,如將英文”;”錯誤輸入成了”;”在C++中,除程序注釋可以采用中文外,其余字符要求使用英文。不少同學(xué)在建立工程或程序名稱時也使用中文名稱,建議改掉這種習(xí)慣。

      5.調(diào)試器錯誤信息:例:error C2676: binary '>>' : 'class std::basic_ostream >' does not define this operator or a conversion to a type acceptable to the predefined operator

      原因:在使用輸入輸出流的時候錯誤使用了標(biāo)示符“>>”“<<”,例cout>>a;

      6.調(diào)試器錯誤信息:warning C4305: 'initializing' : truncation from 'const double' to 'float'

      原因:定義的變量類型與使用不對應(yīng),如聲明為float,但實際給與了一個double的值,例:float pi=3.***;

      7.調(diào)試器錯誤信息:warning C4700: local variable 'a' used without having been initialized

      原因:變量在賦值之前就使用,例:int a, b, c;c=a+b;cin>>a>>b;

      8. error C2556: 'int __cdecl main(void)' : overloaded function differs only by return type from 'void __cdecl main(void)'

      E:tempalskdfldid.cpp(4): see declaration of 'main'

      E:tempalskdfldid.cpp(15): error C2371: 'main' : redefinition;different basic types

      原因:在一個工程中包含多于一個的main函數(shù)

      9.調(diào)試器錯誤信息:error C2447: missing function header(old-style formal list?)

      原因:在函數(shù)定義的()后面使用分號

      例:void chang();

      {?..}

      10.調(diào)試器錯誤信息:error C2660: 'chang' : function does not take 2 parameters

      原因:函數(shù)聲明/定義/調(diào)用參數(shù)個數(shù)不匹配。

      例:void chang(int a,int b, float c)

      {?}

      void main()

      {?chang(3,4);}

      11、atal error C1010: unexpected end of file while looking for precompiled header directive。

      原因:尋找預(yù)編譯頭文件路徑時遇到了不該遇到的文件尾。(一般是沒有#include “stdafx.h”)

      12、fatal error C1083: Cannot open include file: 'R??.h': No such file or directory

      原因:不能打開包含文件“R??.h”:沒有這樣的文件或目錄。

      13、error C2011: 'C??': 'class' type redefinition

      原因:類“C??”重定義。

      14、error C2018: unknown character '0xa3'

      原因:不認(rèn)識的字符'0xa3'。(一般是漢字或中文標(biāo)點符號)

      15、error C2057: expected constant expression

      原因:希望是常量表達(dá)式。(一般出現(xiàn)在switch語句的case分支中)

      16、error C2065: 'IDD_MYDIALOG' : undeclared identifier

      原因:“IDD_MYDIALOG”:未聲明過的標(biāo)識符。

      17、error C2082: redefinition of formal parameter 'bReset'

      原因:函數(shù)參數(shù)“bReset”在函數(shù)體中重定義。

      18、error C2143: syntax error: missing ':' before '{'

      原因:句法錯誤:“{”前缺少“;”。

      19、error C2146: syntax error : missing ';' before identifier 'dc'

      原因:句法錯誤:在“dc”前丟了“;”。

      20、error C2196: case value '69' already used

      原因:值69已經(jīng)用過。(一般出現(xiàn)在switch語句的case分支中)

      21、error C2509: 'OnTimer' : member function not declared in 'CHelloView'原因:成員函數(shù)“OnTimer”沒有在“CHelloView”中聲明。

      22、error C2511: 'reset': overloaded member function 'void(int)' not found in 'B'

      原因:重載的函數(shù)“void reset(int)”在類“B”中找不到。

      23、error C2555: 'B::f1': overriding virtual function differs from 'A::f1' only by return type or calling convention

      原因:類B對類A中同名函數(shù)f1的重載僅根據(jù)返回值或調(diào)用約定上的區(qū)別。

      24、error C2660: 'SetTimer' : function does not take 2 parameters原因:“SetTimer”函數(shù)不傳遞2個參數(shù)。

      25、warning C4035: 'f??': no return value

      原因:“f??”的return語句沒有返回值。

      26、warning C4553: '= =' : operator has no effect;did you intend '='?原因:沒有效果的運(yùn)算符“= =”;是否改為“=”?

      27、warning C4700: local variable 'bReset' used without having been initialized

      原因:局部變量“bReset”沒有初始化就使用。

      28、error C4716: 'CMyApp::InitInstance' : must return a value原因:“CMyApp::InitInstance”函數(shù)必須返回一個值。

      29、LINK : fatal error LNK1168: cannot open Debug/P1.exe for writing

      原因:連接錯誤:不能打開P1.exe文件,以改寫內(nèi)容。(一般是P1.Exe還在運(yùn)行,未關(guān)閉)

      30、error LNK2001: unresolved external symbol “public: virtual _ _thiscall C??::~C??(void)”

      原因:連接時發(fā)現(xiàn)沒有實現(xiàn)的外部符號(變量、函數(shù)等)。

      第二篇:C++上機(jī)實驗報告

      第二次上機(jī)實驗報告

      姓名:王小寧

      班級:

      學(xué)號:

      031012 1234

      第一題:

      題目:

      編寫一個類,聲明一個數(shù)據(jù)成員和一個靜態(tài)數(shù)據(jù)成員,其構(gòu)造函數(shù)初始化數(shù)據(jù)成員,并把靜態(tài)數(shù)據(jù)成員加1,其析構(gòu)函數(shù)把靜態(tài)數(shù)據(jù)成員減1.(1)編寫一個應(yīng)用程序,創(chuàng)建該類的3個對象,然后顯示其數(shù)據(jù)成員和靜態(tài)數(shù)據(jù)成員,再析構(gòu)每個對象,并顯示它們對靜態(tài)數(shù)據(jù)成員的影響。

      (2)修改該類,增加靜態(tài)成員函數(shù)并訪問靜態(tài)數(shù)據(jù)成員,并聲明靜態(tài)數(shù)據(jù)成員為保護(hù)成員。體會靜態(tài)成員函數(shù)的使用,靜態(tài)成員之間與非靜態(tài)成員之間互訪問題。

      編程思想:

      首先,定義一個類,其中含有兩個類的私有變量,一個靜態(tài)數(shù)據(jù)變量,定義構(gòu)造函數(shù),將初值賦給兩個私有變量,并將靜態(tài)數(shù)據(jù)變量加1,并顯示.定義一個析構(gòu)函數(shù),并通過析構(gòu)函數(shù)將靜態(tài)成員變量減1.并顯示。

      修改以上的類,增加一個靜態(tài)成員函數(shù)并通過靜態(tài)成員函數(shù)來訪問靜態(tài)成員變量。在主函數(shù)中利用一個指向函數(shù)的指針指向這個靜態(tài)成員函數(shù),并通過這個指針來訪問類中的靜態(tài)數(shù)據(jù)。代碼實現(xiàn):

      代碼1:

      #include using namespace std;class A { public:

      static int count;

      A(int a=0,int b=0)

      {

      X=a;

      Y=b;

      count++;

      cout<<“startcount=”<

      count--;

      cout<<“overcount=”<

      int GetX(){return X;}

      int GetY(){return Y;}

      private:

      int X,Y;};int A::count=0;int main(){ int *countp=&A::count;A z(2,3);cout<<“x=”<

      cout<<“x=”<

      問題及心得:

      在這次試驗中,我理解了靜態(tài)變量與普通變量之間的差異與聯(lián)系。在實驗過程中因未初靜態(tài)變量始化而無法通過編譯,并且注意到靜態(tài)變量一定要在類外初始化。

      題目2:

      創(chuàng)建一個Person類,該類中有字符數(shù)組,表示姓名、街道地址、市、省和郵政編碼。其功能有修改姓名、顯示數(shù)據(jù)信息。要求其功能函數(shù)的原型放在類定義中,構(gòu)造函數(shù)初始化每個成員,顯示信息函數(shù)要求把對象中的完整信息打印出來。其中數(shù)據(jù)成員為保護(hù)的,函數(shù)為公有的。

      編程思想:

      創(chuàng)建一個PERSON類,定義姓名、街道地址、市、省和郵政編碼分別為CHAR型的指針?biāo)接行妥兞俊T诙x公有型的構(gòu)造函數(shù),并在構(gòu)造函數(shù)中申請動態(tài)內(nèi)存來保存初始化的內(nèi)容,并用相應(yīng)的私有性的指針變量指向,再利用復(fù)制函數(shù)則指針中將會存放入輸入內(nèi)容。定義公有的析構(gòu)函數(shù)釋放動態(tài)申請的空間。定義一個公有的改變函數(shù)改變其中一個變量,方法與構(gòu)造函數(shù)相似。

      代碼實現(xiàn):

      #include using namespace std;class Person {

      private:

      char *name;char *street;char *pro;char *city;char *code;

      public: Person(char *aname,char *astreet,char *apro,char *acity,char *acode){

      name=new char[strlen(aname)+1];

      strcpy(name,aname);

      street=new char[strlen(astreet)+1];

      strcpy(street,astreet);

      pro=new char[strlen(apro)+1];

      strcpy(pro,apro);

      city=new char[strlen(acity)+1];

      strcpy(city,acity);

      code=new char[strlen(acode)+1];

      strcpy(code,acode);

      cout<<“constructor”<

      delete[] name;

      delete[] street;

      delete[] pro;

      delete[] city;

      delete[] code;

      cout<<“destructor”<

      delete[] name;

      name=new char[strlen(aname)+1];

      strcpy(name,aname);} void show(){

      cout<<“姓名:”<

      cout<<“街道地址:”<

      cout<<“省份:”<

      cout<<“城市:”<

      cout<<“郵政編碼:”<

      運(yùn)行結(jié)果:

      實驗心得: 通過這個實驗,我們學(xué)會了對類的私有的字符數(shù)組變量的初始化。利用指針動態(tài)分配空間。

      第三篇:C++上機(jī)實驗報告

      C++上機(jī)實驗報告

      實驗名稱:實驗

      專業(yè)班級:

      名:

      學(xué)

      號:

      實驗日期: 11 實驗

      目錄

      1.實驗?zāi)康?/p>

      2.實驗內(nèi)容

      3.程序代碼

      4.調(diào)試結(jié)果

      5.實驗心得 1.實驗?zāi)康?實驗10(1)進(jìn)一步了解運(yùn)算符重載的概念和使用方法;(2)掌握幾種常用的運(yùn)算符重載的方法;(3)了解轉(zhuǎn)換構(gòu)造函數(shù)的使用方法;

      (4)了解在Visual C++6.0環(huán)境下進(jìn)行運(yùn)算符重載要注意的問題。實驗11(1)了解繼承在面向?qū)ο蟪绦蛟O(shè)計中的重要作用;(2)進(jìn)一步理解繼承和派生的概念;

      (3)掌握通過繼承派生出一個新的類的方法;(4)了解虛基類的作用和用法。

      2.實驗內(nèi)容 實驗10 事先編好程序,上機(jī)進(jìn)行調(diào)試和運(yùn)行程序,分析結(jié)果。(1)聲明一個復(fù)數(shù)類Complex,重載運(yùn)算符“+”,“-”,“*”,“/”,使之能用于復(fù)數(shù)的加,減,乘,除,運(yùn)算符重載函數(shù)作為Complex類成員的函數(shù)。編程序,分別求兩個復(fù)數(shù)之和,差,積和商。(2)聲明一個復(fù)數(shù)類Complex,重載運(yùn)算符“+”,使之能用于復(fù)數(shù)的加法運(yùn)算。參加運(yùn)算的兩個運(yùn)算量可以都是類對象,也可以其中有一個是整數(shù),順序任意。

      運(yùn)行程序,分別求兩個復(fù)數(shù)之和,整數(shù)和復(fù)數(shù)之和,(3)有兩個矩陣a和b,均為2行3列。求兩個矩陣之和。重載運(yùn)算符“+”,使之能用于兩個矩陣相加

      (4)聲明一個Teacher(教師)類和一個Student(學(xué)生)類,二者有一部分?jǐn)?shù)據(jù)成員是相同的,例如num(號碼),name(名字),sex(性別)。編寫程序,將一個Student對象(學(xué)生)轉(zhuǎn)換為Teacher(教師)類,只將以上三個相同的數(shù)據(jù)成員移植過去??梢栽O(shè)想為:一位學(xué)生大學(xué)畢業(yè)了,留校擔(dān)任教師,他原有的部分?jǐn)?shù)據(jù)對現(xiàn)在的教師身份來說任然是有用的,應(yīng)當(dāng)保留并成為其教師的數(shù)據(jù)的一部分。

      實驗11

      事先編寫好程序,上機(jī)調(diào)試和運(yùn)行程序,分析結(jié)果。

      (1)將教材第11章例11.1的程序片段補(bǔ)充和改寫成一個完整的、正確的程序,用公用繼承方式。在程序中應(yīng)當(dāng)包括輸入數(shù)據(jù)的函數(shù),在程序運(yùn)行時輸入num,name,sex,age,addr的值,程序應(yīng)輸出以上5個數(shù)據(jù)的值。

      (2)將教材第11章例11.3的程序修改、補(bǔ)充,寫成一個完整、正確的程序,用保護(hù)繼承方式。在程序中應(yīng)包括輸入數(shù)據(jù)的函數(shù)。(3)修改上面第(2)題的程序,改為用公用繼承方式。上機(jī)調(diào)試程序,使之能夠正確運(yùn)行并得到正確的結(jié)果。

      對這兩種繼承方式做比較分析,考慮在什么情況下二者不能互相替換。

      (4)分別聲明Teacher(教師)類和Cadre(干部)類,采用多 重繼承方式由這兩個類派生出新類Teacher-Cadre(教師兼干部)。要求:

      Ⅰ.在兩個基類中都包含姓名、年齡、性別、地址、電話等數(shù)據(jù)成員。

      Ⅱ.在Teacher類中還包含數(shù)據(jù)成員title(職稱),在Cadre類中還包含數(shù)據(jù)成員post(職務(wù))。在Teacher-Cadre類中還包含數(shù)據(jù)成員wages(工資)。

      Ⅲ.在基類中的姓名、年齡、性別、地址、電話等數(shù)據(jù)成員用相同的名字,在引用這些數(shù)據(jù)成員時,指定作用域。Ⅴ.在類體中聲明成員函數(shù),在類外定義成員函數(shù)。

      Ⅵ.在派生類Teacher-Cadre的成員函數(shù)show中調(diào)用Teacher類中的display函數(shù),輸出性命、年齡、性別、職稱、地址、電話,然后再用cout語句輸出職務(wù)與工資。

      3.程序代碼 實驗10(1)

      #include using namespace std;class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator +(Complex &c2);Complex operator-(Complex &c2);Complex operator*(Complex &c2);Complex operator/(Complex &c2);void display();private: double real;double imag;};

      Complex Complex::operator +(Complex &c2){Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}

      Complex Complex::operator-(Complex &c2){Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;}

      Complex Complex::operator*(Complex &c2){Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;return c;}

      Complex Complex::operator/(Complex &c2){Complex c;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);return c;} void Complex::display(){cout<<“(”<

      (2)

      #include using namespace std;class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2);Complex operator+(int &i);friend Complex operator+(int &,Complex &);void display();private:

      double real;

      double imag;};

      Complex Complex::operator+(Complex &c){return Complex(real+c.real,imag+c.imag);}

      Complex Complex::operator+(int &i){return Complex(real+i,imag);}

      void Complex::display(){cout<<“(”<

      Complex operator+(int &i,Complex &c){return Complex(i+c.real,c.imag);}

      int main(){Complex c1(3,4),c2(5,-10),c3;int i=5;c3=c1+c2;cout<<“c1+c2=”;c3.display();c3=i+c1;cout<<“i+c1=”;c3.display();c3=c1+i;cout<<“c1+i=”;c3.display();return 0;}(3)

      #include using namespace std;class Matrix {public: Matrix();friend Matrix operator+(Matrix &,Matrix &);void input();void display();private:

      int mat[2][3];};

      Matrix::Matrix(){for(int i=0;i<2;i++)for(int j=0;j<3;j++)mat[i][j]=0;}

      Matrix operator+(Matrix &a,Matrix &b){Matrix c;for(int i=0;i<2;i++)for(int j=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];} return c;}

      void Matrix::input(){cout<<“input value of matrix:”<>mat[i][j];}

      void Matrix::display(){for(int i=0;i<2;i++){for(int j=0;j<3;j++){cout<

      #include using namespace std;class Student {public: Student(int,char[],char,float);int get_num(){return num;} char * get_name(){return name;} char get_sex(){return sex;} void display(){cout<<“num:”<

      int num;

      char name[20];

      char sex;

      float score;};

      Student::Student(int n,char nam[],char s,float sco){num=n;strcpy(name,nam);sex=s;score=sco;}

      class Teacher {public: Teacher(){} Teacher(Student&);Teacher(int n,char nam[],char sex,float pay);void display();private:

      int num;

      char name[20];

      char sex;

      float pay;};

      Teacher::Teacher(int n,char nam[],char s,float p} {num=n;strcpy(name,nam);sex=s;pay=p;}

      Teaxher::Teacher(Student& stud){num=stud.get_num();strcpy(name,stud.get_name());sex=stud.get.sex();pay=1500;}

      void Teacher::display(){cout<<“num:”<int main(){Teacher teacher1(10001,”Li“,'f',1234.5),teacher2;Student student1(20010,”Wang“,'m',89.5);cout<<”student1:“<

      #include using namespace std;class Student {public:

      void get_value()

      {cin>>num>>name>>sex;}

      void display()

      {cout<<”num:“<

      cout<<”sex:“<

      int num;

      char name[10];char sex;};

      class Student1:public Student {public: void get_value_1(){get_value();cin>>age>>addr;} void display_1(){cout<<”age:“<

      char addr[30];};

      int main(){Student1 stud1;stud1.get_value_1();stud1.display();stud1.display_1();return 0;}(2)

      #include using namespace std;class Student {public: void get_value();

      void display();protected: int num;

      char name[10];

      char sex;};

      void Student::get_value(){cin>>num>>name>>sex;} void Student::display(){cout<<”num:“<

      class Student1:protected Student {public: void get_value_1();void display1();private: int age;char addr[30];};

      void Student1::get_value_1(){get_value();cin>>age>>addr;} void Student1::display1(){cout<<”num:“<

      int main(){Student1 stud1;stud1.get_value_1();stud1.display1();return 0;}(3)

      #include using namespace std;class Student {public: void get_value();void display();protected:

      int num;

      char name[10];

      char sex;};

      void Student::get_value(){cin>>num>>name>>sex;}

      void Student::display(){cout<<”num:“<

      class Student1:public Student {public: void get_value_1();void display1();private:

      int age;

      char addr[30];};

      void Student1::get_value_1(){get_value();cin>>age>>addr;} void Student1::display1(){cout<<”num:“<

      int main(){Student1 stud1;stud1.get_value_1();stud1.display1();return 0;}(4)

      #include #include using namespace std;class Teacher {public:

      Teacher(int,char[],char);void display();private: int num;char name[20];char sex;};

      Teacher::Teacher(int n,char nam[],char s){num=n;strcpy(name,nam);sex=s;}

      void Teacher::display(){cout<<”num:”<

      class BirthDate {public: BirthDate(int,int,int);void display();void change(int,int,int);private: int year;int month;int day;};

      BirthDate::BirthDate(int y,int m,int d){year=y;month=m;day=d;}

      Void BithDate::display(){cout<<”birthday:”<

      void BirthDate::change(int y,int m,int d){year=y;month=m;day=d;}

      class Professor:public Teacher {public: Professor(int,char[],char,int,int,int,float);void display();void change(int,int,int);private: float area;BirthDate birthday;}

      Professor::Professor(int n,char name[20],char s,int y,int m,int d,float a): Teacher(n,name,s),birthday(y,m,d),area(a){}

      void Professor::display(){Teacher::display();birthday.display();cout<<”area:”<

      Int main(){Professor profl(3012,”Zhang”,’f’,1949,10,1,125.4);cout<

      4.調(diào)試結(jié)果

      實驗10(1)c1+c2=(8,-6i)c1-c2=(-2,14i)c1*c2=(55,-10i)c1/c2=(-0.2,0.4)(2)c1+c2=(8,-6i)i+c1=(8,4i)c1+i=(8,4i)

      (3)

      input value of Matrix:11 22 33 44 55 66 input value of Matrix:12 13 14 15 16 17 Matrix a: 11 22 33 44 55 66 Matrix b: 12 13 14 15 16 17 Matrix c=Matrix a + Matrix b : 23 25 47 59 71 83

      (4)student1 : num :20010 name:Wang sex:m score;89.5 Teacher2: num:20010 name:Wang sex:m pay:1500

      實驗11(1)

      10101 Li M 20 Beijing num:10101 name:Li sex:M age:20 address:Beijing

      (2)

      10101 Li M 20 Beijing num:10101 name:Li sex:M age:20 address:Beijing

      (3)

      10101 Li M 20 Beijing num:10101 name:Li sex:M age:20 address:Beijing

      (4)The original data: num:3012 name:Zhang sex:f area:125.4

      The new data: num:3012 name:Zhang sex:f birthday:6/1/1950 area:125.4 5.實驗心得

      這一次上機(jī)實驗,除了了解到了運(yùn)算符重載的概念和用法,掌握幾種常用的運(yùn)算符重載的方法,了解轉(zhuǎn)換構(gòu)造函數(shù)的使用方法,同時也能了解虛基類的用法,理解繼承與派生的概念。

      但是,最主要的,我覺得,是通過這一次的上機(jī)實驗,我了解到,有的實驗本身是沒有程序錯誤的,但是,也會由于實驗環(huán)境的影響而不能正常運(yùn)行。換句話說,有的程序并不能在Visaul C++的環(huán)境下運(yùn)行,而不是程序自身的問題。所以,對于沒辦法調(diào)試成功的程序,我們也不能一味的認(rèn)為程序有錯誤,要學(xué)會理性的判斷程序的對錯,再下結(jié)論。

      第四篇:C++上機(jī)實驗:名稱空間和頭文件

      實驗6 名稱空間和頭文件

      1.實驗?zāi)康?/p>

      學(xué)習(xí)并理解變量的作用域;掌握頭文件的使用方法;掌握名稱空間的作用和使用方法。

      2.實驗要求

      (1)掌握全局變量和靜態(tài)變量的作用域。

      (2)掌握頭文件的使用方法。

      (3)掌握名稱空間的使用方法。

      3.實驗內(nèi)容

      (1)全局變量一般在函數(shù)的外部定義,運(yùn)行下面程序并思考變量的作用域。

      #include using namespace std;

      int a=3;//全局變量

      int main(){

      int a=5;cout<

      (2)靜態(tài)變量(static).靜態(tài)變量之所以是因為靜態(tài),是因為他在整個程序生命周期的地址靜止不變。也就是說在整個程序里面只保存有一份拷貝。運(yùn)行下面兩個程序并思考靜態(tài)變量的規(guī)則。

      #include using namespace std;

      int max_so_far(int curr)//求至今(本次調(diào)用)為止最大值 {

      static int biggest=0;//該變量保持著每次調(diào)用時的最新值,它的有效期等于整個程序的有效期,如果去掉static,同學(xué)們看看程序運(yùn)行的結(jié)果是什么?

      cout<

      if(curr > biggest)

      biggest = curr;

      return biggest;}

      int main()

      {

      cout<

      return 0;

      }

      #include using namespace std;

      void fun1()

      {

      static int value = 1;//體會靜態(tài)變量的作用:函數(shù)調(diào)用結(jié)束后,其所占用的地址依然存在

      value=value+1;

      cout<

      }

      int main()

      {

      fun1();

      fun1();

      fun1();

      return 0;

      }

      (3)為了實現(xiàn)協(xié)同開發(fā),減少開發(fā)時間,降低錯誤,提高效率,C++提供了頭文件和名稱空間機(jī)制。一般函數(shù)、全局變量、類、名稱等的聲明放在擴(kuò)展名為.h(稱為接口interface文件)的頭文件中,而其實現(xiàn)部分則放在相同主名,擴(kuò)展名為.cpp(稱為實現(xiàn)implementation文件),而用戶的應(yīng)用程序則是調(diào)用(稱為應(yīng)用application文件)

      //E:cfun.h

      頭文件的定義 #ifndef FUN_H #define FUN_H

      #include using namespace std;int f(int);#endif

      //E:cfun.cpp

      實現(xiàn)部分的定義 #include

      int f(int x){ } #include

      //E:ccpp1.cpp

      應(yīng)用程序的定義 int main(){

      } cout<

      (4)名稱空間主要是為了解決重名的問題

      #ifndef FUN_H #define FUN_H

      #include using namespace std;

      namespace n1{

      namespace n2{

      #endif #include

      //extern int n1::a=1;//extern int n2::a=2;int f(int);} int f(int);} int n1::f(int x){ }

      int n2::f(int x){ }

      #include int main(){

      } cout<

      (1)利用頭文件的方式,寫出實現(xiàn)數(shù)學(xué)運(yùn)算(+,-,*,/,%,^)的函數(shù)庫(mathsx),然后在主程序中調(diào)用,體會頭文件的作用

      (2)利用名稱空間的方法,分別在兩個名稱空間中實現(xiàn)交換兩個變量的值的函數(shù),分別用指針和引用作為參數(shù)swap(int *, int *)以及swap(int & ,int &)然后在主程序中調(diào)用,體會函數(shù)的闡述傳遞的規(guī)則

      第五篇:C++上機(jī)實驗報告5(定稿)

      C++程序?qū)嶒瀳蟾?/p>

      實驗五繼承與派生 實驗?zāi)康?/p>

      1.學(xué)習(xí)定義和使用類的繼承關(guān)系,定義派生類 2.熟悉不同繼承方式下對基類成員的訪問控制 3.學(xué)習(xí)利用虛基類解決二義性問題 實驗要求

      1.定義一個基類Animal,有私有整型成員變量age,構(gòu)造其派生類dog,在其成員函數(shù)SetAge(int n)中直接給age賦值,看看會有什么問題,把a(bǔ)ge改為公有成員變量,還會有問題嗎?編程嘗試。

      2.定義一個基類BaseClass,有整型變量Number,構(gòu)造其派生類DerivedClass,觀察構(gòu)造函數(shù)和析構(gòu)函數(shù)的執(zhí)行情況。

      3.定義一個車(vehicle)基類,具有Maxspeed、Weight等成員,Run、Stop等成員函數(shù),由此派生出自行車(bicycle)類、汽車(motorcar)類。自行車(bicycle)類有高度(Height)等屬性,汽車(motorcar)類有座位數(shù)(SeatNun)等屬性。從bicycle和motorcar派生出摩托車(motorbicycle)類,在繼承過程中,注意把vehicle設(shè)置為虛基類。如果不把vehicle設(shè)置為虛基類,會有什么問題?編程嘗試。實驗內(nèi)容及實驗步驟

      1.編寫程序定義基類Animal,成員變量age定義為私有的。構(gòu)造派生類dog,在其成員函數(shù)SetAge(int n)中直接對age賦值時,會出現(xiàn)類似以下的錯誤提示:

      error C2248 : ‘a(chǎn)ge’ : cannot access private member declared in class ‘Animal’

      error C2248 : ‘a(chǎn)ge’ : cannot access private member declared in class ‘Animal’ 把a(bǔ)ge改為公有成員變量后重新編譯就可以了。程序名:lab7_1.cpp。錯誤代碼:

      #include using namespace std;

      class Animal { private: int age;public: Animal(){};};

      class dog:public Animal { public: void SetAge(int n){

      age=n;};};

      int main(){ dog one;one.SetAge(23);return 0;}

      運(yùn)行結(jié)果:

      正確代碼:

      #include using namespace std;

      class Animal { public: int age;public: Animal(){};};

      class dog:public Animal { public: void SetAge(int n){

      age=n;};};

      int main(){ dog one;one.SetAge(23);return 0;}

      運(yùn)行結(jié)果:

      2.編寫程序定義一個基類BaseClass,構(gòu)造其派生類DerivedClass,在構(gòu)造函數(shù)和析構(gòu)函數(shù)中用cout輸出提示信息,觀察構(gòu)造函數(shù)和析構(gòu)函數(shù)的執(zhí)行情況。程序名:lab7_2.cpp。

      源代碼:

      #include using namespace std;class BaseClass { private: int a;public: BaseClass(){

      cout<<“基類構(gòu)造函數(shù)調(diào)用”<

      cout<<“基類析構(gòu)函數(shù)調(diào)用”<

      class DerivedClass:public BaseClass { private: int b;public: DerivedClass(){

      };cout<<“派生類構(gòu)造函數(shù)調(diào)用”<

      cout<<“派生類析構(gòu)函數(shù)調(diào)用”<

      int main(){ DerivedClass one;

      return 0;}

      運(yùn)行結(jié)果:

      3.用debug功能跟蹤程序lab7_2的執(zhí)行過程,觀察基類和派生類的構(gòu)造函數(shù)和析構(gòu)函數(shù)的執(zhí)行情況。

      DEBUG:

      4.編寫程序定義一個車(vehicle)基類,由此派生出自行車(bicycle)類、汽車(motorcar)類,注意把vehicle派生為虛基類。再從bicycle和motorcar派生出摩托車(motorcycle)類,在main()函數(shù)中測試這個類。程序名:lab7_3.cpp。

      編譯成功后,把vehicle設(shè)置為非虛基類,再編譯一次,此時系統(tǒng)報錯,無法編譯成功。這是因為若不把vehicle設(shè)置為虛基類,會出現(xiàn)二義性錯誤,程序不能成功編譯。源代碼:

      #include using namespace std;

      class Vehicle { public: Vehicle(){};int a;void set(){

      cout<<“Vehicle調(diào)用”<

      class Mortorcar:virtual public Vehicle { public: Mortorcar(){};int b;void set(){

      cout<<“Mortorcar調(diào)用”<

      class Bicycle:virtual public Vehicle { public: Bicycle(){};int c;void set(){

      cout<<“Bicycle調(diào)用”<

      class Mortorcycle:public Mortorcar,public Bicycle { public: Mortorcycle(){};int d;void set(){

      cout<<“Mortorcycle調(diào)用”<

      int main(){ Mortorcycle one;one.a =1;one.b=2;one.set();return 0;} 運(yùn)行結(jié)果:

      錯誤情況:

      思考題

      1.如何在已有的類的基礎(chǔ)上設(shè)計新的類?

      派生一個新的類。

      2.基類和派生類對象的構(gòu)造順序是怎樣的?

      先調(diào)用基類的構(gòu)造函數(shù)在調(diào)用派生類的構(gòu)造函數(shù)。3.如何利用虛基類解決二義性問題? 將共同的基類設(shè)置為虛基類,這時從不同的路徑繼承過來的同名數(shù)據(jù)成員在內(nèi)存中就只有一個副本,同一個函數(shù)名也只有一個映射。

      下載C++課程上機(jī)實驗常見錯誤匯集word格式文檔
      下載C++課程上機(jī)實驗常見錯誤匯集.doc
      將本文檔下載到自己電腦,方便修改和收藏,請勿使用迅雷等下載。
      點此處下載文檔

      文檔為doc格式


      聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn)自行上傳,本網(wǎng)站不擁有所有權(quán),未作人工編輯處理,也不承擔(dān)相關(guān)法律責(zé)任。如果您發(fā)現(xiàn)有涉嫌版權(quán)的內(nèi)容,歡迎發(fā)送郵件至:645879355@qq.com 進(jìn)行舉報,并提供相關(guān)證據(jù),工作人員會在5個工作日內(nèi)聯(lián)系你,一經(jīng)查實,本站將立刻刪除涉嫌侵權(quán)內(nèi)容。

      相關(guān)范文推薦

        C++實驗

        上機(jī)實驗: 1、 回文是指正讀,反讀均相同的字符序列,如“abba”和“abdba”均是回文,但是“good”不是回文,試用STACK類編寫該程序。 #include #include #include int IsPalind......

        c++實驗(網(wǎng)絡(luò)工程 ))

        面向?qū)ο蟪绦蛟O(shè)計實驗 Object Oriented Programming 課程編號: 學(xué) 分: 學(xué) 時:10 先修課程:計算機(jī)導(dǎo)論、C語言程序設(shè)計 適用專業(yè):計算機(jī)科學(xué)與技術(shù)、軟件工程 教 材:《C++程序設(shè)計......

        C++實驗總結(jié)報告

        C++ 實驗總結(jié)報告 研究課題:圖形編輯器一、實驗?zāi)康?1. 熟悉C++的一些重要性質(zhì),利用封裝、繼承、虛函數(shù)和多態(tài)性等特性,通過實驗學(xué)習(xí)如何對各類圖元的屬性和方法進(jìn)行合理的封裝......

        c++課程心得體會[★]

        心得體會 在這幾周的面向?qū)ο蟮恼n程設(shè)計中,通過完成本次任務(wù),我所收獲到的東西頗豐。 利用設(shè)計這次學(xué)生信息管理的機(jī)會,使我熟練地掌握了上學(xué)期所學(xué)的C++面向?qū)ο蟪绦蚓幊?,也?.....

        計算方法上機(jī)實驗

        龍格-庫塔 #include #include float function (float x,float y) { return (0-(y*y));//f(x,y)μ?±í′?ê? } int main() { float x0,x1,y0,y1,k1,k2,k3,k4,a,b,c,n,......

        上機(jī)實驗八

        實驗八 折半查找 一、 實驗?zāi)康?1、熟悉visual C++上機(jī)環(huán)境,進(jìn)一步掌握C語言的結(jié)構(gòu)特點。 2、 進(jìn)一步掌握圖的基本概念及其含義。 3、掌握查找的結(jié)構(gòu)特征,以及各種存儲結(jié)構(gòu)的......

        上機(jī)實驗四

        實驗四 串的基本操作實現(xiàn)-堆存儲結(jié)構(gòu)的實現(xiàn) 一、 實驗?zāi)康?1、熟悉visual C++上機(jī)環(huán)境,進(jìn)一步掌握C語言的結(jié)構(gòu)特點。 2、掌握串的基本操作:初始化串、判串為空、求串長等運(yùn)算......

        上機(jī)實驗要求

        上機(jī)實驗要求 一、 實驗前的準(zhǔn)備工作 在上機(jī)實驗前應(yīng)事先做好準(zhǔn)備工作,以提高上機(jī)實驗的效率: 1. 了解所用的計算機(jī)系統(tǒng)(包括C編譯系統(tǒng))的性能和使用方法。 2. 復(fù)習(xí)和掌握與本實......