1. 程式人生 > >C++上機報告

C++上機報告

 

 

面向物件程式設計

(C++ Programming)上機報告

 

 

 

 

 

 

 

 

 

 

 

學    號       

姓    名         

專業班級         通訊164

時    間         2018.5.16

 

 

 

 

 

 

 

 

 

實驗7  運算子過載

int h,m,s;

time24 t1(23,59,57);

time24 t2;

t2=t1+4;

t2.get_time(h,m,s);

cout<<"time t2 is "<<h<<":"<<m<<":"<<s<<endl;

}

2.#include <iostream>       

using namespace std;      

class CFraction      

{      

private:      

    int nume;  // 分子       

    int deno;  // 分母       

public:      

    CFraction(int nu=0,int de=1):nume(nu),deno(de){}      

    void simplify();      

    friend istream& operator >> (istream& input,  CFraction& c);  

    friend ostream& operator << (ostream& output, CFraction& c);  

    int gcd(int x,int y);    

    CFraction operator+(const CFraction &c);  //兩個分數相加,結果要化簡       

 

    CFraction operator+();  //取正一目運算       

    CFraction operator-();  //取反一目運算       

    bool operator>(const CFraction &c);      

    bool operator<(const CFraction &c);      

    bool operator==(const CFraction &c);      

    bool operator!=(const CFraction &c);      

    bool operator>=(const CFraction &c);      

    bool operator<=(const CFraction &c);  

    

};      

  

istream& operator >> (istream& input,  CFraction& c)    

{    

    char c1;      

        

    input >> c.nume  >> c1 >> c.deno;      

    return input;      

}    

ostream& operator << (ostream& output, CFraction& c)    

{    

       

    output <<c.nume<<'/'<<c.deno<<endl;   

    return output;    

}    

  

//取最大公約數        

int CFraction::gcd(int x,int y)    

{    

    int r;    

    while(y!=0)    

    {    

        r=x%y;    

        x=y;    

        y=r;    

    }    

    return x;    

}    

//化簡(使分子分母沒有公因子)        

void CFraction::simplify()      

{      

    int n=gcd(nume,deno);     

    nume=nume/n;    

    deno=deno/n;    

}       

CFraction CFraction::operator+(const CFraction &c) //兩個分數相加,結果要化簡      

{    

    CFraction  c1;    

    c1.nume=nume*c.deno+c.nume*deno;    

    c1.deno=deno*c.deno;    

    c1.simplify();    

    return c1;    

}    

    

    

 

   

CFraction CFraction::operator+()  //取正一目運算      

{      

    return *this;      

}     

    

CFraction CFraction::operator-() //取反一目運算      

{    

    CFraction  c1;    

    c1.nume=-nume;    

    c1.deno=deno;    

    return c1;    

}     

    

bool CFraction::operator>(const CFraction &c)     

{    

    int this_nume,c_nume,common_deno;    

    {    

        this_nume=nume*c.deno;    

        c_nume=c.nume*deno;    

        common_deno=deno*c.deno;    

        if(this_nume>c_nume&&common_deno>0||this_nume<c_nume&&common_deno<0)    

            return true;    

        else     

            return false;    

    }    

}    

    

bool CFraction::operator<(const CFraction &c)     

{    

    int this_nume,c_nume,common_deno;    

    {    

        this_nume=nume*c.deno;    

        c_nume=c.nume*deno;    

        common_deno=deno*c.deno;    

        if(this_nume>c_nume&&common_deno>0||this_nume<c_nume&&common_deno<0)    

            return false;    

        else     

            return true;    

    }    

}    

  

bool CFraction::operator==(const CFraction &c)      

{    

    if(*this!=c)    

        return false;    

    else     

        return true;    

}    

bool CFraction::operator!=(const CFraction &c)     

{    

    if (*this>c || *this<c)   

        return true;    

    else     

        return false;    

}    

bool CFraction::operator>=(const CFraction &c)     

{    

    if(*this<c)    

        return true;    

    else     

        return false;    

}    

bool CFraction::operator<=(const CFraction &c)    

{    

    if(*this>c)    

        return true;    

    else     

        return false;    

}    

int main()      

