C++第五章习题

  1. 什么是静态联编?什么是动态联编?

         静态联编是指系统在编译时就决定如何实现某一动作。

         动态联编是指系统在运行时动态实现某一动作。

 

  1. 编译时的多态性与运行时的多态性有什么区别?他们的实现方式有什么不同?

静态联编支持的多态性称为编译时多态性,也称静态多态性。编译时多态性是通过函数重载和模板实现的。

动态联编所支持的多态性称为运行时多态性,也称动态多态性。是通过虚函数来实现的。

 

  1. 简述运算符重载规则。

a)         C++中绝大部分的运算符允许重载。

b)         C++只能对已有的C++运算符进行重载。

c)         运算符重载是针对新类型数据的实际需要,对原有运算符进行适当的改造完成。应与原有的功能相类似。

d)         重载不能改变运算符的操作对象的个数。

e)         重载不能改变运算符原有的优先级。

f)          不能改变原有的结合特性。

g)         参数至少应有一个是类对象。

h)         重载函数可以是普通函数,也可以是类的成员函数,也可以是类的友元函数。

i)           一般用于类对象的运算符除了赋值运算符都要重载。

 

  1. 友元运算符函数和成员运算符函数有什么不同?

a)         对于双目运算符,成员运算符重载含有一个参数,友元重载含有两个参数;对于单目运算符,成员重载没有参数,友元重载有一个参数。

b)         双目运算符一般可以被重载为友元或成员。但是如果将一个对象和一个普通类型运算,必须重载为友元。

c)         调用方式不同。

d)         一般,双目运算符用友元,单目运算符用成员。如果操作数有隐式类型转换,则必须用友元。

 

  1. 什么是虚函数?虚函数与函数重载有哪些相同点与不同点?

虚函数就是在基类中被关键字virtual说明,并在派生类中重新定义的函数。

函数名都相同。

重载函数的参数个数或参数类型必须有所不同。虚函数要求函数名,返回类型,参数个数,参数的类型和顺序与基类中的虚函数原型完全相同。

 

  1. 什么是纯虚函数?什么是抽象类?

纯虚函数是一个在基类中说明的虚函数,它在该基类中没有定义,但要求在它的派生类中根据需要对它进行定义,或仍说明为纯虚函数。

如果一个类至少有一个纯虚函数,那么就称这个类为抽象类。

 

7-12 DAADCC

 

13.

         不对,参数个数必须一样。

 

14.

7

6

 

15.

This is c++book.

第一个字符:T

第16个字符:.

第26个字符:数组下标超界!

 

16.

m=1.5千米

 

17.

#include <iostream>
using namespace std;

class twoDArray
{
    int a[2][3];
public:
    twoDArray()
    {
        for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                a[i][j] = 5;
    }
    twoDArray(int b[][3])
    {
        for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                a[i][j] = b[i][j];
    }
    void show();
    twoDArray operator+(twoDArray n);
    twoDArray operator-(twoDArray n);
};
void twoDArray::show()
{
    for(int i = 0; i <2; i ++)
    {
            for(int j = 0; j < 3; j ++)
                cout << a[i][j];
            cout << endl;
    }
}

twoDArray twoDArray::operator+(twoDArray n)
{
    twoDArray temp;
    for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                temp.a[i][j] = a[i][j] + n.a[i][j];
    return temp;
}
twoDArray twoDArray::operator-(twoDArray n)
{
    twoDArray temp;
    for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                temp.a[i][j] = a[i][j] - n.a[i][j];
    return temp;
}

int main()
{
    int a[2][3];
    for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                cin >> a[i][j];
    twoDArray a1, a2(a), total, sub;
    total = a1 + a2;
    sub = a1 - a2;
    total.show();
    sub.show();
    return 0;
}

 

18.

#include <iostream>
using namespace std;

class twoDArray
{
    int a[2][3];
public:
    twoDArray()
    {
        for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                a[i][j] = 5;
    }
    twoDArray(int b[][3])
    {
        for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                a[i][j] = b[i][j];
    }
    void show();
    friend twoDArray operator+(twoDArray m, twoDArray n);
    friend twoDArray operator-(twoDArray m, twoDArray n);
};
void twoDArray::show()
{
    for(int i = 0; i <2; i ++)
    {
            for(int j = 0; j < 3; j ++)
                cout << a[i][j];
            cout << endl;
    }
}

twoDArray operator+(twoDArray m, twoDArray n)
{
    twoDArray temp;
    for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                temp.a[i][j] = m.a[i][j] + n.a[i][j];
    return temp;
}
twoDArray operator-(twoDArray m, twoDArray n)
{
    twoDArray temp;
    for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                temp.a[i][j] = m.a[i][j] - n.a[i][j];
    return temp;
}

int main()
{
    int a[2][3];
    for(int i = 0; i <2; i ++)
            for(int j = 0; j < 3; j ++)
                cin >> a[i][j];
    twoDArray a1, a2(a), total, sub;
    total = a1 + a2;
    sub = a1 - a2;
    total.show();
    sub.show();
    return 0;
}

 

19.

#include <iostream>
using namespace std;

class complex
{
    double real, imag;
public:
    complex(double re, double im)
    {
        real = re;
        imag = im;
    }
    friend complex operator+(complex a, complex b);
    void print();
};

complex operator+(complex a, complex b)
{
    return complex(a.real + b.real, a.imag + b.imag);
}
void complex::print()
{
    cout << "(" << real << "," << imag << ")" << endl;
}

int main()
{
    complex c1(2.5,3.7);
    complex c2(4.2,6.5);
    complex total = c1 + c2;
    total.print();
    return 0;
}

 

20.

#include <iostream>
using namespace std;

const double PI = 3.14;
class Container
{
protected:
    double r, d; // 如果是球形,r是半径,d为0. 如果是圆柱体,r为底面半径,d为高。如果是正方体,r为边长, d为0。
public:
    Container(double a, double b = 0)
    {
        r = a;
        d = b;
    }
    virtual double serface() = 0; // 计算表面积
    virtual double volume() = 0; //计算体积
};

class Sphere : public Container
{
public:
    Sphere(double r):Container(r){}
    double serface();
    double volume();
};
double Sphere::serface()
{
    return 4*PI*r*r;
}
double Sphere::volume()
{
    return PI*r*r*r;
}

class Cylinder : public Container
{
public:
    Cylinder(double a, double b):Container(a, b){}
    double serface();
    double volume();
};
double Cylinder::serface()
{
    return 2*PI*r*d + PI*r*r;
}
double Cylinder::volume()
{
    return PI*r*r*d;
}

class Cube : public Container
{
public:
    Cube(double a):Container(a){}
    double serface();
    double volume();
}
double Cube::serface()
{
    return 6*r*r;
}
double Cube::volume()
{
    return r*r*r;
}

 

转载于:https://www.cnblogs.com/tangzhengyue/archive/2012/06/15/2550871.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值