6-1 狗的继承
分数 2
全屏浏览题目
切换布局
作者 杨军
单位 四川师范大学
完成两个类,一个类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
#include<bits/stdc++.h>
using namespace std;
class Animal
{
private:
int age;
public:
Animal (int a)
{
age=a;
}
int getAge()
{
return age;
}
};
class Dog:public Animal
{
private:
string color;
public:
Dog (int age,string c):Animal(age)
{
color=c;
}
void showInfor()
{
cout<<"age:"<<getAge()<<endl<<"color:"<<color<<endl;
}
};
6-2 使用类输出图形
分数 5
全屏浏览题目
切换布局
作者 刘骥
单位 重庆大学
声明一个图形基类 Shape,在它的基础上派生出矩形类 Rectangle 和圆形类 Circle,他们都有计算面积和周长、输出图形信息等成员函数,再在 Rectangle 类的基础上派生出正方形类 Square。编写程序完成各类的定义和实现,以及类的使用。
注意:
题目中定义的所有数值类型的变量都使用 double 类型
π的值为 3.14159
测试程序样例:
#include <iostream> #define PI 3.14159 using namespace std; class Shape{ public: double getArea(){return 0;} double getPerimeter(){return 0;} }; class Rectangle:public Shape { protected: double height; double width; public: Rectangle() {}; Rectangle(double _height, double _width) { height = _height; width = _width; } double getArea(){ return height * width; } double getPerimeter(){ return 2 * (height + width); } void Print(){ cout << "Width=" << width << ",Height=" << height << endl; } }; /* 请在这里填写答案 */ int main() { double ra, rb; cin >> ra >> rb; Rectangle r1(ra, rb); double sa; cin >> sa; Square s1(sa); double ca, cb, cc; cin >> ca >> cb >> cc; Circle c1(ca, cb, cc); cout << "Rectangle" << endl; r1.Print(); cout << "Area=" << r1.getArea() << endl; cout << "Perimeter=" << r1.getPerimeter() << endl ; cout << "Square" << endl; s1.Print(); cout << "Area=" << s1.getArea() << endl; cout << "Perimeter=" << s1.getPerimeter() << endl; cout << "Circle" << endl; c1.Print(); cout << "Area=" << c1.getArea() << endl; cout << "Perimeter=" << c1.getPerimeter() << endl; return 0; }
输入样例:
在这里给出一组输入。例如:
4 2.5
8
4 5 6.5
输出样例:
Rectangle
Width=2.5,Height=4
Area=10
Perimeter=13
Square
Side=8
Area=64
Perimeter=32
Circle
Center=[4,5],Adius=6.5
Area=132.732
Perimeter=40.8407
提示
需要补充的类定义如下:
class Square:public Rectangle{ //...... }; class Circle: public Shape{ //...... };
class Square:public Rectangle
{
private:
int side;
public:
Square (int s)
{
side=s;
}
void Print()
{
cout<<"Side="<<side<<endl;
}
int getArea()
{
return side*side;
}
int getPerimeter()
{
return 4*side;
}
};
class Circle: public Shape
{
private:
int x;
int y;
float adius;
public:
Circle (int x0,int y0,float a)
{
x=x0;
y=y0;
adius=a;
}
void Print()
{
cout<<"Center=["<<x<<","<<y<<"],Adius="<<adius<<endl;
}
double getArea()
{
return PI*adius*adius;
}
double getPerimeter()
{
return 2*PI*adius;
}
};
6-3 派生类的定义和使用
分数 4
全屏浏览题目
切换布局
作者 李廷元
单位 中国民用航空飞行学院
按要求完成下面的程序:
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 s)
{
m_strName=s;
}
void print_name()
{
cout<<"cat name: "<<m_strName<<endl;
}
};
6-4 派生类使用基类的成员函数
分数 5
全屏浏览题目
切换布局
作者 李廷元
单位 中国民用航空飞行学院
按要求完成下面的程序:
1、定义一个Animal
类,成员包括:
(1)整数类型的私有数据成员m_nWeightBase
,表示Animal
的体重;
(2)整数类型的保护数据成员m_nAgeBase
,表示Animal
的年龄;
(3)公有函数成员set_weight
,用指定形参初始化数据成员nWeightBase
;
(4)公有成员函数get_weight
,返回数据成员nWeightBase
的值;
(5)公有函数成员set_age
,用指定形参初始化数据成员m_nAgeBase
;
2、定义一个Cat
类,公有继承自Animal
类,其成员包括:
(1)string类型的私有数据成员m_strName
;
(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;
(3)公有的成员函数print_age
,功能是首先输出成员m_strName
的值,然后输出“, age = ”,接着输出基类的数据成员m_nAgeBase
的值。具体输出格式参见main函数和样例输出。
类和函数接口定义:
参见题目描述。
裁判测试程序样例:
#include <iostream> #include <string> using namespace std; /* 请在这里填写答案 */ int main() { Cat cat("Persian"); //定义派生类对象cat cat.set_age(5); //派生类对象调用从基类继承的公有成员函数 cat.set_weight(6); //派生类对象调用从基类继承的公有成员函数 cat.print_age(); //派生类对象调用自己的公有函数 cout << "cat weight = " << cat.get_weight() << endl; return 0; }
输入样例:
本题无输入。
输出样例:
Persian, age = 5
cat weight = 6
class Animal
{
private:
int m_nWeightBase;
protected:
int m_nAgeBase;
public:
void set_weight(int w)
{
m_nWeightBase=w;
}
int get_weight()
{
return m_nWeightBase;
}
void set_age(int a)
{
m_nAgeBase=a;
}
};
class Cat:public Animal
{
private:
string m_strName;
public:
Cat (string s)
{
m_strName=s;
}
void print_age()
{
cout<<m_strName<<", age = "<<m_nAgeBase<<endl;
}
};
6-5 私有继承派生类使用基类的成员函数
分数 7
全屏浏览题目
切换布局
作者 李廷元
单位 中国民用航空飞行学院
按要求完成下面的程序:
1、定义一个Animal
类,成员包括:
(1)整数类型的私有数据成员m_nWeightBase
,表示Animal
的体重;
(2)整数类型的保护数据成员m_nAgeBase
,表示Animal的年龄;
(3)公有函数成员set_weight
,用指定形参初始化数据成员m_nWeightBase
;
(4)公有成员函数get_weight
,返回数据成员m_nWeightBase
的值;
(5)公有函数成员set_age
,用指定形参初始化数据成员m_nAgeBase
;
2、定义一个Cat
类,私有继承自Animal
类,其成员包括:
(1)string
类型的私有数据成员m_strName
;
(2)带参数的构造函数,用指定形参对私有数据成员进行初始化
;
(3)公有的成员函数set_print_age
,功能是首先调用基类的成员函数set_age
设置数据成员m_nAgeBase
的值为5,接着输出成员m_strName
的值,然后输出“, age = ”,最后输出基类的数据成员m_nAgeBase
的值。具体输出格式参见main函数和样例输出。
(4)公有的成员函数set_print_weight
,功能是首先调用基类的成员函数set_weight
设置数据成员nWeightBase
的值为6,接着输出成员m_strName
的值,然后输出“, weight = ”,最后调用基类的成员函数get_weight
输出基类的数据成员m_nAgeBase
的值。具体输出格式参见main函数和样例输出。
类和函数接口定义:
参见题目描述。
裁判测试程序样例:
#include <iostream> #include <string> using namespace std; /* 请在这里填写答案 */ int main() { Cat cat("Persian"); //定义派生类对象cat cat.set_print_age(); cat.set_print_weight(); //派生类对象调用自己的公有函数 return 0; }
输入样例:
本题无输入。
输出样例:
Persian, age = 5
Persian, weight = 6
class Animal
{
private:
int m_nWeightBase;
protected:
int m_nAgeBase;
public:
void set_weight(int w)
{
m_nWeightBase=w;
}
int get_weight()
{
return m_nWeightBase;
}
void set_age(int a)
{
m_nAgeBase=a;
}
};
class Cat:private Animal
{
private:
string m_strName;
public:
Cat(string s)
{
m_strName=s;
}
void set_print_age()
{
Animal::set_age(5);
cout<<m_strName<<", age = "<<m_nAgeBase<<endl;
}
void set_print_weight()
{
Animal::set_weight(6);
cout<<m_strName<<", weight = "<<get_weight()<<endl;
}
};
6-6 多重继承派生类构造函数
分数 5
全屏浏览题目
切换布局
作者 范鹏程
单位 内蒙古师范大学
根据所给的基类Student和Teacher,定义Graduate类
类定义:
#include <iostream> #include <string> using namespace std; class Teacher {public: Teacher(string nam,int a,string t) {name=nam; age=a; title=t;} void display() {cout<<"name:"<<name<<endl; cout<<"age"<<age<<endl; cout<<"title:"<<title<<endl; } protected: string name; int age; string title; }; class Student {public: Student(string nam,char s,float sco) {name1=nam; sex=s; score=sco;} void display1() {cout<<"name:"<<name1<<endl; cout<<"sex:"<<sex<<endl; cout<<"score:"<<score<<endl; } protected: string name1; char sex; float 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
class Graduate:public Teacher,public Student
{
private:
float wage;
public:
Graduate (string name,int age,char sex,string title,float score,float w):Teacher(name,age,title),Student(name,sex,score)
{
wage=w;
}
void show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl;
cout<<"title:"<<title<<endl;
cout<<"wages:"<<wage<<endl;
}
};
6-7 汽车类的继承
分数 5
全屏浏览题目
切换布局
作者 范鹏程
单位 内蒙古师范大学
根据给定的汽车类vehicle(包含的数据成员有车轮个数wheels和车重weight)声明,完成其中成员函数的定义,之后再定义其派生类并完成测试。
小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。
###Vehicle类声明如下:
#include<iostream>
using namespace std;
class Vehicle
{
protected:
int wheels;
float weight;
public:
Vehicle(int wheels,float weight);
int get_wheels();
float get_weight();
float wheel_load();
void show();
};
/* 请在这里填写答案 */
裁判测试程序样例:
int main () { Vehicle v(4,1000); v.show(); Car car1(4,2000,5); car1.show (); return 0; }
输出样例:
在这里给出相应的输出。例如:
Type:Vehicle
Wheel:4
Weight:1000kg
Type:Car
Type:Vehicle
Wheel:4
Weight:2000kg
Load:5 persons
Vehicle::Vehicle(int wh,float we)
{
this->wheels=wh;
this->weight=we;
}
int Vehicle::get_wheels()
{
return wheels;
}
float Vehicle::get_weight()
{
return weight;
}
float Vehicle::wheel_load()
{
;
}
void Vehicle::show()
{
cout<<"Type:Vehicle"<<endl;
cout<<"Wheel:"<<get_wheels()<<endl;
cout<<"Weight:"<<get_weight()<<"kg"<<endl;
}
class Car:public Vehicle
{
private:
int passenger_load;
public:
Car(int wheels,int weight,int p):Vehicle(wheels,weight)
{
passenger_load=p;
}
void show()
{
cout<<"Type:Car"<<endl;
cout<<"Type:Vehicle"<<endl;
cout<<"Wheel:"<<get_wheels()<<endl;
cout<<"Weight:"<<get_weight()<<"kg"<<endl;
cout<<"Load:"<<passenger_load<<" persons"<<endl;
}
};
6-8 鸭子也是鸟
分数 5
全屏浏览题目
切换布局
作者 fpc
单位 内蒙古师范大学
按要求完成下面的程序:
1、定义一个Bird类,包含一个void类型的无参的speak方法,输出“Jiu-Jiu-Jiu”。
2、定义一个Duck类,公有继承自Bird类,其成员包括:
(1)私有string类型的成员name;
(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;
(3)公有的成员函数printName,无形参,void类型,功能是输出成员name的值,具体输出格式参见main函数和样例输出。
裁判测试程序样例:
#include <iostream>
#include <string>
using namespace std;
/* 请在这里填写答案 */
int main()
{
Bird b;
b.speak();
Duck d("Donald"); //定义派生类对象
d.printName(); //派生类对象使用本类成员函数
d.speak(); //派生类对象使用基类成员函数
return 0;
}
输入样例:
无输入
输出样例:
Jiu-Jiu-Jiu
Dird Name:Donald
Jiu-Jiu-Jiu
class Bird
{
public:
void speak()
{
cout<<"Jiu-Jiu-Jiu"<<endl;
}
};
class Duck:public Bird
{
private:
string name;
public:
Duck(string n)
{
name=n;
}
void printName()
{
cout<<"Dird Name:"<<name<<endl;
}
};