做一些基本题

trycatch2004 2007-03-18 07:31:45
1、下面两个myfunc是重载还是覆盖?
namespace IBM
{
int myfunc(int a);
namespace SUN
{
int myfunc(double b);
}
}


2、假如运行环境int类型4bytes,short类型2bytes,long类型8bytes,存在代码:
unsigned short x=65530;
int a=myfunc(x,20.0);
会优先匹配以下哪一个重载函数?为什么?
A.int myfunc(double,double)
B.int myfunc(short,double)
C.double myfunc(int,float)
D.double myfunc(int,double)

3.
template <typename T>
inline T const & max(T const & a, T const & b)
...

下面哪个会引起编译错误,为什么?
a.max(4,7)
b.max(4,4.2)
c.max(static_cast<double>(4),4.2)
d.max<double>(4,4.2)

4.
#include <string>
template <typename T>
inline T max(T a,T b)
{
return a<b?b:a;
}
下面哪个调用会引起编译错误,为什么?
a.::max("apple","peach")
b.std::string s; ::max("apple",s)

5.下面程序正确的输出是哪个?
#include <iostream>
class BaseException
{
};
class DerivedException::public BaseException
{
};
void makeExcp()
{
BaseException& excp=DerivedException();
throw excp;
}

int main(int argc,char* argv[])
{
try
{
makeExcp();
}
catch(DerivedException&)
{
std::cout<<"caught DerivedException"<<std::endl;
}
catch(BaseException&)
{
std::cout<<"caught BaseException"<<std::endl;
}
catch(...)
{
std::cout<<"caught other Exception"<<std::endl;
}
return 0;
}

a.caught DerivedException
b.caught BaseException
c.caught other Exception
d.没有输出


6.对于名字空间以下哪个说法是错误的:
A.using声明可用于类的声明中
B.using指示具有传递性
C.如果某函数访问其自身所在名字空间中成员元素时,可以直接访问
C.通过使用using指示引入存在有相同成员元素的多个名字空间,在实际存取这些存在冲突的成员元素之前,也会导致当前作用域中

元素冲突的错误发生。

7.在编译过程中处理#include语句额阶段是:
A.预编译 B.词法分析 C.语法分析 D.语义分析 E.二进制代码生成

8.下面说法是否正确:
#include机制用于将源程序片段收集在一起,形成一个完整的编译单元作为编译器的输入。

9.下列初始化中,不合法的赋值有:
A.int i=-1;
B.const int ic=i;
C.const int *pic=⁣
D.int * const cpi=⁣

10.比较代码一和代码二的区别,选择错误说法:
//代码一
int *func(int, int)
{
...
return 0;
};

typedef int *(PF)(int, int);

PF getFunc()
{
return func;
}

int main()
{
PF pFunc = getFunc();
return 0;
}

//代码二
int func(int, int)
{
...
return 0;
};

typedef int (*PF)(int, int);

PF getFunc()
{
return func;
}

int main()
{
PF pFunc = getFunc();
return 0;
}

下列说法哪个错误:
A.两个代码都编译通过
B.代码一getFunc()编译错误,返回类型不能为函数类型


11.
int defaultFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 >= iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}

int CompaerFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 < iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}

typedef int (*PF)(const int &, const int &);
int call(int iTmp1, int iTmp2, PF pFunc = defaultFunc)
{
return pFunc(iTmp1, iTmp2);
}

int main(int argc, char *argv[])
{
int iTmp1 = 1;
int iTmp2 = 2;

PF pCompare = CompareFunc;
printf("result:%d\n",call(iTmp1, iTmp2));
printf("result:%d\n",call(iTmp1, iTmp2, pCompare));
printf("result:%d\n",call(iTmp1, iTmp2, CompareFunc));

return 0;
}
说法错误的是:
A.printf("result:%d\n",call(iTmp1, iTmp2));输出结果为result:2
B.printf("result:%d\n",call(iTmp1, iTmp2, pCompare));输出结果为result:1
C.call(iTmp1, iTmp2)中调用defaultFunc函数
D.call(iTmp1, iTmp2, pCompare)第三个参数传入CompareFunc()的函数指针
E.printf("result:%d\n",call(iTmp1, iTmp2, CompareFunc));此句编译错误

12.
下面这句是否正确:
函数执政可以用0来初始化赋值,以表示该指针不指向任何函数。

