C/C++教程

c++继承与派生,多态

本文主要是介绍c++继承与派生,多态,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

/*继承与派生1
请以点类Point为基类派生出一个圆类Circle。
圆类Circle的数据成员为r(私有属性,存储圆的半径,圆心的点坐标通过继承点类Point加以实现),
成员函数有构造函数Circle、计算圆的面积函数Area、计算圆的周长函数Perimeter和输出函数Display,
其中构造函数实现基类和圆类的数据成员的初始化,
Display函数实现圆心坐标(利用基类Point的Display实现)、圆的半径、圆的面积(利用Area函数实现)和圆的周长(利用Perimeter函数实现)的输出。
请编写圆类的定义及成员函数实现,并在主函数中定义圆类对象,验证各个函数的正确性。
说明:圆周率PI的取值为3.14
已知Point类的定义及main代码如下:(不允许改动)

#include<iostream>
using namespace std;
const double Pi = 3.14;
class Point
{
public:
    Point(double xx, double yy);
    void Display();
private:
    double x, y;
};
Point::Point(double xx, double yy)
{
    x = xx;
    y = yy;
}
void Point::Display()
{
    cout << "Center:Point(" << x << "," << y << ")" << endl;
}
class Circle:public Point
{
public:
    Circle(double xx,double yy, double r):Point(xx,yy)
    {
        c_r = r;
    }
    double Area()
    {
        return c_r * c_r * Pi;
    }
    double Perimeter()
    {
        return c_r * Pi * 2;
    }
    void Display()
    {
        Point::Display();
        cout << "Radius:"<<c_r << endl;
        cout << "Area:"<< Area() << endl;
        cout << "Perimeter:"<< Perimeter() << endl;
    }
private:
    double c_r;
};
int main()
{
    double x, y, r;
    cin >> x >> y >> r;
    Circle C(x, y, r);
    C.Display();
    return 0;
}
/*继承与派生2
1.Person类派生大学生CollegeStu类(1)。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,
编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;
由Person类派生出大学生类CollegeStu,其属性有专业major(指针类型),C++程序设计课程成绩score(double型),
编写构造函数(实现数据初始化)、输出函数Display(包括name,id,major,score)。main的代码如下:(不允许改动)

#include<iostream>
#include<cstring>
using namespace std;
class Person
{
public:
    Person(char *name,int id)
    {
        int a = strlen(name);
        m_name = new char[a+2];
       int i;
        for (i = 0; i < a; i++)
        {
            *(m_name+i) = *(name+i);
        }
        *(m_name + i + 1) = '\0';
        m_id = id;
    }
    void Display()
    {
        cout << "Name:" << m_name << endl;
        cout << "ID:" << m_id << endl;
    }
    ~Person()
    {
        if (m_name != NULL)
        {
            delete[] m_name;
            m_name = NULL;
        }
    }
private:
    char* m_name;
    int m_id;
};
class CollegeStu :public Person
{
public:
    CollegeStu(char *name,int id,char *major,double score):Person(name,id)
    {
        int a = strlen(major);
         m_major = new char[a+2];
        int i;
        for (i = 0;i < a; i++)
        {
            *(m_major+i) = *(major+i);
        }
        *(m_major + i+1 ) = '\0';
        m_score = score;
    }
    void Display()
    {
        Person::Display();
        cout << "Major:"<< m_major<<endl;
        cout << "C++ Score:"<< m_score<<endl;
    }
    ~CollegeStu()
    {
        if (m_major != NULL)
        {
            delete[] m_major;
            m_major = NULL;
        }
    }
private:
    char* m_major;
    double m_score;
};
int main()
{
    char name[81], major[81];
    int id;
    double score;
    cin >> name >> id >> major >> score;
    CollegeStu cs(name, id, major, score);
    cs.Display();
    return 0;
}
/*继承与派生3
 Person类派生大学生CollegeStu类(2)。
 设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,
 编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;
由Person类派生出大学生类CollegeStu,其属性有专业major(指针类型),C++程序设计课程成绩score(double型),
编写构造函数(实现数据初始化)、输出函数Display(只输出major,score)。main的代码如下:(不允许改动)

#include<iostream>
#include<cstring>
using namespace std;
class Person
{
public:
    Person(char *name,int id)
    {
        int a = strlen(name);
        m_name = new char[a+1];
        for (int i = 0; i < a; i++)
        {
            *(m_name+i) = *(name+i);
        }
        m_id = id;
    }
    void Display()
    {
        cout << "Name:" << m_name << endl;
        cout << "ID:" << m_id << endl;
    }
    ~Person()
    {
        if (m_name != NULL)
        {
            delete[] m_name;
            m_name = NULL;
        }
    }
private:
    char* m_name;
    int m_id;
};
class CollegeStu :public Person
{
public:
    CollegeStu(char *name,int id,char *major,double score):Person(name,id)
    {
        int a = strlen(major);
        m_major = new char[a+1];
        for (int i = 0;i < a; i++)
        {
            *(m_major+i) = *(major+i);
        }
        m_score = score;
    }
    void Display()
    {
        cout << "Major:"<< m_major<<endl;
        cout << "C++ Score:"<< m_score<<endl;
    }
    ~CollegeStu()
    {
        if (m_major != NULL)
        {
            delete[] m_major;
            m_major = NULL;
        }
    }
private:
    char* m_major;
    double m_score;
};
int main()
{
    char name[81], major[81];
    int id;
    double score;
    cin >> name >> id >> major >> score;
    CollegeStu cs(name, id, major, score);
    cs.Person::Display();
    cs.Display();
    return 0;
}
/*继承与派生3
 Person类派生大学生CollegeStu类(2)。
 设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,
 编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;
由Person类派生出大学生类CollegeStu,其属性有专业major(指针类型),C++程序设计课程成绩score(double型),
编写构造函数(实现数据初始化)、输出函数Display(只输出major,score)。main的代码如下:(不允许改动)

#include<iostream>
#include<cstring>
using namespace std;
class Person
{
public:
    Person(char *name,int id)
    {
        int a = strlen(name);
        m_name = new char[a+2];
        int i;
        for (i = 0; i < a; i++)
        {
            *(m_name+i) = *(name+i);
        }
        *(m_name + i + 1) = '\0';
        m_id = id;
    }
    void Display()
    {
        cout << "Name:" << m_name << endl;
        cout << "ID:" << m_id << endl;
    }
    ~Person()
    {
        if (m_name != NULL)
        {
            delete[] m_name;
            m_name = NULL;
        }
    }
private:
    char* m_name;
    int m_id;
};
class CollegeStu :public Person
{
public:
    CollegeStu(char *name,int id,char *major,double score):Person(name,id)
    {
        int a = strlen(major);
        m_major = new char[a+2];
        int i;
        for (i = 0;i < a; i++)
        {
            *(m_major+i) = *(major+i);
        }
        *(m_major + i+1 ) = '\0';
        m_score = score;
    }
    void Display()
    {
        cout << "Major:"<< m_major<<endl;
        cout << "C++ Score:"<< m_score<<endl;
    }
    ~CollegeStu()
    {
        if (m_major != NULL)
        {
            delete[] m_major;
            m_major = NULL;
        }
    }
private:
    char* m_major;
    double m_score;
};
int main()
{
    char name[81], major[81];
    int id;
    double score;
    cin >> name >> id >> major >> score;
    CollegeStu cs(name, id, major, score);
    cs.Person::Display();
    cs.Display();
    return 0;
}
/*继承与派生4
已知Base为基类,派生出Derived类,两个类的定义及main的代码如下(不允许改动),
请完成Base类和Derived类的构造函数和析构函数,能够根据输入获取相应的输出。

#include<iostream>
using namespace std;
class Base
{
private:
    int b;
public:
    Base(int);
    ~Base();
};
Base::Base(int a_b)
{
    b = a_b;
    cout << "Base" << " " << b << " " << "says" << " " << "hello" << endl;
}
Base::~Base()
{
    cout << "Base" << " " << b << " " << "says" << " " << "goodbye" << endl;
}
class Derived :public Base
{
private:
    int d;
public:
    Derived(int, int);
    ~Derived();
};
Derived::Derived(int a_b, int a_d):Base(a_b)
{
    d = a_d;
    cout << "Derived" << " " << d << " " << "says" << " " << "hi" << endl;
}
Derived::~Derived()
{
    cout << "Derived" << " " << d << " " << "says" << " " << "bye" << endl;
}
int main()
{
    int a, b;
    cin >> a >> b;
    Derived dr(a, b);
    return 0;
}
/*继承与派生5
由Array类派生出有序数组SortArray类,SortArray类中实现有序数组的插入。
已知Array类的定义如下(不允许增加成员函数):

#include<iostream>
#include<math.h>
using namespace std;
const int MaxSize = 100;
class Array
{
public:
    Array();
    int Length();
    double Get(int pos);
    void Insert(int pos, double x);
    void Display();
private:
    double data[MaxSize];
    int length;
};
Array::Array()
{
    for (int i = 0; i < 100; i++)
    {
        data[i] = 0;
    }
    length = 0;
}
int Array::Length()
{
    return length + 1;
}
void Array::Insert(int pos, double x)
{
    length = pos;
    data[pos] = x;
}
double Array::Get(int pos)
{
    return data[pos];
}
void Array::Display()
{
    cout << "The length:" << Length() << endl;
}
class SortArray :private Array
{
public:
    SortArray();
    int Length();
    double Get(int pos);
    void Display();
    void Insert(double x);
};
SortArray::SortArray()
{

}
int SortArray::Length()
{
    return Array::Length();
}
double SortArray::Get(int pos)
{
    double data2[MaxSize];
    for (int i = 0; i < Length(); i++)
    {
        data2[i] = Array::Get(i);
    }
    for (int i = 0; i < Length() - 1; i++)
    {
        for (int j = 0; j < Length() - 1 - i; j++)
        {
            double temp;
            if (data2[j] > data2[j + 1])
            {
                temp = data2[j];
                data2[j] = data2[j + 1];
                data2[j + 1] = temp;
            }
        }
    }
    return data2[pos];
}
void SortArray::Display()
{
    Array::Display();
    cout << "The elements:";
    for (int i = 0; i < Length(); i++)
    {
        cout << Get(i) << " ";
    }
}
void SortArray::Insert(double x)
{
    for (int i = 0; i < 100; i++)
    {
        if (Array::Get(i) == 0)
        {
            Array::Insert(i, x);
            break;
        }
    }
}
int main()
{
    SortArray sa;
    double num;
    while (1)
    {
        cin >> num;
        if (fabs(num) <= 1e-6) break;
        try
        {
            sa.Insert(num); //
        }
        catch (char* message)
        {
            cout << message << endl;
        }
    }
    sa.Display();
    return 0;
}

继承与派生6
Description

已知Array类的定义如下(不允许增加成员函数):
class Array
{
public:
       Array(int size); 
 //构造函数,初始化数据成员(为data分配内存,MaxSize置为size,length置为0)
       int Length();   //获取顺序表实际长度
       double Get(int pos);     //获取下标为pos的元素的值
       void Insert(int pos, double x); //在下标pos处插入x
       void Display();       //输出线性表         
private:
       double *data; //存储元素


#include<iostream>
#include<math.h>
using namespace std;
class Array
{
public:
    Array(int size);
    int Length();
    double Get(int pos);
    void Insert(int pos, double x);
    void Display();
    ~Array();
private:
    double* data;
    int MaxSize;
    int length;
};
Array::Array(int size)
{
    MaxSize = size;
    data = new double[MaxSize];
    for (int i = 0; i < size; i++)
    {
        *(data+i) = 0;
    }
    length = 0;
}
int Array::Length()
{
    return length + 1;
}
double Array::Get(int pos)
{
    return *(data+pos);
}
void Array::Insert(int pos, double x)
{
    length = pos;
    *(data+pos) = x;
}
void Array::Display()
{
    cout << "The length:" << Length() << endl;
}
Array::~Array()
{
    if (data != NULL)
    {
        delete[] data;
        data = NULL;
    }
}
class SortArray :private Array
{
public:
    SortArray(int size);
    int Length();
    double Get(int pos);
    void Display();  
    void Insert(double x);
};
SortArray::SortArray(int size):Array(size)
{

}
int SortArray::Length()
{
    return Array::Length();
}
double SortArray::Get(int pos)
{
    double data2[100];
    for (int i = 0; i < Length(); i++)
    {
        data2[i] = Array::Get(i);
    }
    for (int i = 0; i < Length() - 1; i++)
    {
        for (int j = 0; j < Length() - 1 - i; j++)
        {
            double temp;
            if (data2[j] > data2[j + 1])
            {
                temp = data2[j];
                data2[j] = data2[j + 1];
                data2[j + 1] = temp;
            }
        }
    }
    return data2[pos];
}
void SortArray::Display()
{
    Array::Display();
    cout << "The elements:";
    for (int i = 0; i < Length(); i++)
    {
        cout << Get(i) << " ";
    }
}
void SortArray::Insert(double x)
{
    for (int i = 0; i < 100; i++)
    {
        if (Array::Get(i) == 0)
        {
            Array::Insert(i, x);
            break;
        }
    }
}
int main()
{
    int size;
    cin >> size;
    SortArray sa(size);
    double num;
    while (1)
    {
        cin >> num;
        if (fabs(num) <= 1e-6) break;
        try
        {
            sa.Insert(num);
        }
        catch (char* wrong)
        {
            cout << wrong << endl;
        }
    }
    sa.Display();
    return 0;
}

/*继承与派生7
已知由Automobille类派生出Car类和Wagon类,而后两者共同派生出StationWagon类,
各类的定义及main中的代码(不允许改动)如下,请实现各个类的成员函数,完成相应的输出:

#include<iostream>
using namespace std;
class Automobile
{
private:
    int power;
public:
    Automobile(int p);
    void Display();
};
Automobile::Automobile(int p)
{
    power = p;
}
void Automobile::Display()
{
    cout << "Power:" << power << endl;
}
class Car :virtual  public Automobile 
{
private:
    int seat;
public:
    Car(int p, int s);
    void Display();
};
Car::Car(int p, int s):Automobile(p)
{
    seat=s;
}
void Car::Display()
{
    cout << "Seat:" << seat << endl;
}
class Wagon :virtual  public Automobile
{
private:
    int load;
public:
    Wagon(int p, int l);
    void Display();
};
Wagon::Wagon(int p, int l):Automobile(p)
{
    load=l;
}
void Wagon::Display()
{
    cout << "Load:" << load << endl;
}
class StationWagon :public Car, public Wagon
{
public:
    StationWagon(int p, int s, int l);
    void Display();
};
StationWagon::StationWagon(int p, int s, int l) :Automobile(p),Car(p, s), Wagon(p, l)
{

}
void StationWagon::Display()
{
    cout << "StationWagon:"<<endl;
    Automobile::Display();
    Car::Display();
    Wagon::Display();
}
int main()
{
    int power, load, seat;
    cin >> power >> seat >> load;
    StationWagon sw(power, seat, load);
    sw.Display();
    return 0;
}

/*多态-虚函数问题
请以点类Point为基类派生出一个圆类Circle。
Point类的数据成员为x、y(私有属性,存储点的横纵坐标),
成员函数有构造函数Point(实现对数据成员x、y的初始化)、输出函数Display(输出点坐标);
圆类Circle的数据成员为r(私有属性,存储圆的半径,圆心的点坐标通过继承点类Point加以实现),成员函数有构造函数Circle、
计算圆的面积函数Area、计算圆的周长函数Perimeter和输出函数Display,其中构造函数实现基类和圆类的数据成员的初始化,
Display函数实现圆心坐标、圆的半径、圆的面积(利用Area函数实现)和圆的周长(利用Perimeter函数实现)的输出。
实现Point类和Circle类的定义及成员函数。主函数的代码(不允许改动)如下:

#include<iostream>
using namespace std;
const double PI = 3.14;
class Point
{
public:
    Point(double xx, double yy)
    {
        x = xx;
        y = yy;
    }
    virtual void Display()
    {
        cout << "Point(" << x << "," << y << ")"<<endl;
        cout << "Circle's center:Point(" << x << "," << y << ")"<<endl;
    }
private:
    double x;
    double y;
};
class Circle:public Point
{
public:
    Circle(double xx, double yy, double rr) :Point(xx, yy)
    {
        r = rr;
    }
    double Area()
    {
        return r * r * PI;
    }
    double Perimeter()
    {
        return 2 * PI * r;
    }
    void Display()
    {
        cout << "Radius:" << r<<endl;
        cout << "Area:"<<Area() << endl;
        cout << "Perimeter:" <<Perimeter() << endl;
    }
private:
    double r;
};
int main()
{
    double x, y, r;
    cin >> x >> y >> r;
    Point* p;
    p = new Point(x, y);
    p->Display();
    delete p;
    p = new Circle(x, y, r);
    p->Display();
    delete p;
    return 0;
}
/*多态-抽象类1
定义一个抽象类Shape,利用抽象类Shape派生圆类Circle,圆类Circle再派生出圆的内接正方形类In_Square和圆的外切正方形类Ex_Square。
main中利用指向抽象类的指针实现求圆、圆的内接正方形和圆的外切正方形的面积和周长的计算。
请实现各个类的成员函数,已知各类的定义及main中的代码如下(不允许修改):

#include<iostream>
#include<cmath>
using namespace std;
const double PI = 3.14;
class Shape
{
public:
    virtual double Area() = 0;
    virtual double Perimeter() = 0;
};
class Circle :public Shape
{
public:
    Circle(double rr);
    double Radius();
    double Area();
    double Perimeter();
private:
    double r;
};
Circle::Circle(double rr)
{
    r = rr;
}
double Circle::Radius()
{
    return r;
}
double Circle::Area()
{
    return r * r * PI;
}
double Circle::Perimeter()
{
    return 2 * r * PI;
}
class In_Square :public Circle
{
public:
    In_Square(double rr);
    double Area();
    double Perimeter();
};
In_Square::In_Square(double rr):Circle(rr){}
double In_Square::Area()
{
    return Radius() * Radius()*2;
}
double In_Square::Perimeter()
{
    return 4 * sqrt(Radius() * Radius()*2);
}
class Ex_Square :public Circle
{
public:
    Ex_Square(double r);
    double Area();
    double Perimeter();
};
Ex_Square::Ex_Square(double r):Circle(r){}
double Ex_Square::Area()
{
    return 2*Radius() * 2*Radius();
}
double Ex_Square::Perimeter()
{
    return 2 * 4 * Radius();
}
int main()
{
    Shape* ptr;
    double r;
    cin >> r;
    ptr = new Circle(r);
    cout << "Circle's area:" << ptr->Area() << endl;
    cout << "Circle's perimeter:" << ptr->Perimeter() << endl;
    ptr = new In_Square(r);
    cout << "In_Square's area:" << ptr->Area() << endl;
    cout << "In_Square's perimeter:" << ptr->Perimeter() << endl;
    ptr = new Ex_Square(r);
    cout << "Ex_Square's area:" << ptr->Area() << endl;
    cout << "Ex_Square's perimeter:" << ptr->Perimeter() << endl;
    return 0;
}
/*多态-抽象类2
定义一个抽象类Shape,应用抽象类Shape派生圆类Circle和长方形类Rectangle。
请完成各个类的定义及成员函数实现。已知Shape类的定义及main中的代码如下(不允许修改):

#include<iostream>
#include<cstring>
using namespace std;
const double PI = 3.14;
class Shape
{
public:
    virtual double Area() = 0;
    virtual double Perimeter() = 0;
};
class Circle:public Shape
{
public:
    Circle(double rr)
    {
        r = rr;
    }
    double Area()
    {
        return r * r * PI;
    }
    double Perimeter()
    {
        return r * 2 * PI;
    }
private:
    double r;
};
class Rectangle :public Shape
{
public:
    Rectangle(double aa, double bb)
    {
        a = aa;
        b = bb;
    }
    double Area()
    {
        return a*b;
    }
    double Perimeter()
    {
        return (a+b)*2;
    }
private:
    double a;
    double b;
};
int main()
{
    double r, a, b;
    char* s[2] = { "Circle:\n","Rectangle:\n" };
    cin >> r >> a >> b;
    Shape* ptr[] = { new Circle(r),new Rectangle(a,b) };
    for (int i = 0; i < 2; i++)
    {
        cout << s[i];
        cout << "Area:" << ptr[i]->Area() << endl;
        cout << "Perimeter:" << ptr[i]->Perimeter() << endl;
    }
    return 0;
}
/*抽象类(3)
定义一个几何图形抽象类Shape,再派生出一个圆类Circle,数据成员为r,
圆类再派生出内接等边三角形类In_Triangle和外切等边三角形类Ex_Triangle。
部分代码已知如下,勿改动,请在此基础上完善代码,并编写main函数,
利用指向Shape类的指针实现各个派生类的面积、周长的函数调用,得到相应的输出结果。圆周率PI值取3.14。

#include<iostream>
#include<cmath>
using namespace std;
const double PI = 3.14;
class Shape
{
public:
    virtual double Area() = 0;
    virtual double Perimeter() = 0;
};
class Circle :public Shape
{
public:
    Circle(double rr);
    double Area();
    double Perimeter();
protected:
    double r;
};
Circle::Circle(double rr)
{
    r = rr;
}
double Circle::Area()
{
    return r * r * PI;
}
double Circle::Perimeter()
{
    return r * 2 * PI;
}
class In_Triangle :public Circle
{
public:
    In_Triangle(double rr);
    double Area();
    double Perimeter();
};
In_Triangle::In_Triangle(double rr) :Circle(rr){}
double In_Triangle::Area()
{
    return 3*0.5*r*r*sqrt(3)/2;
}
double In_Triangle::Perimeter()
{
    return 3*sqrt(3) * r;
}
class Ex_Triangle :public Circle
{
public:
    Ex_Triangle(double r);
    double Area();
    double Perimeter();
};
Ex_Triangle::Ex_Triangle(double r):Circle(r){}
double Ex_Triangle::Area()
{
    return 3*sqrt(3)*r*r;
}
double Ex_Triangle::Perimeter()
{
    return 2*(3 * sqrt(3) * r * r)/r;
}
int main()
{
    double r;
    cin >> r;
    Shape* S =new Circle(r);
    cout << "Circle's area:"<<S->Area()<<endl<<"Circle's perimeter:"<<S->Perimeter()<<endl;
    delete S;
    S = new In_Triangle(r);
    cout << "In_Triangle's area:" << S->Area() << endl << "In_Triangle's perimeter:" << S->Perimeter() << endl;
    delete S;
    S = new Ex_Triangle(r);
    cout << "Ex_Triangle's area:" << S->Area() << endl << "Ex_Triangle's perimeter:" << S->Perimeter() << endl;
    delete S;
    return 0;
}
/*多态-运算符重载1
实现复数的<< ,>>, +, -的运算符重载
已知Complex类的定义(不允许添加)及main中的代码(不允许改动)如下:

#include<iostream>
#include<string>
using namespace std;
const double PI = 3.14;
class Complex
{
public:
    Complex(double r = 0, double i = 0);
    Complex operator +(const Complex& c);
    Complex operator -(const Complex& c);
    friend ostream& operator<<(ostream& os, Complex& com);
    friend istream& operator>>(istream& is, Complex& com);
    void Display();
private:
    double real;
    double imag;
};
Complex::Complex(double r, double i)
{
    real = r;
    imag = i;
}
Complex Complex::operator +(const Complex& c)
{
    Complex a;
    a.real = real + c.real;
    a.imag = imag + c.imag;
    return a;
}
Complex Complex::operator -(const Complex& c)
{
    Complex a;
    a.real = real - c.real;
    a.imag = imag - c.imag;
    return a;
}
ostream& operator<<(ostream& os, Complex& com)
{
    os << "Complex(" << com.real << "," << com.imag << ")";
    return os;
}
istream& operator>>(istream& is, Complex& com)
{
    is >> com.real>>com.imag;
    return is;
}
void Complex::Display()
{
    cout << "Complex(" << real << "," << imag << ")";
}
int main()
{
    Complex c1, c2, c3;
    cin >> c1 >> c2;
    cout << c1 << "+" << c2 << "=";
    c3 = c1 + c2;
    cout << c3 << endl;
    cout << c1 << "-" << c2 << "=";
    c3 = c1 - c2;
    cout << c3 << endl;
    return 0;
}
/*多态-运算符重载2
实现Array类的>>、 <<的运算符重载。已知Array类的定义(不允许增加其它成员函数)及main中的代码如下(不允许修改):

#include<iostream>
#include<string>
using namespace std;
const int MaxSize = 100;
class Array {
public:
    Array();
    void Insert(int i, double x);
    friend ostream& operator<<(ostream& out, Array& arr); 
    friend istream& operator>>(istream& in, Array& arr);
private:
    double data[MaxSize];
    int length;
};
Array::Array()
{
    length = 0;
}
void Array::Insert(int i, double x)
{
    length = i+1;
    data[i] = x;
}
ostream& operator<<(ostream& out, Array& arr)
{
    out << "Length:" << arr.length << endl;
    out << "Data:";
    for (int i = 0; i < arr.length; i++)
    {
        out << arr.data[i] << " ";
    }
    return out;
}
istream& operator>>(istream& in, Array& arr)
{
    int n;
    in >> n;
    double x;
    for (int i = 0; i < n; i++)
    {
        in >> x;
        arr.Insert(i, x);
    }
    return in;
}
int main() {
    Array a;
    cin >> a;
    cout << a;
    return 0;
}

这篇关于c++继承与派生,多态的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!