天天看點

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 = x;return iBody;}

};

class Sub1 : public Base {

  // …

  Sub1(int i, string s);

class Sub2 : public Base {

  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() = 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語言畫出類圖,并給出相應類中方法的界面(頭檔案).

C++設計考試例題

補充一道期末考試題。

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

作者:王陸