13.下面选项中哪个不能应用预赋值表达式处?
int main
{
int a[10];
int *p=NULL;
赋值表达式的位置;
return 0;
}

A.p=a;
B.p=&a;
C.p=&a[0];
D.p=&a[9];*p=1;

14.下面选项中哪个不能应用预赋值表达式处?
int main
{
int a[10];
int *p=NULL;
p=&a[4];
赋值表达式的位置;
printf("Hello World!\n");
return 0;
}
A.*(p++);
B.*(++p);
C.(*p)++;
D.*(p--);

15.
class CTest
{
public:
CTest(){}
~CTest(){}
};
定义一个类的内存分配函数错误的是:
A.static void *operator new(size_t sz);
B.void *operator new(size_t sz);
C.static void *operator new(size_t sz, char chFill);
C.void *operator new();

16.
class Base
{
public:
virtual void display(std::string strShow="I am Base class!")
{
std::cout<<strShow<<std::endl;
}
virtual ~Base(){}
};

class Derive:public Base
{
public:
virtual void display(std::string strShow="I am Derive class!")
{
std::cout<<strShow<<std::endl;
}
virtual ~Derive(){}
};

int main()
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase->display();
pDerive->display();
delete pBase;
delete pDerive;
return 0;
}

程序输出结果是:
A.I am Base class!
I am Base class!
B.I am Derive class!
I am Derive class!
C.I am Base class!
I am Derive class!
D.I am Derive class!
I am Base class!



17.
class Base
{
public:
virtual void display() const //注意这里const
{
std::cout<<"I am Base class!"<<std::endl;
}
virtual ~Base(){}
};

class Derive:public Base
{
public:
virtual void display()
{
std::cout<<"I am Derive class!"<<std::endl;
}
virtual ~Derive(){}
};

int main()
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase->display();
pDerive->display();
delete pBase;
delete pDerive;
return 0;
}

程序输出结果是:
A.I am Base class!
I am Base class!
B.I am Derive class!
I am Derive class!
C.I am Base class!
I am Derive class!
D.I am Derive class!
I am Base class!


18.下面这句说法是否正确:
子类可以访问父类的保护成员,子类的友元类也可以通过子类对象去访问父类的保护成员。

19.类的构造函数可不可以是虚函数,为什么?

20.下面说法是否正确:
全局类对象的构造函数总是在main函数之前执行的

21.下面说法正确的是:
A.纯虚函数是一种特殊的虚函数,它是个空函数
B.一个基类中说明有纯虚函数,其派生类一定要实现该纯虚函数
C.具有纯虚函数的类不能创建类对象

22.类B是通过public继承方式从类A派生而来的,且类A和类B都有完整的实现代码,那么下列说法正确的是:
A.类B中具有public可访问性的成员函数个数一定不少于类A中public成员函数的个数
B.一个类B的实例对象占用的内存空间一定不少于一个类A的实例对象占用的内存空间
C.只要类B中的构造函数都是public的,在main函数中就可以创建类B的实例对象
D.类A和类B中的同名虚函数的返回值类型必须完全一致

23.下面叙述正确的是:
A.当基类的析构函数是虚函数时,它的派生类的析构函数也是虚函数
B.析构函数调用虚函数采用动态联编
...全文
948 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
maoxing63570 2010-12-31
  • 打赏
  • 举报
回复
Question21:
Once a function is declared as virtual in a base class it remains virtual; nothing the derived classes do can change the fact that the function is virtual. When a derived class redefines a virtual, it may use the virtual keyword, but it is not required to do so.

Key Concept: Polymorphism in C++
The fact that the static and dynamic types of references and pointers can differ is the cornerstone of how C++ supports polymorphism.

When we call a function defined in the base class through a base-class reference or pointer, we do not know the precise type of the object on which the function is executed. The object on which the function executes might be of the base type or it might be an object of a derived type.

If the function called is nonvirtual, then regardless of the actual object type, the function that is executed is the one defined by the base type. If the function is virtual, then the decision as to which function to run is delayed until run time. The version of the virtual function that is run is the one defined by the type of the object to which the reference is bound or to which the pointer points.

From the perspective of the code that we write, we need not care. As long as the classes are designed and implemented correctly, the operations will do the right thing whether the actual object is of base or derived type.

On the other hand, an object is not polymorphicits type is known and unchanging. The dynamic type of an object (as opposed to a reference or pointer) is always the same as the static type of the object. The function that is run, virtual or nonvirtual, is the one defined by the type of the object.

