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=100);
    ~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=0; i<=count; i++)
    {
        delete pCe[i];
    }
    delete []pCe;
}

 

 

 if (iC==1)
        {
            pCe[i]= new CommEntry;
        }
        else if(iC==2)
        {
            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[100];
};
Employee::Employee():name("no name yet!"),age(0),wokeage(0),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>=55)
        cout<<"retire!\n";
    else
        cout<<"not retire!\n";
}

Manager::Manager():level(0)
{
}
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>=60)
        cout<<"retire!\n";
    else
        cout<<"not retire!\n";
}

void Manager::addMember(Employee* e)
{
      numOfEmployee[0]=*e;
}
int main()
{
    Employee  e("Jack", 24, 2, "Development");
    Manager   m("Tom", 30, 5, "Development", 2);
    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 0;
}

 

 

3. 已知类的定义如下:

class Base {

protected:

  int iBody;

public:

  virtual void printOn() = 0;

  Base(int i = 0) : iBody(i) {}

  virtual int display(int x=60) {iBody = xreturn iBody;}

};

class Sub1 : public Base {

  // …

public:

  // …

  Sub1(int i, string s);

};

class Sub2 : public Base {

  // …

public:

  // …

  Sub2(int i, short s);

};

试完成类Sub1Sub2的定义和操作的实现代码,使之能符合下面程序及在注释中描述的运行结果的要求:

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() = 0;
    Base(int i = 0) : iBody(i) {}//构造函数,初始化列表
    virtual int display(int x=60)
    {
        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=20)
    {
        sShort = x;
        return sShort;
    }
};

int 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
    return 0;
}

 

 

 

 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 0;
}

试修改上面的程序,使其输出变成:
 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 0;
}

 

posted @ 2019-06-24 21:35  王陸  阅读(1118)  评论(0编辑  收藏  举报