复习题
1.以A栏的类为基类,B栏的类采用公有派生还是私有派生更合适。
A | B | |
---|---|---|
class Bear | class PolarBear | 公有派生 |
class Kitchen | class Home | 私有派生 |
class Person | class Programmer | 公有派生 |
class Person | class HorseAndJockey | 私有派生 |
class Person,class Automobile | class Driver | 公有派生,私有派生 |
2.假设有下面的定义:
class Frabjous {
private:
char fab[20];
public:
Frabjous(const char *s ="C++"){strcpy(fab,s);} //书中该行代码在C11标准下无法运行
virtual void tell(){std::cout << fab;}
};
class Gloam{
private:
int glip;
Frabjous fb;
public:
Gloam(int g = 0,const char * s ="C++"):glip(g),fb(s){}
Gloam(int g,const Frabjous & f):glip(g),fb(f){}
void tell();
};
假设Gloam版本的tell()应显示glip和fb的值,请为这3个Gloam方法提供定义。
Frabjous.h
//
// Created by a1358 on 2021/4/8.
//
#ifndef FIRST_FRABJOUS_H
#define FIRST_FRABJOUS_H
#include
#include
using namespace std;
class Frabjous {
private:
char fab[20];
public:
Frabjous(const char *s ="C++"){strcpy(fab,s);}
virtual void tell(){std::cout << fab;}
};
class Gloam{
private:
int glip;
Frabjous fb;
public:
Gloam(int g = 0,const char * s ="C++"):glip(g),fb(s){}
Gloam(int g,const Frabjous & f):glip(g),fb(f){}
void tell();
};
#endif //FIRST_FRABJOUS_H
Frabjous.cpp
//
// Created by a1358 on 2021/4/8.
//
#include "Frabjous.h"
void Gloam::tell() {
cout << "glip: " << glip << endl;
fb.tell();
}
main.cpp
#include
#include "Frabjous.h"
int main() {
Frabjous f1("Python");
Gloam g1(1,f1);
g1.tell();
}
3.假设有下面的定义:
class Frabjous {
private:
char fab[20];
public:
Frabjous(const char *s ="C++"){strcpy(fab,s);} //书中该行代码在C11标准下无法运行
virtual void tell(){std::cout << fab;}
};
class Gloam: private Frabjous{
private:
int glip;
Frabjous fb;
public:
Gloam(int g = 0,const char * s ="C++"):glip(g),fb(s){}
Gloam(int g,const Frabjous & f):glip(g),fb(f){}
void tell();
};
假设Gloam版本的tell()应显示glip和fb的值,请为这3个Gloam方法提供定义。
Frabjous.h
//
// Created by a1358 on 2021/4/8.
//
#ifndef FIRST_FRABJOUS_H
#define FIRST_FRABJOUS_H
#include
#include
using namespace std;
class Frabjous {
private:
char fab[20];
public:
Frabjous(const char *s ="C++"){strcpy(fab,s);}
virtual void tell(){std::cout << fab;}
};
class Gloam: private Frabjous{
private:
int glip;
public:
Gloam(int g = 0,const char *s="C++");
Gloam(int g,const Frabjous & f);
void tell();
};
#endif //FIRST_FRABJOUS_H
Frabjous.cpp
//
// Created by a1358 on 2021/4/8.
//
#include "Frabjous.h"
Gloam::Gloam(int g, const char *s):Frabjous(s){
glip = g;
}
Gloam::Gloam(int g, const Frabjous &f):Frabjous(f){
glip = g;
}
void Gloam::tell() {
cout << "glip: " << glip << endl;
Frabjous::tell();
}
main.cpp
#include
#include "Frabjous.h"
int main() {
Frabjous f1("Python");
Gloam g1(1,f1);
g1.tell();
cout << endl;
Gloam g2;
g2.tell();
}
4.假设有下面的定义,它是基于程序清单14.13中的Stack模板和程序清单14.10中的Worker类的:
Stack sw;
请写出将生成的类声明。只实现类声明,不实现非内联类方法。
Stack_Worker.h
//
// Created by a1358 on 2021/4/8.
//
#ifndef STACK_WORKER_STACK_WORKER_H
#define STACK_WORKER_STACK_WORKER_H
#include
#include
class Worker // an abstract base class
{
private:
std::string fullname;
long id;
public:
Worker() : fullname("no one"), id(0L) {}
Worker(const std::string & s, long n)
: fullname(s), id(n) {}
~Worker(){};
void Set(std::string st,long ID){fullname=st,id=ID;}
void Show() {std::cout << "fullname: " << fullname <<
" " <<"id: " << id << '\n';};
};
template
class Stack: private Worker
{
private:
enum {MAX = 10}; // constant specific to class
Type items[MAX]; // holds stack items
int top; // index for top stack item
public:
Stack();
bool isempty();
bool isfull();
bool push(const Type & item); // add item to stack
bool pop(Type & item); // pop top into item
};
template
Stack::Stack()
{
top = 0;
}
template
bool Stack::isempty()
{
return top == 0;
}
template
bool Stack::isfull()
{
return top == MAX;
}
template
bool Stack::push(const Type & item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
template
bool Stack::pop(Type & item)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
#endif //STACK_WORKER_STACK_WORKER_H
main.cpp
#include
#include "Stack_Worker.h"
using namespace std;
int main() {
Stack sw;
Worker *w = new Worker[3];
w[0] = Worker("Tom",1);
w[1] = Worker("John",2);
w[2] = Worker("Daming",3);
cout << sw.isempty() << endl;
for(int i = 0;i < 3;i++)
{
sw.push(&w[i]);
}
cout << sw.isfull() << endl;
for(int i = 0;i < 3;i++)
{
sw.pop(w);
}
cout << sw.isempty() << endl;
}
5.使用本章中的模板定义对下面的内容进行定义:
-
String 对象数组;
valarray
a1; -
double数组栈;
Stack
s1; -
指向Worker对象的指针的栈数组。
Stack
sw;
程序清单14.18生成了多少个模板类定义
4个
6.指出虚基类和非虚基类之间的区别
如果基类是虚基类,派生类将包括基类的一个子对象;
如果基类不是虚基类,派生类将包含多个子对象。