C++设计考试例题
1. 采用面向对象的方式编写一个通迅录管理程序,通迅录中的信息包括:姓名,公司,联系电话,邮编。要求的操作有:添加一个联系人,列表显示所有联系人。先给出类定义,然后给出类实现。(提示:可以设计二个类,一个通迅录条目类CommEntry,一个通讯录类Commus)
class CommEntry
{
public:
CommEntry();
~CommEntry();
virtual void input();
virtual void output();
void setName(string nm);
void setTel(string t);
string getName();
string getTel();
void setTelCount(int c);
private:
string name;
int telCount;
string tel;
string telType;
}; class FreindEntry: public CommEntry
{
public:
void input();
void output();
void setEmail(string nm);
string getEmail();
private:
string Email;
}; class Comms
{
public:
Comms(int max=);
~Comms();
void inputAll();
void outputAll();
void find(string nm);
void modify_tel(string nm);
private:
CommEntry **pCe;
int maxCount;
int count;
};
Comms::Comms(int maxCount)
{
pCe = new CommEntry * [maxCount];
}
Comms::~Comms()
{
int i;
for(i=; i<=count; i++)
{
delete pCe[i];
}
delete []pCe;
}
if (iC==)
{
pCe[i]= new CommEntry;
}
else if(iC==)
{
pCe[i]= new FreindEntry;
}
pCe[i]->input();
/*Employee 和Manager,Manager 是一种特殊的Employee。
Employee 对象所具有的基本信息为:姓名、年令、工作年限、部门号,
对象除具有上述基本信息外,还有级别(level)信息。公司中的两类职
输出Employee/Manager 对象的个人信息
retire() // 判断是否到了退休年令,是,屏幕给出退休提示。公司规定:
类对象的退休年令为55 岁,Manager 类对象的退休年令为60 岁
定义并实现类Employee 和Manager;
(注意:Manager继承自Employee)
定义一个测试程序,测试所定义的类Employee 和Manager*/
#include<iostream>
#include<string>
using namespace std;
class Employee
{
public:
Employee();
Employee(string the_name,int the_age,int the_wokeage,string the_depNo);
void printOn();
void retire();
protected:
string name;
int age;
int wokeage;
int number;
string depNo;//部门号
}; class Manager:public Employee
{
public:
Manager();
Manager(string the_name,int the_age,int the_wokeage,string the_depNo,int the_level);
void printOn();
void retire();
void addMember(Employee*);
private:
int level;
Employee numOfEmployee[];
};
Employee::Employee():name("no name yet!"),age(),wokeage(),depNo("no name yet!")
{
}//初始化列表 Employee::Employee(string the_name,int the_age,int the_wokeage,string the_depNo)
{
name=the_name;
age=the_age;
wokeage=the_wokeage;
depNo=the_depNo;
}
void Employee::printOn()
{
cout<<"name is "<<name<<endl
<<"age is "<<age<<endl
<<"wokeage is "<<wokeage<<endl
<<"bumen number is "<<number<<endl;
} void Employee::retire()
{
if(age>=)
cout<<"retire!\n";
else
cout<<"not retire!\n";
} Manager::Manager():level()
{
}
Manager::Manager(string the_name,int the_age,int the_wokeage,string the_depNo,int the_level)
:Employee(the_name,the_age,the_wokeage,the_depNo),level(the_level)
{ }//初始化列表
void Manager::printOn()
{
cout<<"name is "<<name<<endl
<<"age is "<<age<<endl
<<"wokeage is "<<wokeage<<endl
<<"bumen number is "<<number<<endl
<<"level is "<<level<<endl;
}
void Manager::retire()
{
if(age>=)
cout<<"retire!\n";
else
cout<<"not retire!\n";
} void Manager::addMember(Employee* e)
{
numOfEmployee[]=*e;
}
int main()
{
Employee e("Jack", , , "Development");
Manager m("Tom", , , "Development", );
m.addMember(&e);//m管理e
e.printOn();
m.printOn();
Employee* p = &e;//基类指针指向基类对象
p->retire(); // 如果雇员的年龄是55,则b为true
p = &m;//基类指针指向派生类对象
p->retire (); // 如果管理者的年龄是60,则 b为true
return ;
}
3. 已知类的定义如下:
|
class Base { protected: int iBody; public: virtual void printOn() = 0; Base(int i = 0) : iBody(i) {} virtual int display(int x=60) {iBody = x;return iBody;} }; class Sub1 : public Base { // … public: // … Sub1(int i, string s); }; class Sub2 : public Base { // … public: // … Sub2(int i, short s); }; |
试完成类Sub1和Sub2的定义和操作的实现代码,使之能符合下面程序及在注释中描述的运行结果的要求:
|
main(){ Sub1 s1(1000, "This is an object of Sub1"); Sub2 s2(1000, 20); s1.printOn(); // 此时显示出: 1000: This is an object of Sub1 s2.printOn(); // 此时显示出: 20 and 1000 cout<<s2.display(20); // 此时显示出: 20 } |
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
class Base
{
protected:
int iBody;
public:
virtual void printOn() = ;
Base(int i = ) : iBody(i) {}//构造函数,初始化列表
virtual int display(int x=)
{
iBody = x;
return iBody;
}
};
class Sub1 : public Base
{
string cpString;
public: Sub1(int i, string s) : Base(i),cpString(s)
{ }
void printOn()
{
cout<<iBody<<":"<<cpString<<endl;
}
};
class Sub2 : public Base
{
short sShort;
public: Sub2(int i, short s) : Base(i),sShort(s) {}
void printOn()
{
cout<<sShort<<" and "<<iBody<<endl;
}
int display(int x=)
{
sShort = x;
return sShort;
}
}; int main()
{
Sub1 s1(, "This is an object of Sub1");
Sub2 s2(, );
s1.printOn(); // 此时显示出: 1000: This is an object of Sub1
s2.printOn(); // 此时显示出: 20 and 1000
cout<<s2.display(); // 此时显示出: 20
return ;
}
4. 在一个GUI程序中,有一系列相关的类,如circle,triangle,square等等,其中square由二个triangle对象构成. circle,triangle,square等类的对象都有相似的行为print(string)(打印出该类对象的相应信息,如类circler的此函数输出”Circle”),draw()(画出相应的类对象的图形),我们应如何组织这些类,使得系统易于扩充和维护?请用UML语言画出类图,并给出相应类中方法的界面(头文件).

补充一道期末考试题。
5.
#include <iostream>
using namespace std;
void hello( ) { cout << " Hello, world!\n"; }
int main( ) {
hello( ); return ;
}
试修改上面的程序,使其输出变成:
Begin
Hello, world!
End
限制:(1)不能对main()进行任何修改;(2)不能修改hello()函数。
解题思路:利用类的构造函数和析构函数来实现!!!
#include <iostream>
using namespace std;
class A {
public:
A ( ) { cout << "Begin\n"; }
~A ( ) { cout << "End\n"; }
}; void hello( ) {cout << " Hello, world!\n"; } A a; // a是一个全局对象
int main( ) {
hello( );
return ;
}
C++设计考试例题的更多相关文章
- 基于Web在线考试系统的设计与实现
这是一个课程设计的文档,源码及文档数据库我都修改过了,貌似这里复制过来的时候图片不能贴出,下载地址:http://download.csdn.net/detail/sdksdk0/9361973 ...
- C#基础视频教程6.3 如何简单读写数据库
在继续往下做之前,我们需要把之前的代码尽可能的精简(会对后面很有好处,而且读者也应该仔细比对这一部分的代码和上一部分哪里真正得到了优化,从而提高编程水平). 首先数据库的操作类有哪些是可以做的更加普遍 ...
- 2017.10.26 JavaWeb----第五章 JavaBean技术
JavaWeb----第五章 JavaBean技术 (1)JavaBean技术 JavaBean技术是javaweb程序的重要组成部分,是一个可重复使用的软件组件,是用Java语言编写的.遵循一定的标 ...
- 关于托福改革后的难度、评分和拼分,听听ETS的老师怎么说
“笔者有幸于几天前去到ETS位于普林斯顿的总部学习,聆听了他们关于托福考试的发展和变革的说明,在这里向大家汇报一下此行的收获.” 当从车上下来那一刻起,我们便被那辽阔的绿草地和宜人的风景所吸引,伴随着 ...
- 《Spring_Four》第三次作业——基于Jsoup的大学生考试信息展示系统的原型设计与开发
<Spring_Four团队>第三次团队项目——基于Jsoup的大学生考试信息展示系统的原型设计与开发 一.实验目的与要求 (1)掌握软件原型开发技术: (2)学习使用软件原型开发工具:本 ...
- 课程设计之"网络考试系统"(php、Extjs)
1.TestSystem大概结构框图 2.数据库设计(11张表) 数据库名称:db_testsystem 数据库表: tb_admin 记录题库管理员帐户信息 代码 tb_allcontent 记录随 ...
- MongoDB实现问卷/考试设计
MongoDB的特点 MongoDB是一个面向文档存储的数据库.在MongoDB中,一条记录叫做document(文档),由类似于JSON结构的键值对组成. 由于类似于MongoDB直接存储JSON的 ...
- 基于B/S架构的在线考试系统的设计与实现
前言 这个是我的Web课程设计,用到的主要是JSP技术并使用了大量JSTL标签,所有代码已经上传到了我的Github仓库里,地址:https://github.com/quanbisen/online ...
- 紫书 例题 9-3 UVa 1347 ( 状态设计)
首先做一个转化,这种转化很常见. 题目里面讲要来回走一遍,所以就转化成两个从起点到终点,路径不重合 那么很容易想到用f[i][j]表示第一个走到i,第二个人走到j还需要走的距离 但是这里无法保证路径不 ...
随机推荐
- Java工作流引擎表单引擎之JS表单字段输入脚本验证
关键字: 表单设计器, 字段验证. workflow,ccform, ccBPM. 工作流快速开发平台 工作流流设计 业务流程管理 asp.net 开源工作流bpm工作流系统 java工作流 ...
- 分享一个web安全学习站点
大神建议: https://blog.knownsec.com/Knownsec_RD_Checklist/v3.0.html#FMID_1218170279FM https://websec.rea ...
- Android Activity启动流程, app启动流程,APK打包流程, APK安装过程
1.Activity启动流程 (7.0版本之前) 从startActivity()开始,最终都会调用startActivityForResult() 在该方法里面会调用Instrumentation. ...
- 基于XML的开发
基于XML的开发 1.定义一个切面类 /** * Created by zejian on 2017/2/20.*/ public class MyAspectXML { public void be ...
- 互联网大厂Java面试题集—Spring boot面试题(一)
Spring Boot 需要独立的容器运行吗? 可以不需要,内置了 Tomcat/ Jetty 等容器.通过pom.xml中导入依赖: <!--spring-boot-starter-web:代 ...
- 【戾气满满】Ubuntu 18.04使用QT通过FreeTDS+unixODBC连接MSSQL填坑记(含泪亲测可用)
前言 照例废话几句,想玩下QT,但是学习吧总得想点事情做啊,单纯学习语法用法这些?反正我是学不下去的,脑袋一拍,就先学下怎么连接数据库吧!然而万万没想到,我这是给自己挖了一个深深的坑啊! 学习自然去官 ...
- 文件系统之parted 分区
parted分区命令 1.分区表区别 我们 Linux 系统中有两种常见的分区表 MBR 分区表(主引导记录分区表)和 GPT 分区表(GUID 分 区表) MBR 分区表:支持的最大分区是 2TB( ...
- C# -- 使用缓冲区进行文件下载操作
C# -- 使用缓冲区进行文件下载操作 1. 为避免下载超大文件占用内存资源,文件下载使用缓冲区,一点一点读取文件资源. string str0 = @"ftp://localhost:21 ...
- 使用EasyPOI导入导出图片出现数组越界异常
在我使用easypoi做导出功能的时候,突然抛了一个数组越界异常,找了很久也没找到,最后猜想有可能是路径出了问题,然后打印了一下图片存放的路径,结果发现在其保存路径上存在“.”,也就是easypoi底 ...
- Prism_ViewModelLocator(5)
ViewModelLocator ViewModelLocator用于绑定视图的DataContext,以使用标准命名约定的一个ViewModel的实例. Prism ViewModelLocator ...