刚做的c++作业,高手可以帮忙指点下吗,谢谢啦!如有需要可加我 QQ:568835322

1)       下面描述错误的是____A_______3分)
  A.
局部变量和局部静态变量均存放于栈内存中
  B.
全局变量和全局静态变量存放于数据区内存中
  C.
动态分配的内存存放于堆内存中
  D.
类的静态成员变量存放于数据区内存中

2)       以下是合法的标识符的是____B____3分)
  A. define          B. abc/            C. vector             D. #int

3)       int i;
for (i=0; i=2; i++) {};
试问该循环执行次数:   无限次         (3)

4)       char szText[100]; sizeof(szText) =      100       (3)
char * pszText = szText; sizeof(pszText) =     4        (3
)
void Function(short szParam[100])
{
    sizeof(szParam) =     200        (3
)
}
char szText[] = “Hello”; sizeof(pszText) =     4        (3
)
struct TestNode
{
    char ch1;
    short s;
    char ch2;
    long l;
};
sizeof(TestNode) =      12       (3
)

5)       下面与函数 void Print(char ch = ‘a’, int i = 0); 不构成重载的两个函数        C    (3)
   A. void Print(int i, char ch = ‘a’);
   B. void Print(char ch);
   C. int Print();
   D. int Print(char ch, char ch);

6)       下面程序的输出结果为     C       (3)
#include<iostream>
using namespace std;
void main()
{
  char* a[] = {"hello", "the", "world"};
  char** pa = a;
  pa++;
  cout << *pa << endl;
}
  A. hello             B. elllo              C. the             D. world

7)       关于构造函数与析构函数的描述正确的是    D        (3)
  A.
一个类可以没有构造函数与析构函数
  B.
一个类可以有多个构造函数与多个析构函数
  C.
一个类肯定会存在一个默认构造函数和析构函数
  D.
一个类肯定存在构造函数与析构函数

8)       class CBase {};
class CDerive : public CBase {};
CBase base;
CDerive derive;
以下用法不正确的是      D       (3)

  A. CBase* p = &derive;                   B. CBase& r = base;
  C. base = derive;                         D. derive = base;

9)       class CBase{
public:
    virtual ~ CBase () { Print(); }
    virtual void Print() { cout << “CBase destruct” << endl; }
};
class CDrive : public CBase {
public:
    virtual void Print() { cout << “CDrive destruct” << endl; }
};
其输出结果为    CBase destruct          (3)

10)    typedef <typename _T> class CObjectImpl {
public:
    void Print() { _T* pThis = (_T*)this;  pThis->OnPrint(); }
    void OnPrint() { cout << “CObjectImpl” << endl; }
};
class CInstance : public CObjectImpl< CInstance> {
public:
    void OnPrint() { cout << “CInstance” << endl; }
}
class CDerive : public CInstance {
public:
    void OnPrint() { cout << “CDerive” << endl; }
}
void main(void) {
    CInstance instance;
    CDerive derive;
    instance.Print();
    derive.Print();
}
运行结果为:

     CObjectImpl                     (3
)
      CInstance                    (3
)

11)    int nData[100];
请写出sizeof(nData)/ sizeof(nData[0])的值,并说明这个代码的作用。(5
)
sizeof(nData)=400
sizeof(nData[0])=4
sizeof(nData)/ sizeof(nData[0])=100





12)    请写出const的用途(5分)
1
,定义常量

2,修饰参数

3,修饰函数(返回值)







13)    class CBase {};
class CDrive : public CBase
{
public:
    CDrive() : m_strName(“”);
private:
    string       m_strName;
}

void main()
{
    CBase* pBase = new CDrive[10];
    delete[] pBase;
}
请列出上述代码存在的问题:(10分)


答:在类中声明默认构造函数CDrive(),但是没有实现这个默认构造函数

而且 : m_strName(“”) 是在定义函数是使用的!
一个类必须要要一个析构函数





14)    请实现函数memcpy
pDest
为目标地址,pSrc为源地址,nSize为要复制的字节数。(15分)
#include <iostream>

#include <string>

using namespace std;

void* memcpy(void* pDest, const void* pSrc, int nSize)

       {

              if (pDest == NULL || pSrc == NULL)

          {

                return NULL;

          }

       if (pDest == pSrc)

       {

              return pDest;

       }

      

if( nSize>0 )

{

        int Offset = pDest - pSrc;          

        if (Offset > 0 && Offset < nSize+1)

{

              pDest += nSize;

              pSrc += nSize;

              for (;nSize >=0; nSize --, * pDest -- = * pSrc --)

              {

               }

         }

         else

         {

              for (; (*pDest ++=* pSrc ++) != '/0' && nSize>=0; nSize--) {};

         }

              return pDest;

       }

int main(int argc, char* argv[])

{

    char *pszDest = new char[100];

       memset(pszDest, 0, 100);

memcpy(pszDest, “Hello !”,8);

       cout << pszDest << endl;

    return 0;                                                                                                  

}
}

15)    请实现类CString(20)
class CString
{
public:
    CString(const char* psz = NULL);
    CString(const CString&);
    ~CString();
    CString& operator=(const char* psz);
    bool operator==(const char* psz) const;
private:
    char* m_psz;
}

///

CString::CString(const char* psz)

: m_psz(NULL)

{

       if (psz != NULL)

       {

         Int nLength;

              nLength = strlen(psz);

              m_psz = new char[nLength+1];

              strcpy(m_psz, psz);

       }

}

CString::CString(const CString& )

: m_psz(NULL)

 {

       if (this.m_psz != NULL)

       {

              Int nLength = strlen(m_psz);

              m_psz = new char[nLength+1];

              strcpy(m_psz, this.m_psz);

       }

}

///

CString::~CString()

{

       if (m_psz != NULL)

       {

              delete[] m_psz;

              m_psz = NULL;

       }

}

//

 

 

 

CString& CString::operator=(const CString &psz)

{

       if (this == &psz)

       {

              return *this;

       }

 

       if (m_psz != NULL)

       {

              delete[] m_psz;

              m_psz = NULL;

       }

       if (psz.m_psz != NULL)

       {

              int nLength = strlen(m_psz);

              m_psz = new char[nLength+1];

              strcpy(m_psz, psz.m_psz);

       }

       return *this;

}

/

bool CString:: operator==(const char* psz) const

{

       psz = (psz==NULL) ? "" : psz;

       const char* pString = m_psz==NULL ? "" : m_psz;

       return strcmp(pString, psz) == 0;

}

/

int main ()

{

CString str1("bear");

     CString str2("rabit");

If(str1 == str2)

{

     Cout<<str1<< == <<str2<<endl;

}

else

{

  Cout<<str1<< != <<str2<<endl;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值