1.定义一个Book(图书)类,在该类定义中包括以下数据成员和成员函数。
数据成员:bookname(书名)、price(价格)和number(存书数量)。
成员函数:display()显示图书的情况;borrow()将存书数量减1,并显示当前存书数量;restore()将存书数量加1,并显示当前存书数量。
在main函数中,要求创建某一种图书对象,并对该图书进行简单的显示、借阅和归还管理。
#include<iostream>
#include<cstring>
using namespace std;
class Book
{
private:
string bookname;
double price;
int number;
public:
Book(string name,double pri,int num)
{
bookname = name;
price = pri;
number = num;
}
int borrow(int num)
{
if (num == 0)
{
cout << "NULL" << endl;
cout << "num=" << num << endl;
}
else
{
num = num - 1;
cout << num << endl;
}
return num;
}
int restore(int num)
{
num= num + 1;
cout << num << endl;
return num;
}
void display()
{
cout <<bookname<< endl;
cout << price << endl;
cout << number << endl;
}
};
int main()
{
string bname;
double bprice;
int bnum;
cout << "请输入图书的书名,价格,数量:" << endl;
cin >> bname >> bprice >> bnum;
Book b(bname,bprice,bnum);
cout << "借阅一本后的数量:" << endl;
b.borrow(bnum);
cout << "添加一本后的数量:" << endl;
b.restore(bnum);
cout << "显示图书的书名,价格,数量:" << endl;
b.display();
return 0;
}
2.改写以下程序。要求定义类student,封装三个数据成员和两个成员函数intpt和output,使程序得到相同的运行效果。需要加入构造函数给其参数默认初始化值为:( “\0”, 0, 0 )。
#include
using namespace std;
struct student
{ char name[20];
unsigned int id;
double score;
};
void input(student &stu)
{ cout<<“name?”;
cin>>stu.name;
cout<<“id?”;
cin>>stu.id;
cout<<“score?”;
cin>>stu.score;
}
void output(student &stu)
{ cout<<“name: “<<stu.name<<”\tid: “<<stu.id<<”\tscore: “<<stu.score<<endl; }
int main()
{ student s={”\0”, 0, 0};
input(s);
output(s);
}
#include <iostream>
using namespace std;
class Student
{
private:
string s_name;
unsigned int s_id;
double s_score;
public:
Student()
{
s_name = "\0";
s_id = 0;
s_score = 0;
}
void input()
{
cout << "name?" << endl;
cin >> s_name;
cout << "id?" << endl;
cin >> s_id;
cout << "score?" << endl;
cin >> s_score;
}
void output()
{
cout << "name:" << s_name << endl;
cout << "id:" << s_id << endl;
cout << "score:" << s_score << endl;
}
};
int main()
{
Student S;
string S_name;
unsigned int S_id;
double S_score;
S.input();
S.output();
return 0;
}
3.创建一个函数f1(),返回其参数的平方根。重载f1()三次,让它返回整数、长整数与双精度的平方根(计算平方根可以使用标准库函数sqrt())。
#include <iostream>
#include<cmath>
using namespace std;
int f1(int n)
{
return sqrt(n);
}
long int f1(long int n)
{
return sqrt(n);
}
double f1(double n)
{
return sqrt(n);
}
int main()
{
int m1;
cout << "Please input m1:" << endl;
cin >> m1;
cout<<f1(m1)<<endl;
long int m2;
cout << "Please input m2:" << endl;
cin >> m2;
cout<<f1(m2)<<endl;
double m3;
cout << "Please input m3:" << endl;
cin >> m3;
cout<<f1(m3)<<endl;
return 0;
}
4、建立一个Student类,含有私有的学号(字符型)、姓名(字符型)、年龄(整型)数据成员。用new自动为Student的对象分配内存,并将学号“0504020101”,姓名“张三”,年龄20存入内存的相应域中,把该生的信息显示出来。提示:用new创建对象并调用的是有参构造函数的格式为:类名 *指针变量=new 类名(实参1,实参2,…);(根据自己掌握的情况,需要的话可借助课件复习此知识点)
#include <iostream>
using namespace std;
class Student
{
private:
string num;
string name;
int age;
public:
Student(string num, string name, int age)
{
this->num = num;
this->name = name;
this->age = age;
}
void output()
{
cout << "num=" << num << endl;
cout << "name=" << name << endl;
cout << "age=" << age << endl;
}
};
int main()
{
string S_num ;
string S_name;
int S_age;
cout << "Please input num:" << endl;
cin >> S_num;
cout << "Please input name:" << endl;
cin >> S_name;
cout << "Please input age:" << endl;
cin >> S_age;
Student* S = new Student(S_num, S_name, S_age);
S->output();
delete S;
return 0;
}
5、定义学生类,属性包括:学生姓名、语文成绩、数学成绩。完成:置学生信息、输出学生信息、求每个学生的平均成绩、求每个学生的总分、输出每个学生的平均成绩和总分。(要求用对象数组、构造函数完成)提示:考虑学生人数不定的情况,可用new建立动态对象数组(用new创建动态对象数组同用new创建其它数据类型的动态数组的操作相同)。
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
string name;
double cscore;//语文成绩
double mscore;//数学成绩
public:
Student()
{
cin >> name;
cin >> cscore;
cin >> mscore;
}
void print()
{
cout << name<<' ' << cscore<<' ' << mscore;
}
double average()
{
return (cscore + mscore) / 2.0;
}
double sum()
{
return (cscore + mscore);
}
};
int main()
{
int n,i;
string sname;
double chinese,math;
cout << "请输入学生个数:" << endl;
cin >>n;
cout << "请输入学生姓名、语文成绩、数学成绩:" << endl;
Student* stu = new Student[n];
cout << "显示:" << endl;
for (i = 0; i < n; i++)
{
stu[i].print();
cout << endl;
}
cout << "学生的平均成绩和总成绩为:" << endl;
for (i = 0; i < n; i++)
{
cout << stu[i].average()<<' '<<stu[i].sum() << endl;
}
return 0;
}
6.定义一个圆类(Circle),属性为半径(radius)、圆周长和面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。要求定义构造函数(以半径为参数、默认值为0,周长和面积在构造函数中生成)和复制(拷贝)构造函数。在主函数中进行测试。
#include<iostream>
using namespace std;
class circle
{
double R;//半径
double C;//周长
double S;//面积
public:
circle(double r = 0)
{
R = r;
C = 2 * 3.14 * R;
S = 3.14 * R * R;
}
circle(circle& c)
{
R = c.R;
C = c.C;
S = c.S;
}
void print()
{
cout << R <<' '<< C<<' ' << S << endl;
}
};
int main()
{
double radius;
cout << "请输入半径长:" << endl;
cin >> radius;
circle c(radius);
circle C(c);
cout << "输出半径、周长、面积:" << endl;
c.print();
C.print();
return 0;
}
7.设计一个学校在册人员类,数据成员包括身份证号(id,可定义成字符串类型)、姓名(name)、性别(sex)、生日(birth,可定义成结构体类型)和家庭住址(homeadd)。成员函数包括人员信息的录入和显示,还包括构造函数和拷贝构造函数。在主函数中进行测试。
#include<iostream>
#include<cstring>
using namespace std;
struct Data
{
int year, mouth, day;
};
class People
{
string id;
string name;
string sex;
string homeadd;
Data data;
public:
People(string i, string n, string s, string h, Data d)
{
id = i;
name = n;
sex = s;
homeadd = h;
data = d;
}
People(People& p)
{
id = p.id;
name = p.name;
sex = p.sex;
homeadd = p.homeadd;
data = p.data;
}
void print()
{
cout << id << endl;
cout << name << endl;
cout << sex << endl;
cout << homeadd << endl;
cout << data.year<<' '<<data.mouth<<' '<<data.day << endl;
}
};
int main()
{
Data da;
string Id, Name, Sex, Homeadd;
cout << "请输入学号、姓名、性别、家庭住址:" << endl;
cin >> Id>> Name>>Sex>>Homeadd;
cout << "请输入出生日期:" << endl;
cin >> da.year >> da.mouth >> da.day;
People p1(Id,Name,Sex,Homeadd,da);
People p2(p1);
cout << "显示:" << endl;
p1.print();
p2.print();
return 0;
}
实验心得:
实验四的第四题我自己写出了无参情况下的代码,有参的写不出来,然后我请教了班里的同学,在同学的帮助下理解了。还有第六七题的拷贝构造,有点遗忘了。所以!学过的知识点要温故而知新!