您的位置:首页 > 编程语言 > C语言/C++

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

2010-07-14 22:24 447 查看
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<<”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