当前位置: 首页 > news >正文

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

相关文章:

  • open cv对比度与亮度调节
  • POJ 1905
  • open cv绘制形状与文字
  • open cv均值 中值 高斯 双边高斯 滤波及模糊
  • C语言运算符优先级
  • open cv膨胀与腐蚀
  • 浅谈外链因何而存在?
  • open cv形态学操作
  • 微软发布Surface平板电脑 再度挑战苹果
  • open cv提取水平线与垂直线
  • 用VBScript实现Zip解压缩目录中的所有文件
  • open cv图像上采样与下采样
  • C#中的Main方法
  • open cv阈值的基本操作
  • GridView 点击子View时设置子View的背景色为透明
  • JavaScript 如何正确处理 Unicode 编码问题!
  • 分享一款快速APP功能测试工具
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 【comparator, comparable】小总结
  • Angular6错误 Service: No provider for Renderer2
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • ES6简单总结(搭配简单的讲解和小案例)
  • golang 发送GET和POST示例
  • JavaScript实现分页效果
  • js算法-归并排序(merge_sort)
  • python学习笔记 - ThreadLocal
  • React 快速上手 - 07 前端路由 react-router
  • 代理模式
  • 类orAPI - 收藏集 - 掘金
  • 免费小说阅读小程序
  • 前端存储 - localStorage
  • 小程序开发之路(一)
  • 一起参Ember.js讨论、问答社区。
  • HanLP分词命名实体提取详解
  • 翻译 | The Principles of OOD 面向对象设计原则
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • #LLM入门|Prompt#3.3_存储_Memory
  • (1)bark-ml
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (solr系列:一)使用tomcat部署solr服务
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (转) Face-Resources
  • (转) ns2/nam与nam实现相关的文件
  • (转)3D模板阴影原理
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • .gitignore文件_Git:.gitignore
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .net中我喜欢的两种验证码
  • .sys文件乱码_python vscode输出乱码