文章目录

  • 第五章 继承类:派生类
  • 第六章 运算符重载
  • 第七章 多态性和虚函数
  • 第八章 模板

第五章 继承类:派生类

1.在protected保护继承中,对于垂直访问等同于公有继承,对于水平访问等同于私有继承。T
2.下列程序的执行结果为 C

#include <iostream>
using namespace std;class A {
public:A() {     cout << "1";    }~A() {    cout << "2";    }
};
class B: public A {
public:B() {    cout << "3";    }~B() {    cout << "4";    }
};
int main() {B b;return 0;
}

A.1234
B.1324
C.1342
D.3142
3.下列关于派生类构造函数和析构函数的说法中,错误的是 D

A. 派生类的构造函数会隐含调用基类的构造函数
B. 如果基类声明了带有形参表的构造函数,则派生类就应当声明构造函数
C. 在建立派生类对象时,先调用基类的构造函数,再调用派生类的构造函数
D. 在销毁派生类对象时,先调用基类的析构函数,再调用派生类的析构函数

4.建立派生类对象时, 3种构造函数分别是a(基类的构造函数)、b(成员对象的构造函数)、c(派生类的构造函数),这3种构造函数的调用顺序为 A

A. abc
B. acb
C. cab
D. cba

5.从shape类派生出一个直角三角形类RTriangle
从shape类派生出一个直角三角形类RTriangle类(regular triangle)。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积

virtual double getPerimeter()=0; // 求周长};
直角三角形类名:RTriangle
直角三角形类的构造函数原型如下:
RTriangle(double a, double b);
其中 a 和 b 都是直角三角形的两条直角边。
裁判测试程序样例:
#include
#include
using namespace std;

class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
//你提交的代码将嵌入到这里

int main()
{
double a,b;
cin>>a>>b;
RTriangle t(a,b);
cout<<t.getArea()<<endl;
cout<<t.getPerimeter()<<endl;
}

输入样例:
在这里填写一组输入
3 4

输出样例:
在这里填写相应的输出
12
12

class RTriangle:public shape {private:double height;double width;public:RTriangle(double a, double b){height = a;width = b;}~RTriangle(){}double getArea(){return 0.5 * height * width;}double getPerimeter(){return height + width + sqrt(height * height + width * width);}
};

6.从shape类派生出一个圆形类Circle
请从下列shape类派生出一个圆形类Circle,这个类圆形的半径radius作为私有成员,类中应包含初始化半径的构造函数。圆周率PI取3.1415926。
class shape {
public:
double getArea(){ return -1; };
double getPerimeter(){ return -1 };
}

圆形类名Circle

裁判测试程序样例:
#include
using namespace std;

class shape {
public:
double getArea(){ return -1 };// 求面积
double getPerimeter(){ return -1 }; // 求周长
};

/* 你提交的代码将被嵌入到这里 */

int main() {
double r;
cin>>r;
Circle c®;
cout<<c.getArea()<<endl;
cout<<c.getPerimeter()<<endl;
return 0;
}
/* 请在这里填写答案 */

输入样例:
在这里填写一组输入
2.78

输出样例:
在这里填写相应的输出
24.2795
17.4673

const double PI = 3.1415926;
class Circle:public shape {private:double r;public:Circle(double radius) {r = radius;}double getArea() {return PI * r * r;}double getPerimeter(){return 2 * PI * r;}
};

7.计算圆柱体的表面积(函数名隐藏)
Cylinder类是Circle类的派生类,在下面的程序中计算并输出了圆柱体的表面积。请阅读理解下面的程序。将下面的Cylinder类补充完整后提交。
Cylinder类的定义:
class Cylinder :public Circle{
double height;
public:

};

你提交的Cylinder类的定义将嵌入到下面的程序中:
#include
using namespace std;
const double PI=3.1415926;
class Circle{
protected:
double radius;
public:
Circle(double r){
radius=r;
}
double getArea(){
return PIradiusradius;
}
};
// 你提交的代码将嵌入在这里

int main()
{
double r,h;
cin>>r>>h;
Cylinder Cy1(r,h) ;
cout<<Cy1.getArea()<<endl;
return 0;
}

输入样例:
3.5 4.2

输出样例:
106.369

class Cylinder:public Circle{double height;  public:Cylinder(double radius,double h):Circle(radius) {height = h;}~Cylinder(){}double getArea(){return PI * radius * radius * 2 + height * PI * radius * 2;}
};

8.狗的继承
完成两个类,一个类Animal,表示动物类,有一个成员表示年龄。一个类Dog,继承自Animal,有一个新的数据成员表示颜色,合理设计这两个类,使得测试程序可以运行并得到正确的结果。
函数接口定义:
按照要求实现类
裁判测试程序样例:
/* 请在这里填写答案 */

int main(){
Animal ani(5);
cout<<“age of ani:”<<ani.getAge()<<endl;
Dog dog(5,“black”);
cout<<“infor of dog:”<<endl;
dog.showInfor();
}

输入样例:

输出样例:
age of ani:5
infor of dog:
age:5
color:black

#include<iostream>
#include<cstring>
using namespace std;
class Animal
{protected:int age;public:Animal(int age1) {age = age1;}~Animal(){}int getAge() {return age;}
};
class Dog:public Animal{private:char color[10];int age1;public:Dog(int age1, char color1[]):Animal(age1){age = age1;strcpy(color, color1);}~Dog(){};void showInfor(){cout << "age:" << getAge() << endl;cout << "color:" << color << endl;}
};

9.面向对象程序设计的继承性鼓励程序员重用被实践验证的高质量软件。T
10.一个类的私有成员 B

A. 只能被该类的成员函数访问
B. 只能被该类的成员函数和友元函数访问
C. 只能被该类的成员函数、友元函数和派生类访问
D. 以上答案都不对
11.在公有继承的情况下,在派生类中能够访问的基类成员包括 D

A. 公有成员
B. 保护成员
C. 公有成员、保护成员和私有成员
D. 公有成员和保护成员
12.派生类继承基类的方式有 D

A. public
B. private
C. protected
D. 以上都对
13.假设在公有派生情况下,以下说法不正确的是 A

A. 可以将基类对象复制给派生类对象
B. 可以将派生类对象的地址复制给基类指针
C. 可以将派生类对象赋值给基类的引用
D. 可以将派生类对象赋值给基类对象
14.从shape类派生出一个正六边形类RHexagon
从下列的抽象类shape类派生出一个正六边形(regular hexagon)RHexagon类。RHexagon类将正六边形的边长作为私有成员,类中包含初始化这个值的构造函数。
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积

virtual double getPerimeter()=0; // 求周长};

正六边形类名:
RHexagon

裁判测试程序样例:
#include
#include
using namespace std;

class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
//你提交的代码将嵌入到这里

int main()
{
double s;
cin>>s;
RHexagon p(s);
cout<<p.getArea()<<endl;
cout<<p.getPerimeter()<<endl;
}

输入样例:
16.8

输出样例:
733.281
100.8

class RHexagon:public shape {private :double length;public:RHexagon(double r):length(r){} double getArea(){return 3*sqrt(3)*length*length/2;}double getPerimeter(){return 6*length;}
};

15.求平面几何形状的面积(圆形和矩形)C++
下列shape类是一个表示形状的基类,该类有一个计算形状面积的方法area( )。请以形状类"Shape"为基类派生出圆形类"Circle"和矩形类"Rectangle",要求保持求面积的接口不变。从键盘输入圆形的半径和矩形的长和宽,在主函数中创建一个圆形类"Circle"的对象和一个矩形类"Rectangle"的对象,计算并输出这个圆形和矩形的面积。圆周率PI=3.14
class shape{
public:
double area( ){ }
};

圆形类名:Circle 注意:圆周率PI=3.14
矩形类名:Rectangle

裁判测试程序样例:
#include < iostream >
using namespace std;
class Shape{
public:
double area ( ){ return -1; }
};
//你提交的代码将嵌入到这里。

int main()
{
double r,h,w;
cin>>r>>h>>w;
Circle c®; Rectangle rect(h,w);
cout<<"The area of the Circle is "<<c.area( )<<endl;
cout<<"The area of the Rectangle is "<<rect.area( )<<endl;
return 0;
}

输入样例:
8.8
16.8 9.8

输出样例:
The area of the Circle is 243.162
The area of the Rectangle is 164.64

class Circle:public Shape {private:double radius;public:Circle(double r):radius(r) {}double area(){return 3.14 * radius * radius;}
};
class Rectangle:public Shape {private:double width;double length;public:Rectangle(double w, double l):width(w), length(l) {}double area(){return width * length;}
};

16.学生CPP成绩计算
给出下面的人员基类框架:
class Person
{
protected:
string name;
int age; public:
Person();
Person (string p_name, int p_age);
void display () {cout<<name<<“:”<<age<<endl;}};
建立一个派生类student,增加以下成员数据:
int ID;//学号
float cpp_score;//cpp上机成绩
float cpp_count;//cpp上机考勤
float cpp_grade;//cpp总评成绩
//总评成绩计算规则:cpp_grade = cpp_score * 0.9 + cpp_count * 2;增加以下成员函数:
student类的无参构造函数
student类的参数化构造函数//注意cpp_grade为上机成绩和考勤的计算结果
void print()//输出当前student的信息
//其中cpp_grade输出保留一位小数
//输出格式为ID name cpp_grade生成上述类并编写主函数,根据输入的学生基本信息,建立一个学生对象,计算其cpp总评成绩,并输出其学号、姓名、总评成绩。
输入格式: 测试输入包含若干测试用例,每个测试用例占一行(学生姓名 学号 年龄 cpp成绩 cpp考勤)。当读入0时输入结束,相应的结果不要输出。
输入样例:
Bob 10001 18 75.5 4
Mike 10005 17 95.0 5
0
输出样例:
10001 Bob 75.9
10005 Mike 95.5

#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
class Person
{protected:string name;int age;public:Person(){}      Person (string p_name, int p_age) {name = p_name; age = p_age;}~Person(){ }void display () {cout << name << ":" << age << endl;}
};
class student:public Person
{   private:int ID;    //学号float cpp_score;  //cpp上机成绩float cpp_count; //cpp上机考勤float cpp_grade; //cpp总评成绩public: //总评成绩计算规则:cpp_grade = cpp_score * 0.9 + cpp_count * 2;student(){}student(string n,int i,int a,float p,float q):Person(n,a) {name = n;ID = i;age = a;cpp_score = p;cpp_count = q;cpp_grade = p * 0.9 + q * 2;}~student(){}void print(){cout << ID << " " << name << " " << setiosflags(ios::fixed) << setprecision(1) << cpp_grade << endl;}
};
int main() {  int a, i;string n;float p, q;while(1) {   cin>>n;if(n == "0") return 0;else {cin >> i;cin >> a;cin >> p;cin >> q;student s(n, i, a, p, q); s.print();}
}return 0;
}

17.在C++语言中设置虚基类的目的是( C ) 。

A. 简化程序代码
B. 提高程序的运行效率
C. 解决多继承造成的二义性问题
D. 缩短程序的目标代码

18.下列关于继承的描述中,错误的是( D )。

A. 析构函数不能被继承
B. 派生类是基类的组合
C. 派生类的成员除了它自己的成员外,还包含了它的基类的成员
D. 派生类中继承的基类成员的访问权限到派生类保持不变

19.In a(n) is-a relationship, an object of a derived class also can be treated as an object of its base class.

20.A base class’s protected members can be accessed only in the base-class definition or in derived-class definitions.

21.写出派生类构造方法(C++)
裁判测试程序样例中展示的是一段定义基类People、派生类Student以及测试两个类的相关C++代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。
函数接口定义:
提示:
观察类的定义和main方法中的测试代码,补全缺失的代码。

裁判测试程序样例:
注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关函数。
#include
using namespace std;
class People{
private:
string id;
string name;
public:
People(string id, string name){
this->id = id;
this->name = name;
}
string getId(){
return this->id;
}
string getName(){
return name;
}
};
class Student : public People{
private:
string sid;
int score;
public:
Student(string id, string name, string sid, int score)
/** 你提交的代码将被嵌在这里(替换此行) **/
}
friend ostream& operator <<(ostream &o, Student &s);
};
ostream& operator <<(ostream &o, Student &s){
o << “(Name:” << s.getName() << “; id:”
<< s.getId() << “; sid:” << s.sid
<< “; score:” << s.score << “)”;
return o;
}
int main(){
Student zs(“370202X”, “Zhang San”, “1052102”, 96);
cout << zs << endl;
return 0;
}

输入样例:
(无)

输出样例:
(Name:Zhang San; id:370202X; sid:1052102; score:96)

:People(id,name),sid(sid),score(score){

22.7-1 日程安排(多重继承+重载) (12 分)已有一个日期类Date,包括三个protected成员数据
int year;
int month;
int day;
另有一个时间类Time,包括三个protected成员数据
int hour;
int minute;
int second;
现需根据输入的日程的日期时间,安排前后顺序,为此以Date类和Time类为基类,建立一个日程类Schedule,包括以下新增成员:
int ID;//日程的ID
bool operator < (const Schedule & s2);//判断当前日程时间是否早于s2
生成以上类,并编写主函数,根据输入的各项日程信息,建立日程对象,找出需要最早安排的日程,并输出该日程对象的信息。
输入格式: 测试输入包含若干日程,每个日程占一行(日程编号ID 日程日期(**//)日程时间(::))。当读入0时输入结束,相应的结果不要输出。
输入样例:
1 2014/06/27 08:00:01
2 2014/06/28 08:00:01
0
输出样例:
The urgent schedule is No.1: 2014/6/27 8:0:1

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
class Date {protected:int year;int month;int day;public:Date(int _y, int _m, int _d):year(_y), month(_m), day(_d){}
};
class Time {protected:int hour;int minute;int second;public:Time(int _h, int _m, int _s):hour(_h), minute(_m), second(_s){}
};
class Schedule:public Date, public Time {private:int ID;public:Schedule():Date(0,0,0), Time(0,0,0), ID(0){}Schedule(int _id, int _year, int _month, int _day, int _hour, int _minute, int _second):Date(_year, _month, _day), Time(_hour, _minute, _second), ID(_id){}bool operator < (const Schedule &s) {char timeArr1[15], timeArr2[15];sprintf(timeArr1, "%04d%02d%02d%02d%02d%02d", year, month, day, hour, minute, second);sprintf(timeArr2, "%04d%02d%02d%02d%02d%02d", s.year, s.month, s.day, s.hour, s.minute, s.second);return atof(timeArr1) < atof(timeArr2);}void show() {cout << "The urgent schedule is No."<<ID<<": "<<year<<"/"<<month<<"/"<<day<<" "<<hour<<":"<<minute<<":"<<second<<endl;}
};
int main() {int id, rows = 0;int y, m, d, h, i, s;Schedule min;while (cin >> id) {if(id == 0) break;scanf("%d/%d/%d %d:%d:%d", &y, &m, &d, &h, &i, &s);Schedule cur(id, y, m, d, h, i, s);if(rows++ == 0) min = cur;if(cur < min) min = cur;}if(rows != 0) min.show();return 0;
}

第六章 运算符重载

1.对每个可重载的运算符来讲,它既可以重载为友元函数,又可以重载为成员函数,还可以重载为非成员函数。F
2.对单目运算符重载为友元函数时,可以说明一个形参。而重载为成员函数时,不能显式说明形参。T
3.重载operator+时,返回值的类型应当与形参类型一致。
比如以下程序中,operator+的返回值类型有错:F

class A {int x;public:A(int t=0):x(t){   }int operator+(const A& a1){ return x+a1.x;  }};

4.In C++, only existing operators can be overloaded. T
5.下列运算符中,(C )运算符不能重载。

A. &&
B. [ ]
C. ::
D. <<
6.下列关于运算符重载的描述中,(D )是正确的。

A. 运算符重载可以改变操作数的个数
B. 运算符重载可以改变优先级
C. 运算符重载可以改变结合性
D. 运算符重载不可以改变语法结构

7.使用成员函数重载复数类的运算符+
类Complex声明了一个复数类,有两个数据成员realPart(代表复数的实部)和imgPart(代表复数的虚部),并定义了成员函数实现了重载运算符“+”以实现两个复数对象的相加操作。成员函数Show用来输出复数的实部和虚部。请完成对运算符“+”的重载操作。
函数接口定义:
Complex& Complex::operator+(Complex& com);

参数com为复数类Complex的对象的引用,函数的返回值为当前对象与com对象相加后的值。
裁判测试程序样例:
#include
using namespace std;

class Complex {
public:
Complex(double realPart = 0, double imgPart = 0) {
this->realPart = realPart;
this->imgPart = imgPart;
}
Complex& operator+(Complex& com);
void Show() {
cout << realPart << " " << imgPart << endl;
}
private:
double realPart, imgPart;
};
int main() {
int r1, i1; //第1个复数对象的实部和虚部
int r2, i2; //第1个复数对象的实部和虚部
cin >> r1 >> i1;
cin >> r2 >> i2;
Complex c1(r1, i1); //构造第1个复数对象c1
Complex c2(r2, i2); //构造第2个复数对象c2
c1 = c1 + c2;
c1.Show();
return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:
3 4
10 20

输出样例:
13 24

Complex  &Complex :: operator+(Complex& com) {realPart += com.realPart;imgPart += com.imgPart;return *this;
}

8.单目运算符重载(时钟类)
本题已给出时钟类及其成员函数实现,要求补充完整运算符++重载函数(前置和后置),使之能够实现时钟对象自增1秒。
时钟类定义如下:
class Clock {
public:
Clock(int NewH=0, int NewM=0, int NewS=0);
void ShowTime();
friend Clock operator++(Clock& op); //前置单目运算符重载
friend Clock operator++(Clock& op,int); //后置单目运算符重载
private:
int Hour, Minute, Second;
};

裁判测试程序样例:
#include
using namespace std;

class Clock {
public:
Clock(int NewH=0, int NewM=0, int NewS=0);
void ShowTime();
friend Clock operator++(Clock& op); //前置单目运算符重载
friend Clock operator++(Clock& op,int); //后置单目运算符重载
private:
int Hour, Minute, Second;
};

Clock::Clock(int NewH, int NewM, int NewS) {
Hour=NewH;
Minute=NewM;
Second=NewS;
}
void Clock::ShowTime() {
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}

/---------请在这里填写答案-----------/

int main() {
int h, m, s;
cin>>h>>m>>s;
Clock a(h,m,s);
(++a).ShowTime();
(a++).ShowTime();
a.ShowTime();
return 0;
}

输入样例:
在这里给出一组输入。例如:
10 10 10

输出样例:
在这里给出相应的输出。例如:
10:10:11
10:10:11
10:10:12

Clock operator ++(Clock &op) {op.Second++;if (op.Second >= 60) {op.Second = op.Second - 60;op.Minute++;if (op.Minute >= 60) {op.Minute = op.Minute - 60;op.Hour++;op.Hour = op.Hour % 24;}}return op;
}
Clock operator ++(Clock &op, int) {Clock OP = op;op.Second++;if (op.Second >= 60) {op.Second = op.Second - 60;op.Minute++;if (op.Minute >= 60) {op.Minute = op.Minute - 60;op.Hour++;op.Hour = op.Hour % 24;}}return OP;
}

9.多数运算符可以重载,个别运算符不能重载,运算符重载是通过函数定义实现的。T
10.为了能出现在赋值表达式的左右两边,重载的"[]"运算符应定义为: B

A. A operator [ ] (int);
B. A& operator [ ] (int);
C. const A operator [ ] (int);
D. 以上答案都不对

11.在C++中不能重载的运算符是 A

A. ?:
B. +
C. -
D. <=

12.对学生对象按照成绩升序排序
下面这段代码要实现对学生对象按照成绩升序排序。
仔细阅读代码,要求实现编程实现输出运算符“<<”和小于“<”运算符,使本程序能完成预定的排序功能。
裁判测试程序样例:
#include
#include
#include
using namespace std;

class Student {
string name;
char sex;
int score;
string grade;

public:
Student(string name, char sex, int score, string grade);
friend ostream &operator<< (ostream& os, Student st) ;
friend bool operator<(Student &st1, Student &st2);
};
//你提交的代码将被嵌入到这里

Student::Student(string name, char sex, int score, string grade) {
this->name = name;
this->sex = sex;
this->score = score;
this->grade = grade;
}

int main() {
list st;
string name, grade;
char sex; int score;

for(int i = 0; i < 5; i++) {
cin>>name; cin>>sex;
cin>>score; cin>>grade;
st.push_back(Student(name, sex, score, grade));
}

st.sort();

list::iterator p = st.begin();
while(p != st.end()) {
cout<<*p;
p++;
}
return 0;
}

输入样例:
Bill M 86 JK1501
David M 98 JK1502
Mary F 78 JK1503
Adam M 83 JK1504
Rose F 96 JK1505

输出样例:
Mary F 78 JK1503
Adam M 83 JK1504
Bill M 86 JK1501
Rose F 96 JK1505
David M 98 JK1502

ostream &operator<< (ostream& os, Student st) {os << st.name << " " << st.sex << " " << st.score << " " << st.grade << endl;
}
bool operator<(Student &st1, Student &st2) {if(st1.score < st2.score) {return true;} else {return false;}
}

13.重载下标运算符[ ]
这段程序实现了安全数组。
请认真阅读程序并补全程序使之能正确执行。
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include
#include
using namespace std;
const int SIZE = 3;

class atype {
int a[SIZE];
public:
atype( ) {
register int i;
for(i=0; i<SIZE; i++) a[i] = i;
}
int &operator[](int i);
};
//你提交的代码将被嵌入到这里

int main( )
{
atype ob;
cin >> ob[1];
ob[2] = ob[1]; // 下标运算符[]出现在赋值运算符的左边和右边
cout << ob[2];
ob[3] = 44; // 产生运行时错误,下标3超出了数组边界
return 0;
}

输入样例:
98

输出样例:
98
Index value of 3 is out-of-bounds.

int &atype :: operator[](int i) {if (i < 0 || i > SIZE) {cout << endl;cout << "Index value of 3 is out-of-bounds.";exit(0);}return a[i];
}

14.复数类重载加法、减法和乘法运算符
以下定义了一个复数类及其部分实现,现要求将类的构造函数以及运算符+、- 和 * 函数重载补充完整。
复数类定义:
在这里描述复数类定义。具体如下:
class complex {
public:
complex(float r=0,float i=0); // 构造函数
complex operator+(const complex &op2) const; //重载运算符 +
complex operator-(const complex &op2) const; //重载运算符 -
complex operator*(const complex &op2) const; //重载运算符 *
void display() const; // 按数学写法输出复数
private:
float real;
float imag;
};

裁判测试程序样例:
在这里给出复数类测试的例子。例如:
#include
using namespace std;
class complex {
public:
complex(float r=0,float i=0); // 构造函数
complex operator+(const complex &op2) const; //重载运算符 +
complex operator-(const complex &op2) const; //重载运算符 -
complex operator*(const complex &op2) const; //重载运算符 *
void display() const; // 按数学写法输出复数
private:
float real;
float imag;
};

/* ------------------- 请在这里填写答案-------------------- */

void complex::display() const {
if(real&&imag)
if(imag == 1 || imag == -1)
cout<<real<<(imag>0?"+":"-")<<“i”<<endl;
else
cout<<real<<(imag>0?"+":"")<<imag<<“i”<<endl;
else if(real)
cout<<real<<endl;
else if (imag)
if(imag == 1||imag == -1)
cout<<(imag>0?"":"-")<<“i”<<endl;
else
cout<<imag<<“i”<<endl;
else
cout<<0<<endl;
}

int main() {
double real,imag;
complex c,d,e;
cin>>real>>imag;
complex c1(real,imag);
cin>>real>>imag;
complex c2(real,imag);
c=c1+c2;
d=c1-c2;
e=c1*c2;
c.display() ;
d.display() ;
e.display();
return 0;
}

输入样例:
在这里给出一组输入。例如:
2 3
-4 -5

输出样例:
在这里给出相应的输出。例如:
-2-2i
6+8i
7-22i

complex :: complex(float r, float i) {real = r;imag = i;
}
complex  complex :: operator+(const complex &op2) const {complex Complex;Complex.real = op2.real + real;Complex.imag = op2.imag + imag;return Complex;
}
complex  complex :: operator-(const complex &op2) const {complex Complex;Complex.real = real - op2.real ;Complex.imag =  imag - op2.imag;return Complex;
}
complex  complex :: operator*(const complex &op2) const {complex Complex;Complex.real = real * op2.real - imag * op2.imag;Complex.imag =  imag * op2.real + real * op2.imag;return Complex;
}

第七章 多态性和虚函数

1.虚函数是用virtual 关键字说明的成员函数。 T
2.动态绑定是在运行时选定调用的成员函数的。 T
3.构造函数可以声明为虚函数。F
4.构造函数可以声明为纯虚函数。F
5.虚函数不能是类的静态成员。T
6.重定义虚函数的派生类必须是公有继承的。T
7.作为虚函数隐含参数的this指针,决定了虚函数调用时执行的代码。T

8.关于纯虚函数和抽象类的描述中,(C )是错误的。

A. 纯虚函数是一种特殊的虚函数,它没有具体的实现
B. 抽象类是指具有纯虚函数的类
C. 一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类
D. 抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出

9.虚析构函数的作用是。 C

A. 虚基类必须定义虚析构函数
B. 类对象作用域结束时释放资源
C. delete动态对象时释放资源
D. 无意义

10.在派生类中,重载一个虚函数时,要求函数名、参数的个数、参数的类型、参数的顺序和函数的返回值。 A

A. 相同
B. 不同
C. 相容
D. 部分相同

11.若一个类中含有纯虚函数,则该类称为。 C

A. 基类
B. 纯基类
C. 抽象类
D. 派生类

12.假设 Aclass为抽象类,下列正确的说明语句是。 B

A. Aclass fun( int ) ;
B. Aclass * p ;
C. int fun( Aclass ) ;
D. Aclass Obj ;

13.关于动态绑定的下列描述中,(D )是错误的。

A. 动态绑定是以虚函数为基础的
B. 动态绑定在运行时确定所调用的函数代码
C. 动态绑定调用函数操作是通过指向对象的指针或对象引用来实现的
D. 动态绑定是在编译时确定操作函数的

14.汽车收费
现在要开发一个系统,管理对多种汽车的收费工作。
给出下面的一个基类框架
class Vehicle
{
protected:
string NO;public:
Vehicle(string n){
NO = n;
}
virtual int fee()=0;//计算应收费用
};
以Vehicle为基类,构建出Car、Truck和Bus三个类。
Car的收费公式为: 载客数8+重量2
Truck的收费公式为:重量5
Bus的收费公式为: 载客数
3
生成上述类并编写主函数
主函数根据输入的信息,相应建立Car,Truck或Bus类对象,对于Car给出载客数和重量,Truck给出重量,Bus给出载客数。假设载客数和重量均为整数
输入格式:第一行输入测试用例数。接着每个测试用例占一行,每行给出汽车的基本信息,第一个数据为当前汽车的类型:1为car,2为Truck,3为Bus。第二个数据为它的编号,接下来Car是载客数和重量,Truck要求输入重量,Bus要求输入载客数。
要求输出各车的编号和收费。
裁判测试程序样例:
#include
#include
using namespace std;
class Vehicle
{
protected:
string NO;//编号
public:
Vehicle(string n){ NO = n; }
virtual int fee()=0;//计算应收费用
};

/* 请在这里填写答案 */

int main()
{
Car c("",0,0);
Truck t("",0);
Bus b("",0);
int i, repeat, ty, weight, guest;
string no;
cin>>repeat;
for(i=0;i<repeat;i++){
cin>>ty>>no;
switch(ty){
case 1: cin>>guest>>weight; c=Car(no, guest, weight); cout<<no<<’ ‘<<c.fee()<<endl; break;
case 2: cin>>weight; t=Truck(no, weight); cout<<no<<’ ‘<<t.fee()<<endl; break;
case 3: cin>>guest; b=Bus(no, guest); cout<<no<<’ '<<b.fee()<<endl; break;
}
}
return 0;
}

输入样例:
4
1 002 20 5
3 009 30
2 003 50
1 010 17 6

输出样例:
002 170
009 90
003 250
010 148

class Car:public Vehicle{
public:int guest;int weight;Car(string Number,int g,int w):Vehicle(Number){guest = g;weight = w;}int fee(){return guest*8+weight*2;}
};
class Truck:public Vehicle{
public:int weight;Truck(string Number,int w):Vehicle(Number){weight = w;}int fee(){return weight * 5;}
};
class Bus:public Vehicle{
public:int guest;Bus(string Number,int g):Vehicle(Number){guest = g;}int fee(){return guest * 3;}
};

15.抽象类Shape
请编写一个抽象类Shape,包括两个纯虚函数,分别为计算面积getArea()和计算周长getPerim()。通过Shape类派生出矩形类Rectangle和圆类Circle,并计算各自的面积和周长。
测试用例具体要求:输入1表示测试矩形类,之后输入矩形长和宽。输入2表示测试圆类,之后输入圆半径。
Shape类定义如下:
class Shape {
public:
virtual double getArea()=0;
virtual double getPerim()=0;
};

裁判测试程序样例:
#include
using namespace std;
const double PI=3.14;

class Shape {
public:
virtual double getArea()=0;
virtual double getPerim()=0;
};

/* ------请在这里填写答案 ------*/

int main() {
Shape *p;
int n;
double w,h,r;
scanf("%d",&n);
switch(n) {
case 1: {
cin>>w>>h;
Rectangle rect(w,h);
cout<<“area=”<<rect.getArea()<<endl;
cout<<“perim=”<<rect.getPerim()<<endl;
break;
}
case 2: {
cin>>r;
Circle c®;
cout<<“area=”<<c.getArea()<<endl;
cout<<“perim=”<<c.getPerim()<<endl;
break;
}
}
return 0;
}

输入样例1:
在这里给出一组输入。例如:
1
4 5

输出样例1:
在这里给出相应的输出。例如:
area=20
perim=18

输入样例2:
在这里给出一组输入。例如:
2
5

输出样例2:
在这里给出相应的输出。例如:
area=78.5
perim=31.4

class Rectangle : public Shape{
private:double width;double height;
public:Rectangle(double w, double h){width = w;height = h;}double getArea(){return width * height;}double getPerim(){return (width + height) * 2;}
};
class Circle : public Shape{
private:double radius;
public:Circle(double r){radius = r;}double getArea(){return PI * radius * radius;}double getPerim(){return (PI * radius) * 2;}
};

16.虚函数的应用
补充下列代码,使得程序的输出为:

A:3

A:15

B:5

3

15

5
类和函数接口定义:
参见裁判测试程序样例中的类和函数接口。

裁判测试程序样例:
#include
using namespace std;
class CMyClassA {
int val;
public:
CMyClassA(int);
void virtual print();
};
CMyClassA::CMyClassA(int arg) {
val = arg;
printf(“A:%d\n”, val);
}
void CMyClassA::print() {
printf("%d\n", val);
return;
}

/* 在这里填写代码 */

int main(int argc, char** argv) {
CMyClassA a(3), *ptr;
CMyClassB b(5);
ptr = &a;
ptr->print();
a = b;
a.print();
ptr = &b;
ptr->print();
return 0;
}

输入样例:
None

输出样例:
A:3
A:15
B:5
3
15
5

来源:
openjudge.cn

class CMyClassB :public CMyClassA {public:int val2;CMyClassB(int x) :CMyClassA(3 * x), val2(x) {printf("B:%d\n", val2);}void print() {printf("%d\n", val2);}
};

17.关于虚函数的描述中,(C )是正确的。

A. 虚函数是一个static 类型的成员函数
B. 虚函数是一个非成员函数
C. 基类中说明了虚函数后,派生类中与其对应的函数可不必说明为虚函数
D. 派生类的虚函数与基类的虚函数具有不同的参数个数和类型

18.阅读下列说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如下所示

【C++代码】

#include
#include
#include
using namespace std;

class AbstractFile {
protected :
string name; // 文件或目录名称
public:
void printName(){cout << name;} // 打印文件或目录名称
virtual void addChild(AbstractFile * file)=0; // 给一个目录增加子目录或文件
virtual void removeChild(AbstractFile * file)=0;// 删除一个目录的子目录或文件
virtual list<AbstractFile* > * getChildren()=0;// 获得一个目录的子目录或文件
};
class File : public AbstractFile {
public :
File(string name) { this -> name(2分) = name; }
void addChild(AbstractFile * file) { return ; }
void removeChild(AbstractFile * file) { return ; }
list <AbstractFile*>* (2分) getChildren() { return NULL(2分); }
};
class Folder :public AbstractFile {
private :
list <AbstractFile*> childList; // 存储子目录或文件
public :
Folder(string name) { this -> name(2分)= name; }
void addChild(AbstractFile *file) { childList.push_back(file); }
void removeChild(AbstractFile file) { childList.remove(file);}
list<AbstractFile
> *getChildren() { return NULL(2分) ; }
};

int main( ) {
// 构造一个树形的文件/目录结构
AbstractFile *rootFolder = new Folder(“c:\”);
AbstractFile *compositeFolder = new Folder(“composite”);
AbstractFile *windowsFolder = new Folder(“windows”);
AbstractFile *file = new File(“TestComposite.java”);
rootFolder->addChild(compositeFolder);
rootFolder->addChild(windowsFolder);
compositeFolder->addChild(file);
return 0;
}

19.从抽象类shape类派生出一个正五边形类(C++)
从下列的抽象类shape类派生出一个正五边形(regular pentagon)类RPentagon,这个类将正五边形的边长作为私有成员,类中包含初始化这个值的构造函数。
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积

virtual double getPerimeter()=0; // 求周长};

正五边形类名:
RPentagon

裁判测试程序样例:
#include
#include
using namespace std;

class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
//你提交的代码将嵌入到这里

int main()
{
double s;
cin>>s;
RPentagon p(s);
cout<<p.getArea()<<endl;
cout<<p.getPerimeter()<<endl;
}

输入样例:
16.8

输出样例:
485.5875
84

class RPentagon : public shape {
public:double a;RPentagon(double s){a = s;}double getArea() {return a * a * sqrt(25 + 10 * sqrt(5)) / 4.0;}double getPerimeter () {return a *5;}
};

20.从shape类派生出一个直角三角形类RTriangle
从shape类派生出一个直角三角形类RTriangle类(regular triangle)。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积

virtual double getPerimeter()=0; // 求周长};
直角三角形类名:RTriangle
直角三角形类的构造函数原型如下:
RTriangle(double a, double b);
其中 a 和 b 都是直角三角形的两条直角边。
裁判测试程序样例:
#include
#include
using namespace std;

class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
//你提交的代码将嵌入到这里

int main()
{
double a,b;
cin>>a>>b;
RTriangle t(a,b);
cout<<t.getArea()<<endl;
cout<<t.getPerimeter()<<endl;
}

输入样例:
在这里填写一组输入
3 4

输出样例:
在这里填写相应的输出
12
12

class RTriangle:public shape{
private:double a;double b;
public:RTriangle(double a1,double b1) {a = a1;b = b1;}double getArea() {return 0.5 * a * b;}double getPerimeter() {return a + b + sqrt(a * a + b * b);}
};

第八章 模板

1.关于函数模板,描述错误的是。 A

A. 函数模板必须由程序员实例化为可执行的函数模板
B. 函数模板的实例化由编译器实现
C. 一个类定义中,只要有一个函数模板,则这个类是类模板
D. 类模板的成员函数都是函数模板,类模板实例化后,成员函数也随之实例化

2.下列的模板说明中,正确的是。 C

A. template < typename T1, T2 >
B. template < class T1, T2 >
C. template < typename T1, typename T2 >
D. template ( typedef T1, typedef T2 )

3.假设有函数模板定义如下:

template Max( T a, T b ,T &c)
{ c = a + b ; }

下列选项正确的是( B )。

A. int x, y; char z ;Max( x, y, z ) ;
B. double x, y, z ;Max( x, y, z ) ;
C. int x, y; float z ;Max( x, y, z );
D. float x; double y, z;Max( x, y, z ) ;

4.建立类模板对象的实例化过程为。 C

A. 基类-派生类
B. 构造函数-对象
C. 模板类-对象
D. 模板类-模板函数

5.一个简单的队列类模板
请按照下列简单的整数队列类创建一个简单的队列类模板。
整数队列类如下:
const int SIZE=100;
//整数队列类
class Queue {
int q[SIZE];
int front; //队列头
int rear; //队列尾
public:
Queue( )
{ front = rear = 0; }
void put(int i); // 在队尾放置一个数据
int get( ); // 从队列头部取一个数据
};

裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include
#include
using namespace std;
// 你提交的代码将嵌入到这里

int main()
{
Queue a; // 创建一个整数队列
int m,n;
cin>>m>>n;
a.put(m);
a.put(n);
cout << a.get() << " ";
cout << a.get() << endl;

Queue d; // 创建一个双精度浮点数队列
double x,y;
cin>>x>>y;
d.put(x);
d.put(y);
cout << d.get() << " ";
cout << d.get() << endl;

Queue qs;// 创建一个字符串队列
string s1,s2,s3;
cin>>s1>>s2>>s3;
qs.put(s1);
qs.put(s2);
qs.put(s3);
cout << qs.get() << " ";
cout << qs.get() << " ";
cout << qs.get() << endl;

return 0;
}

输入样例:
6 9
3.14159 2.781828
ShenZhen Beijing HangZhou

输出样例:
6 9
3.14159 2.78183
ShenZhen Beijing HangZhou

const int SIZE = 100;
template <typename T> class Queue {
private:T q[SIZE];int front;int rear;
public:Queue() {front = rear = 0;}void put(T i);T get();
};
template <class T> void Queue<T> :: put(T i) {if (front == SIZE)exit(0);rear++;q[rear] = i;
}
template <class T>T Queue<T> :: get() {if (rear == front)return 0;front++;return q[front];
}

6.Max函数模板
创建一个函数模板Max( ),该模板返回两个参数中的最大值,例如:

Max(3,4)返回4
Max(‘c’,‘a’)返回’c’

裁判测试程序样例:
#include
#include
using namespace std;
//你提交的代码将被嵌入到这里

int main( )
{
int m,n;
char c,d;
string s1,s2;
cin>>m>>n;
cin>>c>>d;
cin>>s1>>s2;
cout<<Max(m,n)<<endl;
cout<<Max(c,d)<<endl;
cout<<Max(s1,s2)<<endl;
return 0 ;
}

/* 请在这里填写答案 */

输入样例:
在这里填写一组输入
-21252 3

输出样例:
在这里填写相应的输出
3

template<class T> T Max(T x, T y) {return x > y ? x : y;
}

7.创建函数模板实现求数组中的最小元素
创建一个函数模板实现求数组中的最小元素,在主函数将分别使用该模板求整形数组和double型数组中的最小元素并输出。
输入:
6 (整型数组中的元素个数)
8 3 4 1 6 9 (整型数组中的元素)
5 (双精度浮点数数组中的元素个数)
2.7 3.1 9.8 5.3 7.6 (双精度浮点数数组中的元素)
输出:
1
2.7
函数模板接口定义:
T Min(T *p, int len)

其中 p 和 len 都是用户传入的参数。 p 是数组元素的起始地址; len 是数组的长度。
裁判测试程序样例:
#include
using namespace std;
// 你提交的代码将嵌入到这里

int main( )
{
int n,m,*pn,i=0;
cin>>n;
pn=new int[n];
do{
cin>>pn[i];
i++;
}while(i<n);
double *pd;
i=0;
cin>>m;
pd=new double[m];
do{
cin>>pd[i];
i++;
}while(i<m);
cout<<Min(pn,n)<<endl;
cout<<Min(pd,m)<<endl;
delete [ ] pn;
delete [ ] pd;
return 0;
}

输入样例:
9
8 2 7 6 4 5 3 1 0
7
3.1 9.6 5.8 2.7 6.3 7.0 8.5

输出样例:
0
2.7

emplate<class T1, class T2> T1 Min(T1 *p, T2 len) {int j;T1 min = p[0];for (j = 0; j < len; j++) {if (p[j] < min)min = p[j];}return min;
}

8.关于类模板,描述错误的是。 A

A. 一个普通基类不能派生类模板
B. 类模板可以从普通类派生,也可以从类模板派生
C. 根据建立对象时的实际数据类型,编译器把类模板实例化为模板类
D. 函数的类模板参数需生成模板类并通过构造函数实例化

9.下列有关模板的描述,错误的是_D___。

A. 模板把数据类型作为一个设计参数,称为参数化程序设计
B. 使用时,模板参数与函数参数相同,是按位置而不是名称对应的
C. 模板参数表中可以有类型参数和非类型参数
D. 类模板与模板类是同一个概念

10.编写一个函数模板Swap,实现两个变量交换。
编写一个函数模板Swap,实现两个变量交换。
函数模板实例化:
Swap( i, j );

:其中 i 和 j 都是用户传入的参数, i 和 j类型相同,但可以是任何数据类型。
裁判测试程序样例:
#include
using namespace std;
//你提交的代码将被嵌入到这里

int main()
{
int i,j;
char m,n;
cin>>i>>j;
cin>>m>>n;
Swap(i,j);
Swap(m,n);
cout<<i<<’ ‘<<j<<’\n’;
cout<<m <<’ '<<n<<endl;
return 0;
}

输入样例:
2 1
a c

输出样例:
1 2
c a

template <class T> void Swap(T &a, T &b) {T t;t = a;a = b;b = t;
}

11.编写一个函数模板 sort 实现选择法排序的功能
编写一个函数模板 sort 实现选择法排序的功能。
函数模板实例化:
Mysort(a,m);

其中 a 和 n 都是用户传入的参数。 a 表示数组名; m 表示数组元素的个数。
裁判测试程序样例:
#include
using namespace std;
//你提交的代码将被嵌入到这里

int main()
{
int m,n,a[20],i,j;
float b[20];
cin>>m;
for(i=0;i<m;i++)
cin>>a[i];
cin>>n;
for(j=0;j<n;j++)
cin>>b[j];
Mysort(a,m);
Mysort(b,n);
for(i=0;i<m;i++)
cout<<a[i]<<’ ‘;
cout<<endl;
for(j=0;j<n;j++)
cout<<b[j]<<’ ';
cout<<endl;
return 0;
}

输入样例:
在这里填写一组输入
6
1 4 2 6 3 5
5
1.5 1.2 1.1 1.3 1.4

输出样例:
在这里填写相应的输出
1 2 3 4 5 6
1.1 1.2 1.3 1.4 1.5

template <class T> void Mysort(T a[], int n) {T t;int i, j, k;for (i = 0; i < n; i++) {k = i;for(j = i + 1; j < n; j++){if (a[k] > a[j])k = j;}if (k != i) {t = a[i];a[i] = a[k];a[k] = t;}}
}

C++PTA习题总结(二)相关推荐

  1. [PTA]习题9-3 平面向量加法

    [PTA]习题9-3 平面向量加法 本题要求编写程序,计算两个二维平面向量的和向量. 输入格式: 输入在一行中按照"x₁ y₁ x₂ y₂"的格式给出两个二维平面向量v₁ = (x ...

  2. 严蔚敏《数据结构》习题(二)

    严蔚敏<数据结构>习题(二) 2.38 设有一个双向循环链表,每个结点中除有pre,data和next三个域外,还增设了一个访问频度域freq.在链表被起用之前,频度域freq的值均初始化 ...

  3. 考研数学|23强化分题型习题数二【目录】

    考研数学|23强化分题型习题数二 高等数学 线性代数 高等数学 线性代数

  4. 数据采集与清洗基础习题(二)Python爬虫常用模块,头歌参考答案

    数据采集习题参考答案,会持续更新,点个关注防丢失.为了方便查找,已按照头歌重新排版,朋友们按照头歌所属门类查找实训哦,该篇为Python爬虫常用模块. 创作不易,一键三连给博主一个支持呗. 文章目录 ...

  5. 刘汝佳《算法竞赛入门经典(第二版)》习题(二)

    刘汝佳<算法竞赛入门经典(第二版)>第二章习题 目录 刘汝佳<算法竞赛入门经典(第二版)>第二章习题 习题2-1 水仙花数 习题2-2 韩信点兵 习题2-3 倒三角形 习题2- ...

  6. pta习题:给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。

    5-6 树的遍历 分数 20 全屏浏览题目 切换布局 作者 陈越 单位 浙江大学 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一 ...

  7. PTA习题7-1 选择法排序 (20 分)

    习题7-1 选择法排序 (20 分) 本题要求将给定的n个整数从大到小排序后输出. 输入格式: 在一行中输出从大到小有序的数列,相邻数字间有一个空格,行末不得有多余空格. 输入样例: 4 5 1 7 ...

  8. 【机器学习】中国大学慕课《机器学习》课后习题(二)(回归)

    部分习题来自黄海广老师的慕课<机器学习>的课后习题,答案暂不公布,可以留言讨论. 课程链接: https://www.icourse163.org/course/WZU-146409617 ...

  9. pta 是否完全二叉搜索树_二叉树:构造一棵搜索树

    给「代码随想录」一个星标吧! ❝ 构造二叉搜索树,一不小心就平衡了 ❞ 108.将有序数组转换为二叉搜索树 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指 ...

最新文章

  1. PHP中session_register函数详解用法
  2. cf559C. Gerald and Giant Chess(容斥原理)
  3. window系统快捷键
  4. 从源码深处体验Spring核心技术--基于Xml的IOC容器的初始化
  5. php 读取 stl 文件,科学网—[转载]【源码】读取ASCII STL文件的STLReader - 刘春静的博文...
  6. Linux系统安装MySql步骤及截屏
  7. myeclipse2014 mysql连接池_MyEclipse配置数据源连接池
  8. PyCharm中文指南2.0
  9. GitLab non-standard SSH port
  10. 个人项目集 - Oliver Chu
  11. 必应搜索昨日起出现大规模的无法访问
  12. 微信小程序中自定义select下拉选项框
  13. Visual Paradigm 下载安装及使用
  14. 修复Critical dependency: the request of a dependency is an expression警告
  15. Unity3D模型中的SubMesh
  16. python打造excel神器_将Excel与Python完美组合的神器
  17. 烧录esp32 并用oled显示
  18. hitTest的作用与用法
  19. java核心技术卷代码下载
  20. 双系统安装教程(win10 ubuntu20.04)

热门文章

  1. oracle 新建一个用户和赋予权限
  2. python进阶 (Jupyter 快捷键)
  3. 夜光带你走进Jquery(十四)擅长的领域
  4. 中国地址英文书写格式(转载)
  5. 皮尔逊、斯皮尔曼、肯德尔相关系数python实现
  6. 蓝牙协议规范(如Bluetooth Core Specification)下载地址
  7. Marvell®AQC113C-B1-C、AQC113-B1-C、AQC113C-B1-I、AQC113-B1-I【以太网控制器】基本介绍
  8. 判断复选框是否被选中
  9. 图片标签 [HTML][web前端]
  10. 血型遗传关系c语言编程,血型遗传