1.
指出下列程序的错误并改正
#include <iostream.h>
void swap(int &a,int &b)
{ a=a+b;
b=a-b;
a=a-b;
}
void main()
{int a=19,b=15;
cout<<"a="<<a<<",b="<<b<<endl;
swap(&a,&b);
cout<<"a="<<a<<",b="<<b<<endl;
}
#include <iostream> //头文件错误
using namespace std;
void swap(int &a,int &b)
{ a=a+b;
b=a-b;
a=a-b;
}
int main()
{
int a=19,b=15;
cout<<"a="<<a<<",b="<<b<<endl;
swap(a,b); //引用错误
cout<<"a="<<a<<",b="<<b<<endl;
return 0;
}
2.
指出下列程序的错误并改正
#include <iostream.h>
class A
{public:
void A(int i=0){m=i}
void show(){cout<<m;}
void ~A(){}
private:
int m;
};
void main()
{
A a(5);
a.m+=10;
a.show();
}
#include <iostream>
using namespace std; //未引入std命名空间
class A
{public:
A(int i=0){m=i;}//构造函数没有返回值
void show(){cout<<m;}
~A(){}//析构函数没有返回值
int m;//在主函数无法直接调用私有的属性
};
int main()
{
A a(5);
a.m+=10;
a.show();
return 0;
}
3.
指出下列程序的错误并改正
#include <iostream.h>
class A
{ private:
int x;
public:
A(int i){x=i;}
A(){x=0;}
friend int min(A&,A&);
};
int A::min(A &a,A &b)
{return(a.x>b.x)?a.x:b.x;}
void main()
{ A a(3),b(5);
cout<<a.min(b)<<endl;}
#include <iostream>
using namespace std;
class A
{
private:
int x;
public:
A(int i){x=i;}
A(){x=0;}
friend int min(A&,A&);
};
int A::min(A &a,A &b)
{
return (a.x<b.x)?a.x:b.x;//根据函数名求的是最小值
}
int main()
{
A a(3),b(5);
cout<<a.min(a,b)<<endl; //声明的形参数是两个
return 0;}
4.
分析下面程序中的错误
class X{
private:
int a = 0, &b;
const int c;
void setA(int i){a = i;}
X(int i){ a = i}
public:
int X(){ a = b = c =0};
X(int i ,int j, int k){ a = i; b = j; c = k;}
static void setB(int k){ b = k;}
setC(int k) const { c = c + k;}
};
void main(){
X x1;
X x2(2);
X x3(1,2,3);
x1.setA(3);
}
#include<iostream>
using namespace std;
class X{
private:
int a; //成员变量不可直接赋初值
int& b;
const int c;
public:
void setA(int i){a = i;} //主函数不可直接调用私有成员函数
X(int i):b(b),c(c){ a = i;} //引用变量b需赋初值
X():b(b),c(c){a=0;}
X(int i ,int j, int k):b(j),c(k){ a = i;}//引用的构造函数需用初始化表
void setB(int k){
b=k;
}
setC(int k) const {
int* p;
p = (int*)&c;
*p += k;}
};
int main(){
X x1;
X x2(2);
X x3(1,2,3);
x1.setA(3);
return 0;
}
5. (论述题) 建立一个对象数组,内放5个学生的数据(学号、成绩),建立一个函数max,用指向对象的指针作为函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
#include<iostream>
using namespace std;
class Student{
public:
string num;
int grade;
Student(){
}
Student(string n,int g){
num=n;
grade=g;
}
};
void max(Student *s){
Student *p=s;
for(int i=1;i<5;i++)
{
if(s[i].grade>p->grade) p=s;
}
cout<<p->num;
}
int main()
{
Student str[5]={
Student("202107044123",99),
Student("202107044124",87),
Student("202107044125",89),
Student("202107044126",94),
Student("202107044127",73)
};
max(str);
return 0;
}
6.建立一个结点包括职工的姓名、年龄(整数)和性别(0表示女,1表示男)的单向链表,分别定义函数完成以下功能:
(1)遍历该链表输出全部职工信息;
(2)在链表尾部插入新职工结点;(主函数建立简单菜单选择,测试你的程序)
#include<iostream>
#include<string>
using namespace std;
struct Employee
{
string name;
int age;
string sex;
struct Employee *next;
};
class EmployeeLink
{
struct Employee *pEmployee;
int number;
public:
EmployeeLink();
~EmployeeLink();
void DisplayAll();
int Insert();
};
EmployeeLink::EmployeeLink()
{
pEmployee=new Employee;
pEmployee->name="";
pEmployee->age=0;
pEmployee->sex="";
pEmployee->next=NULL;
number=0;
}
EmployeeLink::~EmployeeLink()
{
cout<<"Press any key to continue."<<endl;
}
void EmployeeLink::DisplayAll()
{
struct Employee *q;
q=pEmployee->next;
if(number==0) cout<<"无职工信息!!"<<endl;
else cout<<"当前共有【"<<number<<"】名职工"<<endl;
while(q!=NULL)
{
cout<<"职工姓名:"<<q->name<<endl;
cout<<"年龄:"<<q->age<<endl;
cout<<"性别:"<<q->sex<<endl;
q=q->next;
}
}
int EmployeeLink::Insert()
{
struct Employee *head,*node;
int number1=0;
head=pEmployee;
while(number1<=number-1){
head=head->next;
number1++;
}
node=new Employee;
cout<<"请输入职工的姓名,年龄,性别(用空格或回车分隔)"<<endl;
string mingzi,xingbie;
int nianling;
cin>>mingzi>>nianling>>xingbie;
node->name=mingzi;
node->age=nianling;
node->sex=xingbie;
node->next=NULL;
head->next=node;
number++;
cout<<"插入成功!!"<<endl;
return 0;
}
int main()
{
EmployeeLink el;
int number;
cout<<"1、显示所有职工信息\n2、插入一个新职工\n3、退出程序\n";
while(1)
{
cout<<"请输入选项(1,2,3):";
cin>>number;
if(number==1) el.DisplayAll();
else if(number==2) el.Insert();
else if(number==3) break;
else cout<<"输入错误,请重新输入"<<endl<<"请输入选择:";
}
return 0;
}
7.定义一个Book(图书)类,在该类定义中包括:数据成员: bookname(书名)、price(价格)和number(存书数量);
成员函数: display()显示图书的情况;borrow()将存书数量减1,并显示当前存书数量;restore()将存书数量加1,并显示当前存书数量。
在 main函数中,要求创建某一种图书对象,并对该图书进行简单的显示、借阅和归还管理。
#include<iostream>
#include<string>
using namespace std;
class Book{
private:
string bookname;
double price;
int number;
public:
Book();
Book(string,double,int);
~Book();
void display();
void borrow();
void restore();
};
Book::Book(){
bookname="";
price=0;
number=0;
}
Book::Book(string name,double p,int num){
bookname=name;
price=p;
number=num;
}
Book::~Book(){
cout<<"Press any key to continue."<<endl;
}
void Book::display(){
if(number==0) {
cout<<"当前《"<<bookname<<"》的存量为0"<<endl;
cout<<"图书《"<<bookname<<"》当前无存量,请稍后再借!!"<<endl;
}
else cout<<"当前《"<<bookname<<"》的存量为"<<number<<endl;
}
void Book::borrow(){
number--;
cout<<"借阅成功,";
}
void Book::restore(){
number++;
cout<<"归还成功,";
}
int main()
{
cout<<"请分别输入图书的名称,价格,数量(已空格和回车分隔):"<<endl;
string bookname;
double price;
int number;
cin>>bookname>>price>>number;
Book b(bookname,price,number);
cout<<"1.借阅图书"<<endl<<"2.归还图书\n"<<"3.结束操作"<<endl;
int select;
while(1)
{
cout<<"请输入选项(1,2,3):";
cin>>select;
if(select==1) {b.borrow(); b.display();}
else if(select==2) {b.restore(); b.display();}
else if(select==3) break;
else cout<<"输入错误,请重新输入"<<endl<<"请输入选择:";
}
return 0;
}