(11)有如下类定义:
class Sample{
public:
Sample();
~Sample();
Private:
Static int date;
};
将静态数据成员data初始化为0的语句是 【11】 。
(12)“图形”类Shape中定义了纯虚函数CalArea(),“三角形”类Triangle继承了类Shape,请将Triangle类中的CalArea函数补充完整。
class Shape{
public:
virtual int CalArea()=0;
}
class Triangle: public Shape{
public:
Triangle{int s, int h}: side(s),height(h) {}
【12】 { return side*height/2 ; }
private:
int side;
int height;
};
(13)有如下程序:
#include <iostream>
using namespace std;
class GrandChild{
public:
GrandChild(){ strcpy (name,”Unknown”); }
const char * getName()const { return name; }
virtual char * getAddress()const=0;
private:
char name[20];
};
class GrandSon : public GrandChild{
public:
GrandSon{char *name} {}
Char * getAddress() const { return “Shanghai”; }
};
int main(){
GrandChild *gs=new GrandSon(“Feifei”);
cout<<gs->getName()<<”住在”<<gs->getAddress()<<endl;
delete gs;
return 0;
}
运行时的输出结果是 【13】 。
(14)如下程序定义了“单词”类word,类中重载了<运算符,用于比较“单词”的大小,返回相应的逻辑值。程序的输出结果为:After Sorting: Happy Welcome,请将程序补充完整。
#include <iostream>
#include <string>
using namespace std;
class Word{
public:
Word(string s) : str(s) { }
string getStr(){ return str; }
【14】 const { return (str<w.str); }
friend ostream& operator << (ostream& output, const Word &w)
{ output<<w.str; return output; }
private:
string str;
};
Int main(){
Word w1(“Happy”),w2(“Welcome”);
Cout<<”After sorting: “;
if(w1<w2) cout<<w1<<’ ’<<w2;
else cout<<w2<<’ ‘<<w1;
return 0;
}
(15)请将下列模板类Data补充完整。
template <typename T>
class Data{
public:
void put (T v) { val=v; }
【15】 get() //返回数据成员val的值,返回类型不加转换
{ return val; }
private:
T val;
};
上一页 [1] [2] [3] [4] [5] [6] [7] 下一页