应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。完成fun()函数,其功能是:求出M行N列二维数组每行元素中的最小值,并计算它们的和值。和值通过形参传回主函数输出。注意:不能修改程序的其他部分,只能修改fun()函数。
#include<iostream.h>
#define M 2
#define N 4
void fun(int a[M][N],int *sum)
{
}
void main()
{
int X[M][N]={7,6,5,2,4,2,8,3);
int s;
fun(x,s);
cout<<s<<endl;
return;
}
应用题 使用VC++6.0打开下的源程序文件2.cpp。请完成以下两个函数。
(1)fun1(int n)求出n的阶乘,必须使用递归调用。
(2)fun2(int n)求出n的阶乘,不能使用递归调用。如果n小于1则返回0。
注意:不能修改函数的其他部分。
试题程序:
#include<iostream.h>
//必须使用递归
int funl(int n)
{
}
//不能使用递归
int fun2(int n)
{
}
void main()
{
int i;
cout<<'请输入一个整数:'<<endl;
cin>>i;
cout<<'输入数字的阶乘是:'<<fun1(i)<<endl;
cout<<'输入数字的阶乘是:'<<fun2(i)<<endl;
return;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)对文件以追加的方式打开文件。请在注释//********1********后添加适当的语句。
(2)定义m、n为类TestClass的公有int型数据成员,请在注释//********2********后添加适当的语句。
(3)定义p为类TestClass的数据成员指针,并指向类TestClass数据成员m,请在注释//********3********后添加适当的语句。
(4)定义p指向类TestClass数据成员n,请在注释//********4********后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include<iostream.h>
#include<fstream>
#include<iomanip>
#include<cmath>
using namespace std;
void WriteFile(int x)
{
ofstream out1;
//********1********
out1.open('modi3.txt',);
out1<<x<<' ';
out1.close();
}
void ClearFiie()
{
ofstream out1;
out1.open('modi3.txt');
out1.close();
}
class TestClass
{
public:
void disp()
{
cout<<'m='<<m<<endl;
WriteFile(m);
cout<<'n='<<n<<endl:
WriteFile(n);
}
//********2********
};
void main()
{
//********3********
ClearFile();
TestClass a;
a.*p=30;
//********4********
a.*p=45;
a.disp();
}
应用题 请使用VC6或使用【答题】菜单打开prog3下的工程prog3,其中包含了类TaxCalculator(“个税计算器”)和主函数main的定义。创建“个税计算器”需要接收税率表信息和起征额信息。在main函数中,通过两个数组创建了如下的税率表:
下标
适用收入段下限(lower_limits)
适用税率(rates)
说明:适用的收入段为
0
0
5
大于0小于等于500
1
500
10
大于500小于等于2000
2
2000
15
大于2000小于等于5000
3
5000
20
大于5000小于等于20000
4
20000
25
大于20000小于等于40000
5
40000
30
大于40000小于等于60000
6
60000
35
大于60000小于等于80000
7
80000
40
大于80000小于等于100000
8
100000
45
大于100000
利用这个税率表创建“个税计算器”时,假定起征额为2000元(即不超过2000元的所得不征收个人所得税)。请补充完成计算应纳个人所得税额的成员函数getTaxPayable,其中的参数income为月收入。此程序的正确输出结果应为:
月收入为800元时应缴纳个人所得税0元
月收入为1800元时应缴纳个人所得税0元
月收入为2800元时应缴纳个人所得税55元
月收入为3800元时应缴纳个人所得税155元
月收入为4800元时应缴纳个人所得税295元
月收入为5800元时应缴纳个人所得税455元
注意:只能在函数getTaxPayable中的“// ********333********”和“// ********666********”之间填入若干语句,不要改动程序中的其他内容。
//TaxCalculator.h
#include <iostream>
#include <iomanip>
using namespace std;
class TaxCalculator{
public:
TaxCalculator (double the_limits[], double the_rates[], int the_length, double the_threshold)
: lower_limits (new double[the_length]), rates(new double[the_length]),
list_len (the_length), threshold(the_threshold) {
for(int i = 0; i < list_len; i++){
lower_limits[i] = the_limits[i];
rates[i] = the_rates[i];
}
}
~TaxCalculator() {delete[]lower_limits; delete[] rates;]
double getTaxPayable (double income)const; //返回指定月收入的应纳个人所得税额
void showTaxPayable (double income) const; //显示指定月收入的应纳个人所得税额
private:
double * lower limits; //适用收入段下限
double * rates; //适用税率
int list_len; //税率表项数
double threshold; //起征额
};
void writeToFile(const char * path);
//TaxCalcnlator.cpp
#include 'TaxCalculator.h'
double TaxCalculator::getTaxPayable(double income) const {
double taxable = income-threshold;
//应纳税工资额
double tax_payable = 0.0;
//应纳个人所得税额
int i=list len-1;
while(i >= 0){
// ********333********
// ********666********
--i;
}
return taxpayable;
}
void TaxCalculator::showTaxPayable (double income) const {
cout << '月收入为' << setw(6) << income << '元时应缴纳个人所得税'
<<setw(4) << getTaxPayable (income) << '元' << endl;
}
//main.cpp
#include 'TaxCalculator.h'
int main(){
double limits[] = {0.0, 500.0, 2000.0, 5000.0, 20000.0, 40000.0, 60000.0, 80000.0, 100000.0};
double rates[] = {0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45};
TaxCalculator calc (limits,rates,9,2000.0);
calc.showTaxPayable(800.0);
calc.showTaxPayable(1800.0);
calc.showTaxPayable(2800.0);
calc.showTaxPayable(3800.0);
calc.showTaxPayable(4800.0);
calc.showTaxPayable(5800.0);
writeToFile(' ');
return 0;
}
应用题 使用VC++6.0打开下的源程序文件2.cpp。完成fun函数,使其功能为将两个按小到大排序的数组a和b,复制合并成一个有序整数序列c,其中形参n和m分别是数组a和b的元素个数。
注意:不能修改程序的其他部分,只能修改fun函数。
试题程序:
#include<iostream.h>
void fun(int a[], int n, int b[], int m, int*c)
{
}
void main()
{
int A[]={3, 5, 7, 9, 11, 18, 21};
int B[]={6, 15, 19, 21, 39);
int C[25], i;
for(i=0; i<25; i++)C[i]=0;
cout<<'A[]=';
for(i=0; i<7; i++)
cout<<A[i]<<',';
cout<<endl;
cout<<'B[]=';
for(i=0; i<5; i++)
cout<<B[i]<<',';
cout<<endl;
fun(A, 7, B, 5, C);
cout<<'C[]=';
for(i=0; i<12; i++)
cout<<C[i]<<',';
cout<<endl;
return;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,其中定义了Employee类和Manager类。Employee用于表示某公司的雇员,其属性包括姓名(name)和工作部分(dept)。Manager是Employee的公有派生类,用于表示雇员中的经理。除了姓名和工作部分之外,Manager的属性还包括级别(level)。Employee类的成员函数print用于输出雇员的信息;Manager类的成员函数print负责输出经理的信息。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
Name:Sally Smith
Dept:Sales
Level:2
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee (string name, string dept):
// **********found**********
______
{}
virtual void print() const;
string dept() const //返回部门名称
{
// **********found**********
______
}
virtual ~Employee() {}
private:
string name_;
string dept_;
};
class Manager: public Employee {
public:
Manager (string name, string dept, int level):
// **********found**********
______
{}
virtual void print() const;
private:
int level;
};
void Employee::print() const
{
cout << 'Name:' << name_ << endl;
cout << 'Dept:' << dept_ << endl;
}
void Manager::print() const
{
// **********found**********
cout << 'Level:' << level_ << endl;
}
int main()
{
Employee * emp = new Manager('Sally Smith', 'Sales', 2);
emp -> print();
delete emp;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。其中定义的类并不完整,按要求完成列操作,将类的定义补充完整,实现以下功能:
(1)完成CBook类构造函数,对整型变量ID和作者Author进行赋值,请在注释//********1********后添加适当的语句。
(2)完成类CBooks的析构函数,释放申请的内存,请在注释//********2********后添加适当的语句。
(3)完成类CBooks的AddBookMember函数,请在注释//********3********后添加适当的语句。
(4)完成CBooks类,用于由书的ID检索到作者的函数char* GetBookAuthor(int nID),请在注释//********4********后添加适当的语句。
(5)程序的输出结果为:
Tom
Harry
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include<iostream.h>
#include <cstring>
class CBook
{
public:
int ID;
char Author[32];
public:
CBook(int ID_Number, char* Author_Name)
{
this->ID = ID Number;
//********1********
}
};
class CBooks
{
private:
class Node
{
public:
Node* next;
CBook* book;
}*m_pBook;
public:
CBooks()
{
m_pBook = NULL;
}
~CBooks()
{
//********2********
while()
{
Node* p = m_pBook->next;
delete m_pBook->book;
delete m_pBook;
m_pBook = p;
}
}
int AddBookMenber(int nID, char* Author)
{
Node* p = m_pBook;
Node* q = NULL;
//********3********
while()
{
if(nID == p->book->ID)
{
return 0;
}
q = p;
p = p->next;
}
if(p == NULL)
{
p = new Node;
p->next = NULL;
p->book = new CBook(nID,Author);
}
if(q == NULL)
{
m_pBook =p;
}
else
{
q->next = p;
}
return 1;
}
char* GetBookAuthor (int nID)
{
Node* p = m_pBook;
//*******4*******
while()
{
if(p->book->ID == nID)
{
return p-> book->Author;
}
p = p->next;
}
return 0;
}
};
int main()
{
CBooks books1;
books1.AddBookMenber (1,'Tom');
books1.AddBookMenber (3,'Lee');
books1.AddBookMenber (4,'Lily');
books1.AddBookMenber (5,'Harry');
cout<<books1.GetBookAuthor (1)<<endl;
cout<<books1.GetBookAuthor (5)<<endl;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码,补充空出的代码。函数IsPalindromes (cha* string)实现的功能是判定给定的字符串是否构成回文字符串,如果是则返回1,否则返回0。
如:1234554321或者1234321都认为是回文字符串。
如果串为空或一个字母时,均认为是回文字符串。
注意:不能修改程序的其他部分,只能补充IsPalindromes()函数。
#include <iostream.h>
#define MAXLEN 1024
bool IsPalindromes(char*string)
{
}
void main()
{
char str[MAXLEN];
cout<<'请输入一行文字'<<endl;
cin.getline(str,MAXLEN);
cout<<IsPalindromes(str)<<endl;
return;
}
应用题 使用VC++6.0打开下的源程序文件2.cpp。完成fun()函数,其功能是:求出M行N列二维数组每行元素中的最小值,并计算它们的和。和通过形参传回主函数并输出。
注意:不能修改程序的其他部分,只能修改fun()函数。
试题程序:
#include <iostream.h>
#define M 2
#define N 4
void fun(int a[M][N],int *sum)
{
}
void main()
{
int x[M][N]={7,6,5,2,4,2,8,3};
int s;
fun(x,s);
cout<<s<<endl;
return;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp。其中定义了类Set和用于测试该类的主函数main。类Set是一个用于描述字符集合的类,在该字符集合中,元素不能重复(将“a”和“A”视为不同元素),元素最大个数为100。为该类实现一个构造函数Set(char * s),它用一个字符串来构造一个集合对象,当字符串中出现重复字符时,只放入一个字符。此外,还要为该类实现另一个成员函数InSet(char c),用于测试一个字符c是否在一个集合中,若在,则返回true;否则返回false。
构造函数Set和成员函数InSet的部分实现代码已在文件proj2.cpp中给出,请在标有注释“// TODO:”的行中添加适当的代码,将这两个函数补充完整,以实现其功能。
提示:在实现构造函数时,可以调用InSet函数来判断一个字符是否已经在集合中。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
//proj2.cpp
#include <iostream>
using namespace std;
const int MAXNUM = 100;
class Set {
private:
int num; //元素个数
char setdata[MAXNUM]; //字符数组,用于存储集合元素
public:
Set(char *s); //构造函数,用字符串s构造一个集合对象
bool InSet(char c);
//判断一个字符c是否在集合中,若在,返回true,否则返回false
void Print() const; //输出集合中所有元素
};
Set::Set(char * s)
{
num = 0;
while (* s) {
// **********found**********
if (______) //TODO:添加代码,测试元素在集合中不存在
// **********found**********
______; //TODO:添加一条语句,加入元素至集合中
s++;
}
}
bool Set::InSet (char c)
{
for (int i = 0; i < num; i++)
// **********found**********
if (______) //TODO:添加代码,测试元素c是否与集合中某元素相同
// **********found**********
______; //TODO:添加一条语句,进行相应处理
return false;
}
void Set::Print () const
{
cout << 'Set elements:' << endl;
for(int i = 0; i < num; i++)
cout << setdata[i] << '';
cout << endl;
}
int main()
{
char s[MAXNUM];
cin.getline (s, MAXNUM - 1); //从标准输入中读入一行
Set setobj(s); //构造对象setobj
setobj.Print(); //显示对象setobj中内容
return 0;
}
应用题
请打开考生文件夹下的解决方案文件proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
80
150
100
1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
//**********found**********
vehicle(int maxspeed,int weight):______
~vehicle(){};
int getMaxSpeed(){return MaxSpeed;}
int getWeight(){return Weight;}
};
//**********found**********
class bicycle:______ public vehicle
{
private:
int Height;
public:
bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}
int getHeight(){return Height;};
};
//**********found**********
class motorcar:______public vehicle
{
private:
int SeatNum;
public:
motorcar(int maxspeed,int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum){}
int getSeatNum(){return SeatNum;};
};
//**********found**********
class motorcycle:______
{
public:
motorcycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1){}
};
void main()
{
motorcycle a(80,150,100);
cout<<a.getMaxSpeed()<<endl;
cout<<a.getWeight()<<endl;
cout<<a.getHeight()<<endl;
cout<<a.getSeatNum()<<endl;
}
应用题 请使用【答题】菜单命令或直接用VC6打开下的工程prog3,其中声明了ValArray类,该类在内部维护一个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在main函数中给出了一组测试数据,此种情况下程序的输出应该是:
ValArray v1={1,2,3,4,5}
ValArray v2={1,2,3,4,5}
要求:
补充编制的内容写在“// ********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为boj文件,并且在本程序中调用。
//ValArray.h
#include <iostream>
using namespace std;
class ValArray {
int * v;
int size;
public:
ValArray (const int * p, int n) : size(n)
{
v = new int[size];
for (int i = 0; i < size; i ++)
v[i] = p[i];
}
ValArray (const ValArray other);
~ValArray() {delete [] v;}
void print (ostream out) const
{
out << '{';
for (int i = 0; i < size -1; i ++)
out << v[i] << ',';
out << v[size-1] << '}';
}
void setArray (int i, int val)
{
v[i] = val;
}
};
void writeToFile(const char *);
//main.cpp
#include 'ValArray.h'
ValArray:: ValArray (const ValArray other)
{
// ********333********
// ********666********
}
int main()
{
const int a[] = {1,2,3,4, 5};
ValArray v1(a, 5);
cout << 'ValArray v1 =';
v1.print(cout);
cout << endl;
ValArray v2(v1);
cout << 'ValArray v2 =';
v2.print(cout);
cout << endl;
writeToFile(' ');
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码,补充空出的代码。函数convert (cha* des,char* str)实现的功能是:
(1)如果字符串最后面有空格,则全部删除;
(2)转换后的数据放到des,并且指针作为返回值返回。
注意:不能修改程序的其他部分,只能补充convert()函数。
#include <iostream.h>
#include <ctype.h>
#define MAXLEN 1024
char* convert(char* des,char*str)
{
}
void main()
{
char dest[MAXLEN];
char *string=' abc def ';
cout << string <<'<--' << endl;
cout<<convert(dest,string) <<'<--'<<endl;
return;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码。函数sort(int m,int n,int l)实现将三个整数m、n、l由大到小输出。m最大,l最小。
程序分析:程序实现时,可以把最大的数放到m上,先将m与n进行比较,如果m<n则将m与n的值进行交换,然后再用m与l进行比较,如果m<l则将m与l的值进行交换,这样能使m最大。然后再将n与l进行比较,若n<l则将n与l的值互换,互换后则l最小。
将函数sort(int m,int n,int l)补充完整,实现三个数的排序。
注意:请勿改动主函数。
#include <iostream.h>
void sort(int m, int n, int l)
{
}
int main()
{
int x=9;
int y=13;
int z=-3;
sort(x,y,z);
cout<<x<<','<<y<<','<<z<<endl;
return 0;
}
应用题 请使用【答题】菜单命令或直接用VC6打开考生文件夹下的工程proj3,其中声明的是一个人员信息类,补充编制程序,使其功能完整。在main函数中给出了一组测试数据,此种情况下程序的输出应该是:Zhang 20 Tsinghua。
注意:只能在函数address_change的“// ********333********”和“// ********666********”之间填入若干语句,不要改动程序中的其他内容。
程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//proj3.h
#include <iostream>
#include <string>
using namespace std;
class Person{
char name[20];
int age;
char * address;
public:
Person(){ age = 0;address = 0;}
void name_change (char *_name);
//名字修改函数
void age_change(int_age);
//年龄修改函数
void address_change (char *_add);
//地址修改函数
void info_display();
//人员信息显示
~Person();
//析构函数
};
void writeToFile(const char * path);
proj3.cpp
#include <iostream>
#include <string>
#include 'proj3.h'
using namespace std;
void Person::name_change(char*_name)
{
strcpy(name, _name);
}
void Person::age_change(int _age)
{
age = _age;
}
void Person::address _change (char * add)
{
if (address!=NULL) delete[]address;
// ********333********
// ********666********
}
void Person::info_display(){
cout << name << '\t'
<< age << '\t';
if(address!=NULL)
cout << address << endl;
cout << endl;
}
Person::~Person(){
if(address!=NULL)
delete[] address;
}
void main()
{
Person p1;
p1.name_change('Zhang');
p1.age_change(20);
p1.address_change('Tsinghua University');
p1.address_change('Tsinghua');
p1.info_display();
writeToFile(' ');
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码。函数sort(int m,int n,int l)实现将三个整数m、n、l由大到小输出。m最大,l最小。
程序分析:程序实现时,可以把最大的数放到m上,先将m与n进行比较,如果m<n则将m与n的值进行交换,然后再用m与l进行比较,如果m<l则将m与l的值进行交换,这样能使m最大。然后再将n与l进行比较,若n<l则将n与l的值互换,互换后则l最小。
将函数sort(int m,int n,int l)补充完整,实现三个数的排序。
注意:请勿改动主函数。
#include <iostream.h>
void sort(int m, int n, int l)
{
}
int main()
{
int x=9;
int y=13;
int z=-3;
sort(x,y,z);
cout<<x<<','<<y<<','<<z<<endl;
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2。其中有类Point(“点”)、Rectangle(“矩形”)和Circle(“圆”)的定义。在程序所使用的平面坐标系统中,x轴的正方向是水平向右的,y轴的正方向是竖直向下的。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应该是:
--圆形----------
圆心=(3,2)
半径=1
面积=3.14159
--外切矩形----------
左上角=(2,1)
右下角=(4,3)
面积=4
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
#include <cmath>
using namespace std;
//平面坐标中的点
//本题坐标系统中,x轴的正方向水平向右,y轴的正方向竖直向下。
class Point {
public:
Point(double x=0.0, double y=0.0): x_(x), y_(y) {}
double getX() const {return x_;}
double getY() const {return y_;}
void setX(double x) {x_=x;}
void setY(double y) {y_=y;}
private:
double x_; //x坐标
double y_; //y坐标
};
//矩形
class Rectangle {
public:
Rectangle (Point p, int w, int h)
: point (p), width (w), height (h) {}
double area() const //矩形面积
{
return width * height;
}
Point topLeft() const //左上角顶点
{
return point;
}
Point bottomRight() const
//右下角顶点(注:y轴正方向竖直向下)
{
// **********found**********
return Point(______);
}
private:
Point point; //左上角顶点
double width; //水平边长度
double height; //垂直边长度
};
//圆形
class Circle {
public:
Circle (Point p, double r):center(p), radius(r) {}
Rectangle boundingBox() const; //外切矩形
double area() const //圆形面积
{
// **********found**********
return PI *______;}
public:
static const double PI; //圆周率
private:
Point center; //圆心
double radius; //半径
};
const double Circle::PI = 3.14159;
Rectangle Circle::boundingBox() const
{
// **********found**********
Point pt(______);
int w, h;
// **********found**********
w = h =______;
return Rectangle(pt, w, h);
}
int main ()
{
Point p(3, 2);
Circle c(p, 1);
cout << --圆形---------- \n';
cout << '圆心 = (' << p.getX() << ',' << p.getY() << ')\n';
cout << '半径 =' << 1 << endl;
cout << '面积 =' << c.area() << endl << endl;
Rectangle bb = c.boundingBox();
Point t1 = bb.topLeft();
Point br = bb.bottomRight();
cout << '--外切矩形-----------\n';
cout << '左上角 = (' << t1.getX() << ',' << t1.getY() << ')\n';
cout << '右下角 = (' << br.getX() << ',' << br.getY() << ')\n';
cout << '面积 =' << bb.area() << endl;
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中包含抽象类Shape的声明,以及在此基础上派生出的类Rectangle和Circle的声明,二者都有计算对象面积的函数GetArea()和计算对象周长的函数GetPerim()。程序中位于每个“//****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
The area of the Circle is 78.5
The perimeter of the Circle is 31.4
The area of the Rectangle is 24
The perimeter of the Rectangle is 20
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
using namespace std;
class Shape
{
public:
Shape() {}
~Shape() {}
//********** found**********
______float GetArea () =0;
//********** found**********
______float GetPerim () =0;
};
class Circle : public Shape
{
public:
Circle (float radius): itsRadius (radius) {}
~Circle() {}
float GetArea () { return 3.14 * itsRadius * itsRadius; }
float GetPerim ()
{return 6.28 * itsRadius;}
private:
float itsRadius;
};
class Rectangle : public Shape
{
public:
//********** found**********
Rectangle(float len, float width):
______{};
~Rectangle(){};
virtual float GetArea()
{ return itsLength * itsWidth; }
float GetPerim ()
{ return 2 * itsLength + 2 * itsWidth;}
virtual float GetLength() { return itsLength; }
virtual float GetWidth () { return itsWidth; }
private:
float itsWidth;
float itsLength;
};
int main()
{
//********** found**********
______
sp = new Circle(5);
cout << 'The area of the Circle is' << sp->GetArea () << endl;
cout << 'The perimeter of the Circle is' << sp->GetPerim () << endl;
delete sp;
sp = new Rectangle(4, 6);
cout << 'The area of the Rectangleis' << sp->GetArea() << endl;
cout << 'The perimeter of the Rectangle is' << sp -> GetPerim () <<endl;
delete sp;
return 0;
}
应用题 请使用[答题]菜单命令或直接用VC6打开考生文件夹下的工程proj3,其中声明了一个人员信息类Person。在Person类中数据成员name、age和address分别存放人员的姓名、年龄和地址。构造函数Person用以初始化数据成员。补充编制程序,使其功能完整。在main函数中分别创建了两个Person类对象p1和p2,并显示两个对象信息,此种情况下程序的输出应为:
Jane 25 Beijing
Tom 22 Shanghai
注意:只能在函数Person中的“//********333********”和“//********666********”之间填入若干语句,不要改动程序中的其他内容。
//proj3.h
#include <iostream>
#include <string>
using namespace std;
class Person{
public:
char name[20];
int age;
char* address;
public:
Person (char* _name, int_age,char*_add =NULL); //构造函数
void info_display (); //人员信息显示
~Person (); //析构函数
};
void writeToFile (const char * path ='');
//proj3.cpp
#include <iostream>
#include <string>
#include 'proj3.h'
using namespace std;
Person::Person (char*_name, int_age, char* _add) :age (_age)
{
//把字符串_name复制到数组name中
//使address指向一个动态空间,把字符串_add复制到该数组中。
//******** 333********
//******** 666********
}
void Person::info_display ()
{
cout <<name << '\t' <<age << '\t';
if (address!=NULL)
cout << address << endl;
}
Person:: ~Person ()
{
if (address !=NULL)
delete[] address;
}
void main ()
{
char add[100];
strcpy(add, 'Beijing') ;
Person p1 ('Jane', 25, add) ;
p1.info_display () ;
strcpy(add, 'Shanghai');
Person * p2 =new Person ('Tom', 22, add);
p2 -> info_display () ;
delete p2;
writeToFile ('');
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有类CPolygon(“多边形”)、CRectangle(“矩形”)、CTriangle(“三角形”)的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序的正确输出结果应为:
20
10
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
class CPolygon {
public:
// **********found**********
______//纯虚函数area声明
void printarea (void)
// **********found**********
{cout <<______<< endl;}
};
class CRectangle: public CPolygon {
int width; //长方形宽
int height; //长方形高
public:
CRectangle (int w, int h): width (w), height (h) {}
int area (void) {return (width * height);}
};
class CTriangle : public CPolygon {
int length; //三角形一边长
int height; //该边上的高
public:
CTriangle (int 1, int h): length (1), height (h) {}
// **********found**********
int area (void) {return (______)/2;}
};
int main() {
CRectangle rect (4,5);
CTriangle trgl (4,5);
// **********found**********
______*ppoly1, *ppoly2;
ppoly1 = ▭
ppoly2 = trgl;
ppoly1 -> printarea();
ppoly2 -> printarea();
return 0;
}