{      

    CFraction x,y,s;     

    cout <<"請按照格式輸入一個分數,如6/7"<<endl;  

    cin>>x;    

   cout <<"請按照格式輸入一個分數,如6/7"<<endl;    

    cin>>y;    

    cout<<"x="<<x;       

    cout<<"y="<<y;   

  

    s=x+y;      

    cout<<"x+y=";      

    cout<<s;   

  

    cout<<x;        

    if (x>y) cout<<"大於"<<endl;      

    if (x<y) cout<<"小於"<<endl;      

    if (x==y) cout<<"等於"<<endl;      

    cout<<y;   

  

  

    cout<<endl;      

    system("pause");      

    return 0;      

}

實驗體會:

  本節課在學習的過程中感覺很難,抽象的不好理解,除錯總是出現 bug,在網上找到很多原始碼運行了解,建構函式還是理解不了它的厲害,本次上機知道了:1.過載的運算子是帶有特殊名稱的函式,函式名是由關鍵字 operator 和其後要過載的運算子符號構成的

2.C++ 允許在同一作用域中的某個函式運算子指定多個定義,分別稱為函式過載運算子過載

過載宣告是指一個與之前已經在該作用域內宣告過的函式或方法具有相同名稱的宣告,但是它們的引數列表和定義(實現)不相同。

 

實驗八

1.#include<iostream.h>

class Person

{

protected:

char name[10];

int number;

public:

void input()

{ cin>>name>>number;

}

void show()

{ cout<<name<<"\t"<<number<<endl;

}

};

class Student :public Person

{

char sclass[10];

float score;

 

};

class Teacher :public Person

{

char dept[10];

char title[6];

 

};

void main()

{

Student s1;

Teacher t1;

cout<<"Please input the name and the number of a student:"<<endl;

s1.input();

s1.show();

cout<<"Please input the sclass and the scour of a student:"<<endl;

s1.input();

s1.show();

cout<<"Please input the name and the number of a teacher:"<<endl;

t1.input();

t1.show();

    cout<<"Please input the dept and the title of a teacher:"<<endl;

t1.input();

t1.show();

}

2.#include<iostream.h>

class A

{

public:

A (int i,int j)

{ a=i;

b=j;

}

void move(int x,int y)

{ a+=x;

b+=y;

}

void show()

{ cout<<"("<<a<<","<<b<<")"<<endl;

}

private:

int a,b;

};

class B : public A

{

public:

B(int i,int j,int k,int l) : A(i,j)

{ x=k;

y=l;

}

void show()

{ cout<<"("<<x<<","<<y<<")"<<endl;

}

void fun()

{ move(3,5);

}

void f1()

{ A::show();

}

private:

int x,y;

};

void main()

{

A a(1,2);

a.show();

B b(3,4,5,6);

b.fun();

b.show();

b.f1();

}

3.#include<iostream.h>

class Point

{ int x,y;

public:

Point (int xx,int yy)

{ x=xx;

y=yy;

}

void add(int xa,int ya)

{ x+=xa;

y+=ya;

}

void show()

{ cout<<"x="<<x<<","<<"y="<<y<<endl;

}

};

class Rect:private Point

{ int len,width;

 

 

public:

void add(int xa,int ya)

{int x,y;

 

x+=xa;

y+=ya;

}

Rect (int x,int y,int ll,int ww) : Point (x,y)

{

len=ll;

width=ww;

}

void showRect()

{ show();

cout<<"length="<<len<<","<<"width="<<width<<endl;

}

};

void main()

{ Rect rect(0,2,3,6);

rect.add(4,5);

rect.showRect();

}

3.分析下列程式中的訪問許可權,並回答所提的問題

#include<iostream.h>

class A

{

int i1;

public:

void f1();

private:

int j1;

};

class B : public A

{

int i2;

public:

void f2();

private:

int j2;

};

class C : public B

{

int i3;

public:

void f3();

private:

int j3;

};

 

void main()

{

A a;

B b;

C c;

}

