Void setVar(float f) {x = f;}
Public void setVar(int f) {x = f;}
Public void setVar(float f) {x = f;}
Public double setVar(float f) {x = f;}
Public final void setVar(float f) {x = f;}
Protected float setVar() {x=3.0f; return 3.0f; }
第1题:
指出下面程序段中的错误,并说明出错原因【 】。
class Location {
int X, Y=20;
protected:
int zeroX, zeroY;
int SetZero(int ZeroX, iht ZeroY);
private:
int length, height;
public:
float radius;
void init(int initX,int initY);
int GetX();
Int GetY();
};
第2题:
下列程序的执行结果是______。
include<iostream.h>
class Student
{
public:
Student(int xx){x=xx;}
virtual float calcTuition( );
protected:
int x;
};
float Studertt::calcTuition( )
{
return float(x*x);
}
class GraduateStudent:public Student
{
public:
GraduateStudent(int xx):Student(xx){}
float calcTuition( );
};
float Graduatestudent::calcTuition( )
{
return float(x*2);
}
void main( )
{
Student s(20);
GraduateStudent gs(30);
cout<<s.calcTuition( )<<" "<<gs.calcTuition( )<<endl;
//计算学生s和研究生gs的学费
}
第3题:
A.private void fun( int n ){ //...}
B.void fun ( int n ){ //... }
C.protected void fun ( int n ) { //... }
D.public void fun ( int n ) { //... }
第4题:
下面程序中错误之处是 ______。
include<iostream.h>
class A{
private:
int xl;
protected:
int x2;
public:
int x3;
};
class B: public A{
private:
int b1;
protected:
int b2;
public:
int b3;
void disp(){cout<<x1<<b2<<end1;} //A
void set(int i){x3=i;} //B
};
void main()
B bb;
bb. a3=10 //C
bb. b3=10 //D
}
第5题:
12 一个给定的数值由左边开始升位到右边第 N 位,如
0010<<1 == 0100
或者
0001 0011<<4 == 0011 0000
请用 C 或者 C++或者其他 X86 上能运行的程序实现。
-----------------------------------------------------------------------------
-----------------------------------
附加题(只有在完成以上题目后,才获准回答)
In C++, what does "explicit" mean? what does "protected" mean?
explicit
C++ Specific
This keyword is a declaration specifier that can only be applied to
in-class constructor declarations. Constructors declared explicit will not
be considered for implicit conversions. For example:
class X {
public:
explicit X(int); //legal
explicit X(double) { //legal // ... }
};
explicit X::X(int) {} //illegal
An explicit constructor cannot take part in implicit conversions. It can
only be used to explicitly construct an object. For example, with the class
declared above:
void f(X) {}
void g(int I)
{
f(i); // will cause error
}
void h()
{
X x1(1); // legal
}
The function call f(i) fails because there is no available implicit
conversion from int to X.
Note It is meaningless to apply explicit to constructors with multiple
arguments, since such constructors cannot take part in implicit conversions.
END C++ Specific
protected
C++ Specific —>
protected: [member-list]
protected base-class
When preceding a list of class members, the protected keyword specifies
that those members are accessible only from member functions and friends of
the class and its derived classes. This applies to all members declared up
to the next access specifier or the end of the class.
When preceding the name of a base class, the protected keyword specifies
that the public and protected members of the base class are protected
members of the derived class.
Default access of members in a class is private. Default access of members
in a structure or union is public.
Default access of a base class is private for classes and public for
structures. Unions cannot have base classes.
For related information, see public, private, friend, and Table of Member
Access Privileges.
END C++ Specific
Example
// Example of the protected keyword
class BaseClass {
protected: int protectFunc();
};
class DerivedClass : public BaseClass
{ public:
int useProtect() { protectFunc(); } // protectFunc accessible from
derived class
};
void main()
{
BaseClass aBase;
DerivedClass aDerived;
aBase.protectFunc(); // Error: protectFunc not accessible
aDerived.protectFunc(); // Error: protectFunc not accessible in derived
class } How do you code an infinite loop in C?
第6题:
已知有下列类的说明,则下列哪个语句是正确的?public class Test { private float f=1.0f; int m=12; static int n=1; public static void main(String arg[]) { Test t= new Test(); }}
A.t.f;
B.this. n
C.Test.m;
D.Test.f;
第7题:
有以下程序: #include <iostream> using namespace std; class A { private: int x,y; public: void set (int i,int j) { x=i; y=j; } int get_y() { return y; } }; class box { private: int length,width; A label; public: void set(int 1,int w, int s,int p) { length=1; width=w; label.set(s,p); } int get_area() { return length*width; } }; int main() { box small; small.set(2,4,1,35); cout<<small.get_area()<<end1; return 0; } 运行后的输出结果是( )。
A.8
B.4
C.35
D.70
第8题:
A.t.f
B.this.n
C.Test.m
D.Test.n
第9题:
若有以下程序: #include <iostream> using namespace std; class A { private: int x; protected: int y; public: int z; void setx(int i) { x=i; } int getx () { return x; }; class B : protected A { public: void setvalue(int a, int b, int c) { setx (a); y=b; z=c; } void display() { cout<<getx ( ) <<", "<<y<<", "<<z<<", "<<end1; } }; int main () { B obj; obj.setvalue(5, 6, 7); obj.display ( ); return 0; } 程序运行后的输出结果是( )。
A.产生语法错误
B.7,6,5
C.5,6,7
D.7,5,6
第10题:
请找出下列程序中错误之处 ______。
#include<iostream.h>
class A{
private:
int x1;
protected:
int x2;
public:
int x3;
};
class B:public A{
private:
int y1;
protected:
int y2;
public:
int y3;
void disp(){cout<<x1<<y1<<end1:} //A
void set(int i) {x2=i;} //B
};
void main() {
B bb;
bb.x3=10; //C
bb.y3=10; //D
}
A.A
B.B
C.C
D.D