Virtuals are resolved at run time only if the call is made through a reference or pointer. Only in these cases is it possible for an object's dynamic type to be unknown until run time.

Question17:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Base
{
public:
virtual void display() const //注意这里const
{
std::cout << "I am Base class! " <<std::endl;
}
virtual ~Base(){}
};

class Derive:public Base
{
public:
virtual void display()
{
std::cout << "I am Derive class! " <<std::endl;
}
virtual ~Derive(){}
};




int _tmain(int argc, _TCHAR* argv[])
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase-> display();
pDerive-> display();
delete pBase;
delete pDerive;

system("pause");
return 0;
}
//输出:
//I am Base class!
//I am Derive class!
maoxing63570 2010-12-31
  • 打赏
  • 举报
回复
Question4:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template <typename T>
inline T max(T a,T b)
{
return a <b?b:a;
}

int _tmain(int argc, _TCHAR* argv[])
{
::max( "apple ", "peach ");
std::string s;
::max( "apple ",s);//error C2782: 'T max(T,T)' : template parameter 'T' is ambiguous
system("pause");
return 0;
}

Question11:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int defaultFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 >= iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}

int CompareFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 < iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}

typedef int (*PF)(const int &, const int &);
int call(int iTmp1, int iTmp2, PF pFunc = defaultFunc)
{
return pFunc(iTmp1, iTmp2);
}


int _tmain(int argc, _TCHAR* argv[])
{
int iTmp1 = 1;
int iTmp2 = 2;

PF pCompare = CompareFunc;
printf( "result:%d\n ",call(iTmp1, iTmp2)); //调用默认的返回2
printf( "result:%d\n ",call(iTmp1, iTmp2, pCompare)); //调用compareFunc,返回1
printf( "result:%d\n ",call(iTmp1, iTmp2, CompareFunc)); //同上

system("pause");
return 0;
}
//输出结果为:
//result:2
// result:1
// result:1

Question12:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int CompareFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 < iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}

typedef int (*PF)(const int &, const int &);
int call(int iTmp1, int iTmp2, PF pFunc )
{
return pFunc(iTmp1, iTmp2);
}


int _tmain(int argc, _TCHAR* argv[])
{


PF pCompare = NULL; //你知道这是可以的,是吧

system("pause");
return 0;
}

Question13:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a[10];
int *p=NULL;
p=&a;//error C2440: '=' : cannot convert from 'int (*)[10]' to 'int *'
system("pause");
return 0;
}

Question14:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a[10];
int *p=NULL;
p=&a[4];
*(p++);
*(++p);
(*p)++; //对一个随机值加1,你知道没什么意思,是吧
*(p--);
printf( "Hello World!\n ");

system("pause");
return 0;
}

Question15:
There are two overloaded versions of operator new and operator delete functions. Each version supports the related new and delete expression:
void *operator new(size_t); // allocate an object
void *operator new[](size_t); // allocate an array
void *operator delete(void*); // free an object
void *operator delete[](void*); // free an array
Question16:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Base
{
public:
virtual void display(std::string strShow= "I am Base class! ")
{
std::cout <<strShow <<std::endl;
}
virtual ~Base(){}
};

class Derive:public Base
{
public:
virtual void display(std::string strShow= "I am Derive class! ")
{
std::cout <<strShow <<std::endl;
}
virtual ~Derive(){}
};


int _tmain(int argc, _TCHAR* argv[])
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase-> display();
pDerive-> display();
delete pBase;
delete pDerive;
system("pause");
return 0;
}
//输出结果为:
//I am Base class!
//I am Derive class!

reasons for this:
Like any other function, a virtual function can have default arguments. As usual, the value, if any, of a
default argument used in a given call is determined at compile time. If a call omits an argument that has
a default value, then the value that is used is the one defined by the type through which the function is
called, irrespective of the object's dynamic type. When a virtual is called through a reference or pointer
to base, then the default argument is the value specified in the declaration of the virtual in the base class. If a virtual is called through a pointer or reference to derived, the default argument is the one declared in the version in the derived class.
Using different default arguments in the base and derived versions of the same virtual is almost guaranteed to cause trouble. Problems are likely to arise when the virtual is called through a reference or pointer to base, but the version that is executed is the one defined by the derived. In such cases, the default argument defined for the base version of the virtual will be passed to the derived version, which was defined using a different default argument.
Question18:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Base
{
public:
Base():a(0)
{

}
virtual ~Base(){}
protected:
int a;
};