回答下列問題:

  1. 派生類B中成員函式f2能訪問基類A中的成員函式f1,能訪問資料成員i1、不能訪問j1。
  2. 派生類B的物件b不能訪問基類A中的成員函式f1,能訪問資料成員i1、不能訪問j1。
  3. 派生類C中成員函式f3能訪問直接基類B中的成員函式f2和不能訪問資料成員j2,能問間接基類A中的成員函式f1和資料成員i1、不能訪問j1。
  4. 派生類C的物件c不能訪問直接基類B中的成員函式f2,能訪問資料成員j2,不能訪問間接基類A中的成員函式f1和資料成員j1,能訪問i1.
  5. 從對(1)~(4)問題的回答可以得出對公有繼承有什麼結論?(在公有繼承時,派生類的成員函式可以訪問基類中的公有成員和保護成員,派生類的物件僅可以訪問基類中的公有成員。)

 

  1. 思考與練習
  1. 虛基類用於某類從多個類繼承,這多個基類有共同基類時,這個最上層基類的成員會多次在最終派生類出現而產生二義性,為避免二義性,使得最終派生類中,最上層的基類成員只有一份,這時需要虛擬繼承,該最上層類就是虛基類,需要注意的是,該類在第一層派生時就要虛擬繼承才行,使用方法是在繼承方式前加上一個 virtual就可以了。

(2)任何一個類都可以派生出很多個新類,派生類也可以再派生出新類,因此,基類和派生類是相對而言的。一個基類可以是另一個基類的派生類,這樣便形成了複雜的繼承結構,出現了類的層次。一個基類派生出一個派生類,它又做另一個派生類的基類,則原來基類為該派生類的間接基類。

(3).#include <iostream.h>

#include <string>

using namespace std;

class Mammal

{

    

protected:

    int itsAge;

    int itsWeight;

public:

    Mammal();

    ~Mammal();

    int GetAge(){return itsAge;}

    void SetAge(int age) {itsAge=age;}

    int GetWeight() { return itsWeight;}

    void SetWeight(int weight) {itsWeight= weight;}

};

class Dog : public Mammal

{

protected:

    char itscolor[20];

public:

    Dog();

    void Setcolor(char *color) {strcpy(itscolor,color);}

    

    void getcolor(){cout<<"狗的顏色"<<itscolor<<endl;}

    //定義Dog類的資料成員和成員函式

};

ammal::Mammal()

{

int age1,weight1;

cout<<"請輸入動物的年齡:"<<endl;

cin>>age1;

SetAge(age1);

cout<<"請輸入動物的體重:"<<endl;

cin>>weight1;

SetWeight(weight1);

}

Mammal::~Mammal()

{

    cout<<"Destructorcalled."<<endl;

}

Dog::Dog()

{ char color[20];

    cout<<"請輸入狗的顏色:"<<endl;

cin>>color;Setcolor(color);

cout<<"狗的顏色"<<itscolor<<"體重"<<GetWeight()<<"年齡"<<GetAge()<<endl;

}

void main()

{

Dog dog1;

}

  1. 設計人員基類Person。

#include <iostream.h>

#include <string>

using namespace std;

#define n 20

////////////類的定義

class Person

{

protected:

    char name[n];

    char sex[n];

    int age;

public:

    Person();

    void setperson();

    void displayperson();

};

class Teacher :virtual public Person

{

protected:

    char job[n];

    char room[n];

    char subject[n];

public :

    Teacher();

    void setteacher();

    void displayteacher();

};

class Student:virtual public Person

{

    protected:

    char major[n];

    char banji[n];

    int leibie;

public :

    Student();

    void setstudent();

    void displaystudent();

};

class Postdoctor:public Teacher,public Student

{

public :

    Postdoctor();

    void setpostdoctor();

    void displaypostdoctor();

};

/////////////結構函式

Person::Person()

{

    setperson();

}

Teacher::Teacher()

{

    setteacher();

}

Student::Student()

{

    setstudent();

}

Postdoctor::Postdoctor()

{

}

//////////////////設定資料//////////////////

void Person::setperson()

{

    cout<<"*****"<<"姓名:";

    cin>>name;

    cout<<"*****"<<"性別:";

    cin>>sex;

    cout<<"*****"<<"年齡:";

    cin>>age;

}

void Teacher::setteacher()

{

    cout<<"*****"<<"職稱:";

    cin>>job;

    cout<<"*****"<<"教研室:";

    cin>>room;

    cout<<"*****"<<"所授課程:";

    cin>>subject;

}

