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 对象。

 

 

 

指导教师评语:

 

 

 

 

 

 

 

 

 

 

 

 

                                                成绩评定:

 

猜你喜欢

转载自blog.csdn.net/CSDNwbdream/article/details/82192225
今日推荐