class Derived2;

class Derive:public Base
{
friend class Derived2;
public:
void set(int b)
{
a=b;
}
virtual ~Derive(){}
};

class Derived2
{
public:
int get()const
{
return d.a;
}
private:
Derive d;
};

int _tmain(int argc, _TCHAR* argv[])
{
Derived2 d;
Derive d1;
d1.set(18);
cout<<d.get()<<endl;
system("pause");
return 0;
}
//输出:0

Question20:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Base
{
public:
Base()
{
cout<<"I was executed before main"<<endl;
}
};

Base b;

int _tmain(int argc, _TCHAR* argv[])
{
system("pause");
return 0;
}
//输出:
//I was executed before main

Question21:
1.如果从抽象类继承的类也是抽象类,则可以不实现
2.不一定要是空的
Question22:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class A
{
public:
A()
{
cout<<"A"<<endl;
}
};

class B
{
public:
B()
{
cout<<"B"<<endl;
}
};


int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b;
cout<<sizeof(a)<<endl;
cout<<sizeof(b)<<endl;
system("pause");
return 0;
}
//输出:
//A
//B
//1
//1


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class A
{
public:
A()
{
cout<<"A"<<endl;
}
};

class B
{
public:
B()
{
cout<<"B"<<endl;
}
virtual void display()const=0;
};


int _tmain(int argc, _TCHAR* argv[])
{
B b;// error C2259: 'B' : cannot instantiate abstract class
system("pause");
return 0;
}

A derived type must include a declaration for each inherited member it intends to redefine. Our Bulk_item class says that it will redefine the net_price function but will use the inherited version of book.

With one exception, the declaration (Section 7.4, p. 251)of a virtual function in the derived class must exactly match the way the function is defined in the base. That exception applies to virtuals that return a reference (or pointer) to a type that is itself a base class. A virtual function in a derived class can return a reference (or pointer) to a class that is publicly derived from the type returned by the base-class function.
maoxing63570 2010-12-31
  • 打赏
  • 举报
回复
Question1:
As we've seen, each namespace maintains its own scope. As a consequence, functions that are members of two distinct namespaces do not overload one another.
由于没有virtual也就无从谈起覆盖,再说也没有出现继承,那来的覆盖
Quesion2:

#include "stdafx.h"
#include <iostream>
using namespace std;

int myfunc(double da,double db)
{
cout<<"da: "<<da<<"db: "<<db<<endl;
return da>db?da:db;
}

double myfunc(int ia,float fb)
{
cout<<"ia: "<<ia<<"fb: "<<fb<<endl;
return ia>fb?ia:fb;
}

int myfunc(short sa,double db)
{
cout<<"sa: "<<sa<<"db: "<<db<<endl;
return sa>db?sa:db;
}

double myfunc(int ia,double db)
{
cout<<"ia: "<<ia<<"db: "<<db<<endl;
return ia>db?ia:db;
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned short ix=65530;
int a=myfunc(ix,20.0);
system("pause");
return 0;
}
//输出结果为ia: 65530db: 20

Question3:

#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename T>
inline T const & maxj(T const & a, T const & b)
{
return a>b?a:b;
}

int _tmain(int argc, _TCHAR* argv[])
{
maxj(4,7);
maxj(4,4.2); //error C2782: 'const T &maxj(const T &,const T &)' : template parameter 'T' is ambiguous
maxj(static_cast <double> (4),4.2);
maxj<double> (4,4.2);
system("pause");
return 0;
}
//如果是直接拿max做函数名的话,没一个是正确的

Question4:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template <typename T>
inline T max(T a,T b)
{
return a <b?b:a;
}

int _tmain(int argc, _TCHAR* argv[])
{
::max( "apple ", "peach ");
std::string s;
::max( "apple ",s);
system("pause");
return 0;
}

Question5:

#include "stdafx.h"
#include <iostream>
#include <iostream>
class BaseException
{

};
class DerivedException:public BaseException
{

};
void makeExcp()
{
BaseException& excp=DerivedException();
throw excp;
}




int _tmain(int argc, _TCHAR* argv[])
{
try
{
makeExcp();
}
catch(DerivedException&)
{
std::cout << "caught DerivedException " <<std::endl;
}
catch(BaseException&)
{
std::cout << "caught BaseException " <<std::endl;
}
catch(...)
{
std::cout << "caught other Exception " <<std::endl;
}
system("pause");
return 0;
}
//输出:caught BaseException
//reaons for this:
Like a parameter declaration, an exception specifier for a base class can be used to catch an exception object of a derived type. Again, like a parameter declaration, the static type of the exception specifier determines the actions that the catch clause may perform. If the exception object thrown is of derived-class type but is handled by a catch that takes a base-class type,then the catch cannot use any members that are unique to the derived type.

Question6:A using directive may appear in namespace, function, or block scope. It may not appear in a class scope.
using directives不具有传递性
Question7:预编译
Quesition8:正确
Question9:


#include "stdafx.h"
#include <iostream>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
int i=-1;
const int ic=i;
const int *pic=⁣ //error C2440: 'initializing' : cannot convert from 'const int *' to 'int *const '
int * const cpi=⁣
system("pause");
return 0;
}

Question10:

#include "stdafx.h"
#include <iostream>
#include <iostream>

int *func(int, int)
{

return 0;
}

typedef int *(PF)(int, int);

PF getFunc()
{
return func;
}


int _tmain(int argc, _TCHAR* argv[])
{
PF pFunc = getFunc();
system("pause");
return 0;
}
errors:
error C2091: function returns function
1>d:\c++ demo\c plus plus foundation code\csdn测试代码\homework\homework\homework.cpp(24): error C2072: 'pFunc' : initialization of a function
1>d:\c++ demo\c plus plus foundation code\csdn测试代码\homework\homework\homework.cpp(24): error C2205: 'pFunc' : cannot initialize extern variables with block scope
1>d:\c++ demo\c plus plus foundation code\csdn测试代码\homework\homework\homework.cpp(24): error C2440: 'initializing' : cannot convert from 'PF (__cdecl *)' to 'PF'
1> There are no conversions to function types, although there are conversions to references or pointers to functions
食财物权情性 2010-12-31
  • 打赏
  • 举报
回复
楼上的,你把三年半前的贴都扒出来了。。。。。
liugang9931706 2010-12-31
  • 打赏
  • 举报
回复
23题A正确,B是错误的,虽然是虚函数,但是是静态的。
pstdouble 2007-03-21
  • 打赏
  • 举报
回复
努力而缓慢的做着...
jwqu 2007-03-21
  • 打赏
  • 举报
回复
mark
lwfqt 2007-03-21
  • 打赏
  • 举报
回复
mark
trycatch2004 2007-03-20
  • 打赏
  • 举报
回复
明白第1题了,有三个概念:重载、覆盖、隐藏

第1题
namespace IBM
{
int myfunc(int a);
namespace SUN
{
int myfunc(double b);
}
}
应该说是属于“隐藏”吧?!就相当于子类成员函数“隐藏”父类成员函数。
LurkerSky 2007-03-20
  • 打赏
  • 举报
回复
mark先顶再学
theendname 2007-03-20
  • 打赏
  • 举报
回复
mark
trycatch2004 2007-03-19
  • 打赏
  • 举报
回复
没现成答案,就是大家一起来做出答案啊~~
rosetank 2007-03-19
  • 打赏
  • 举报
回复
int main
{
int a[10];
int *p=NULL;
p = a;
if (sizeof(p) != sizeof(a))
选B吧;
return 0;
}
ff5dq4 2007-03-19
  • 打赏
  • 举报
回复
劫分
ytfhwfnh 2007-03-19
  • 打赏
  • 举报
回复
何时公布正确答案?
zbw8080 2007-03-19
  • 打赏
  • 举报
回复
学习,顶一下
sinall 2007-03-19
  • 打赏
  • 举报
回复
欢迎大家指正:

1、既不重载,也不覆盖。
2、B
3、B
4、B
5、B
6、D ?排除法,B是什么意思不明确。
7、A
8、对
9、D const int * const cpi=⁣
10、B
11、E
12、对
13、B 预赋值表达式?int**赋给int*?
14、C ?
15、D
16、C 《Effective C++》 3rd Item 37
17、C 重载,非覆盖
18、错 友元不能继承
19、不能 规定的
20、对
21、C
22、B ?
23、A ?
linguangcan 2007-03-19
  • 打赏
  • 举报
回复
mark
Hemee 2007-03-18
  • 打赏
  • 举报
回复
biaoji!!
yyzhao21 2007-03-18
  • 打赏
  • 举报
回复
RRRR
加载更多回复(9)

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