void Student::setstudent()

{

    cout<<"*****"<<"專業:";

    cin>>major;

    cout<<"*****"<<"班級:";

    cin>>banji;

    cout<<"*****"<<"類別(1本科2碩士3博士):";

    cin>>leibie;

}

/////////////資料顯示///////////

void Person::displayperson()

{

    cout<<"姓名:"<<name<<"性別:"<<sex<<"年齡:"<<age;

}

void Teacher::displayteacher()

{

    displayperson();

cout<<"職稱:"<<job<<"教研室:"<<room<<"所授課程:"<<subject<<endl;

}

void Student::displaystudent()

{

    displayperson();

    cout<<"專業:"<<major<<"班級:"<<banji<<"類別:"<<leibie<<endl;

}

void Postdoctor::displaypostdoctor()

{

    displayperson();

    cout<<"職稱:"<<job<<"教研室:"<<room<<"所授課程:"<<subject<<"專業:"<<major<<"班級:"<<banji<<"類別:博士後"<<endl;

}

///////////////////

void main()

{

cout<<"您正在輸入一個老師的資訊:"<<endl;

Teacher t1;

cout<<"***************************************************************************syy割"<<endl;

cout<<"您正在輸入一個學生的資訊:"<<endl;

Student s1;

cout<<"***************************************************************************syy割"<<endl;

cout<<"您正在輸入一個博士後的資訊:"<<endl;

Postdoctor p1;

cout<<"***************************************************************************syy割"<<endl;

cout<<endl;

t1.displayteacher();

cout<<endl;

t1.displayteacher();

cout<<endl;

s1.displaystudent();

cout<<endl;

p1.displaypostdoctor();

}

實驗體會:

     C++多型意味著呼叫成員函式時,會根據呼叫函式的物件的型別來執行不同的函式;形成多型必須具備三個條件:

1、必須存在繼承關係;

2、繼承關係必須有同名虛擬函式(其中虛擬函式是在基類中使用關鍵字Virtual宣告的函式,在派生類中重新定義基類中定義的虛擬函式時,會告訴編譯器不要靜態連結到該函式);

3、存在基類型別的指標或者引用,通過該指標或引用呼叫虛擬函式;

 

實驗九

1.#include <fstream>

#include <iostream>

using namespace std;

void main()

{

char ch;

ifstream file("C:/test.txt");//讀取c盤的文字檔案

ofstream file1("C:/test1.txt");//建立文字檔案

while(file.get(ch))//讀取文字中的內容

{

cout << ch;//輸出文字內容到控制檯

file1<<ch;//寫入內容到檔案

}

file.close();   //關閉檔案流

file1.close();

cout<<endl;

}

#include<iostream>

#include<fstream>

using namespace std;

int main()

{ int i,j,temp,r[18];

 const int n=18;

  ifstream file_in("D:\\source.txt",ios::in);

  if(file_in.fail())

  { cerr<<"檔案 source.txt 開啟失敗!"<<endl;

  return 1;

  }

 for(i=0;i<n;i++)

{  

  file_in>>r[i];

 }

file_in.close();

for(i=0;i<n;i++)

    for(j=i+1;j<n;j++)

    {

        if(r[i]>r[j])

        {

           temp=r[j];

           r[j]=r[i];

           r[i]=temp;

        }

    }

ofstream file_out("D:\\target.txt",ios::out);

if(file_out.fail())

{ cerr<<"檔案 target.txt 開啟錯誤!"<<endl;

return 1;

}

for(i=0;i<n;i++)

{

  file_out<<r[i]<<" ";

}

  file_out.close();

  return 0;

}

實驗體會:

上機實驗深刻的感受到了c++的厲害同時也感覺到學C++的困難,要在 C++ 中進行檔案處理,必須在 C++ 原始碼檔案中包含標頭檔案 <iostream> 和 <fstream>,在從檔案讀取資訊或者向檔案寫入資訊之前,必須先開啟檔案。ofstream 和 fstream 物件都可以用來開啟檔案進行寫操作,如果只需要開啟檔案進行讀操作,則使用 ifstream 物件。

 

 

 

指導教師評語:

 

 

 

 

 

 

 

 

 

 

 

 

                                                成績評定: