4-1
写出下面程序的输出结果
#include <iostream>
using namespace std;
class B{
public:
B(){cout<<"B_Con"<<endl;}
~B(){cout<<"B_Des"<<endl;}
};
class C:public B{
public :
C(){cout<<"C_Con"<<endl;}
~C(){cout<<"C_Des"<<endl;}
};
int main(){
C* pc=new C;
delete pc;
return 0;
}
B_Con
C_Con
C_Des
B_Des
4-2
下面程序的输出结果是:
4,6
#include <iostream>
using namespace std;
class A {
int val;
public:
A(int n) { val = n; }
int GetVal() { return val; }
};
class B : public A {
private:
int val;
public:
B(int n) :
A(n*3),val(n*2){} //答案
int GetVal() { return val; }
};
int main() {
B b1(2);
cout<< b1.GetVal() << "," << b1.A::GetVal() <<endl;
return 0;
}
4-3
下面程序的输出结果是:
A::Fun
A::Do
A::Fun
C::Do
填空使程序完整。
#include<iostream>
using namespace std;
class A{
private:
int val;
public:
void Fun()
{
cout<<"A::Fun"<<endl;
}
virtual void Do()
{
cout<<"A::Do"<<endl;
}
};
class B:public A{
public:
virtual void Do()
{
cout<<"B::Do"<<endl;
}
};
class C:public B{
public:
void Do()
{
cout<<"C::Do"<<endl;
}
void Fun()
{
cout<<"C::Fun"<<endl;
}
};
void Call(A *p)//答案
{
p->Fun();
p->Do();
}
int main(){
Call(new A());
//答案
Call(new C());
return 0;
}
6-1 狗的继承
完成两个类,一个类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 <string>
using namespace std;
class Animal
{
int Age;
public:
Animal(int a = 0):Age(a){}
int getAge()
{
return Age;
}
};
class Dog:public Animal
{
string color;
public:
Dog (int a, string c):Animal(a),color(c){}
void showInfor()
{
cout << "age:" << getAge() << endl;
cout << "color:" << color << endl;
}
};
6-2 写出派生类构造方法(C++)
裁判测试程序样例中展示的是一段定义基类People
、派生类Student
以及测试两个类的相关C++代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。
函数接口定义:
提示: 观察类的定义和main方法中的测试代码,补全缺失的代码。
裁判测试程序样例:
注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关函数。
#include <iostream>
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)
{
this->sid = sid;
this->score = score;
6-3 派生类的定义和使用
按要求完成下面的程序:
1、定义一个Animal
类,包含一个void类型的无参的speak
方法,输出“animal language!”。
2、定义一个Cat
类,公有继承自Animal
类,其成员包括:
(1)私有string
类型的成员m_strName
;
(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;
(3)公有的成员函数print_name
,无形参,void
类型,功能是输出成员m_strName
的值,具体输出格式参见main函数和样例输出。
类和函数接口定义:
参见题目描述。
裁判测试程序样例:
#include <iostream>
#include <string>
using namespace std;
/* 请在这里填写答案 */
int main()
{
Cat cat("Persian"); //定义派生类对象
cat.print_name(); //派生类对象使用本类成员函数
cat.speak(); //派生类对象使用基类成员函数
return 0;
}
输入样例:
本题无输入。
输出样例:
cat name: Persian
animal language!
class Animal
{
public:
void speak()
{
cout<<"animal language!"<<endl;
}
};
class Cat:public Animal
{
private:
string m_strName;
public:
Cat(string name):m_strName(name){}
void print_name()
{
cout<<"cat name: "<<this->m_strName<<endl;
}
};
6-4 长方形派生
如下,现已有一个完整的长方形的类Rectangle, 数据成员有长和宽,成员函数包括一个构造函数和一个计算面积的函数area()。
请写出一个表示长方体的派生类Box,继承这个已给出的Rectangle类,满足以下要求:
(1)只能新增一个数据成员:height (高)。
(2)定义一个合适的Box类构造函数,使得main函数中创建对象的初始化可行;
(3)使用合适的继承方式,使得main函数中能通过派生类Box对象直接调用基类中的area()函数输出底面积。
(4)新增一个成员函数 volume() 返回长方体的体积,使得main函数中的调用可行;
函数接口定义:
见题目描述
裁判测试程序样例:
#include <iostream>
using namespace std;
class Rectangle { //长方形
public:
Rectangle(double l, double w) : length(l), width(w) {
}
double area() {
return length * width;
}
private:
double length; //长
double width; //宽
};
//在此定义派生类Box
/* 请在这里填写答案 */
int main() {
Box b1(10, 20, 30);
cout << b1.area() << endl;
cout << b1.volume() << endl;
return 0;
}
输入样例:
无
输出样例:
200
6000
class Box:public Rectangle
{
public:
double height;
Box(double l,double w,double h):Rectangle(l,w),height(h){}
double volume()
{
return this->length*this->width*this->height;
}
};
6-5 多重继承
声明一个教师(Teacher)类和一个学生(Student)类,用多重继承的方式声明一个研究生(Graduate)派生类。教师类中包括数据成员name(姓名),age(年龄),title(职称)。学生类中包括数据成员name(姓名),age(年龄),score(成绩),输出这些数据。
函数接口定义:
参见题目描述
裁判测试程序样例:
/* 请在这里填写答案 */
int main( ) {
Graduate grad1("Wang-li",24,'f',"assistant",89.5,1234.5);
grad1.show( );
return 0;
}
输入样例:
无输入
输出样例:
name:Wang-li
age:24
sex:f
score:89.5
title:assistant
wages:1234.5
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
string name;
int age;
string title;
Teacher(string n,int a,string t):name(n),age(a),title(t){}
};
class Student
{
public:
string name;
char sex;
double score;
Student(string n,char se,double s):name(n),sex(se),score(s){}
};
class Graduate:virtual public Teacher,virtual public Student
{
public:
double wage;
Graduate(string n,int a,char se,string t,double s,double w):Student(n,se,s),Teacher(n,a,t),wage(w){}
void show()
{
cout<<"name:"<<Teacher::name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl;
cout<<"title:"<<title<<endl;
cout<<"wages:"<<wage<<endl;
}
};