问答题

请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中使用友元函数访问类的私有数据成员,求出两个数据成员的大于1的最小公因子。请编写友员函数FriFun,使其输出结果为: Common denominator is 2 要求:补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 //proj3.h class FriFunClass { int a, b; public: FriFunClass(int i, int j) { a=i; b=j;} friend int FriFun (FriFunClass x); //友元函数 }; void writeToFile(const char *); //proj3.cpp #include <iostream> using namespace std; #include "prj3.h" int FriFun (FriFunClass x) { //******** 333******** //由于函数FriFun()是类FriFunClass //******** 666******** } int main ( ) { FriFunClass n(10, 20); if (FriFun (n)) cout <<"Common denominator is "<<FriFun(n)<<"\n"; else cout <<"No common denominator.\n"; writeToFile(""); return 0; }

【参考答案】

int min=x.a<x.bx.a:x.b; //此处为取出x.a与x.b中的最小值 for(int i=2; i<......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:车牌号:冀ABCl234 品牌:ForLand 类别:卡车 当前档位:0 最大载重量:12车牌号:冀ABC1234 品牌:ForLand 类别:卡车 当前档位:2 最大载重量:12车牌号:沪XYZ5678 品牌:QQ 类别:小轿车 当前档位:0 座位数:5车牌号:沪XYZ5678 品牌:QQ 类别:小轿车 当前档位:-1 座位数:5注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“ ****found****”。#include <iostream>#include <iomanip>#include <cmath>using namespace std;class AutoMobile{ 汽车 类char * brand; 汽车品牌char * number; 车牌号int speed; 档位: 1、2、3、4、5, 空档: 0,倒档: -1public:AutoMobile (const char * the_brand, const char * the_number): speed(0) {brand = new char [strlen (the_brand) +1]; ********** found**********______; ********** found**********______;strcpy (number, the_number);}~AutoMobile ( ) {delete[ ] brand;delete[] number; }const char * theBrand const { reurn brand; } 返回品牌名称const char * theNumber const{ return number; } 返回车牌号int currentSpeed const { returnspeed; } 返回当前档位void changeGearTo (int the speed)if(speed>= -1 && speed<=5)speed = the_speed;}virtual const char * category const =0; 类别: 卡车、小轿车等virtual void show const {cout << 车牌号 << theNumber **********found**********<< 品牌: <<<< 类别: << category ;}};class Car: public AutoMobile {int seats; 座位数public:Car (const char * the_brand, constchar * the_number, int the seats):AutoMobile (the_brand, the _number),seats (the seats) {}int numberOfSeat const { returnseats; }const char * category const{ return 小轿车 ; } 返回汽车类别void show const{AutoMobile::show ;cout << 座位数 <<numberOfSeat <<endl;}};class Truck: public AutoMobile { int max_load; 最大载重量public:Truck (const char * the _brand,const char * the number, int the_max_load): AutoMobile (the_brand, the_number), max_load (the_max_load) { }int maxLoad const { return max_load; } 返回最大载重量 ********** found**********const char * category ______ 返回汽车类别void show const{AutoMobile::show ;cout << 最大载重量: <<maxmoad <<endl;}};int main {Truck truck ( ForLand , ABC1234 , 12);truck.show ;truck.changeGearTo(2);truck.show ;Car car( QQ , 沪XYZ5678 , 5);car.show ;car.changeGearTo(-1);car.show ;cout<<endl;return 0;}
问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中包含程序文件main.cpp,其中有ElectricFan(“电风扇”)类和主函数main的定义。程序中位于每个“ ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:品牌:清风牌,电源:关,风速:0品牌:清风牌,电源:开,风速:3品牌:清风牌,电源:关,风速:0注意:只修改每个“ ERRO****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;class ElectricFan{ 电扇 类char * brand;int intensity; 风速: 0-关机, 1-弱, 2-中, 3-强public:ElectricFan (const char * the_brand): intensity(0){brand = new char [ strlen (the_brand) +1];strcpy(brand, the_brand);}~ElectricFan() {delete []brand; } ERROR **********found**********const char * theBrand()const{ return* brand;} 返回电扇品牌int theIntensity () const { returnintensity; } 返回风速bool isOn()const{ return intensity>0;} ERROR **********found**********void turnOff() { intensity=1; } 关电扇void setIntensity (int inten){ ERROR **********found**********if (intensity>=i && intensity <=3)intensity =inten;}void show () {cout<< 品牌: <<theBrand () << 牌 << ,电源: <<(isOn () 开 : 关 )<< ,风速: <<theIntensity () <<endl;}};int main() {ElectricFan fan ( 清风 );fan. show () ;fan. setIntensity (3);fan. show ();fan. turnOff ();fan. show ();return 0;}