大一下c + +上机实验总目录:大一下c + +上机实验总结目录
1、假设图书馆的图书包含书名、编号和作者属性,读者包含姓名和借书证号属性,每个读者最多可借5本书。设计一个类object,从它派生出图书类book和读者类reader,在reader类中有一个rentbook()成员函数用于借阅图书。主函数进行测试。说明:这是一个简单的借阅过程。借阅时,假设要借阅的图书是存在的。
提示:(1)在基类object中定义字符数组(或string类型)的name和整型数据no,这两个数据成员被book类继承后,用于表示书名和编号,这两个数据成员被reader类继承后,用于表示读者姓名和借书证号;(2)book类新增数据成员:作者(字符数组或string类型);reader类新增数据成员:目前借书的数量(整型)top、所借图书的信息(可定义成book类对象数组,book rent[5]);(3)reader类中的成员函数rentbook()的形参可以设置为book类对象的引用,主函数中每调用一次rentbook(),表示借阅一本书,所以rentbook()函数体代码:{rent[top]=b; top++;}。
参考答案:
#include<>
#include<>
class object{
char name[20];
int no;
public:
object(){}
object(char na[],int n)
{strcpy(name,na);no=n;
}
void show();
};
void object::show()
{cout<<name<<"("<<no<<")";
}
class book:public object{
char author[10];
public:
book(){}
book(char na[],int n,char auth[]):object(na,n)
{strcpy(author,auth);
}
void showbook();
};
void book::showbook()
{show();
cout<<"作者:"<<author;
}
class reader:public object{
book rent[5];
int top;
public:
reader(char na[],int n):object(na,n){top=0;}
void rentbook(book &b);
void showreader();
};
void reader::rentbook(book &b)
{rent[top]=b;
top++;
}
void reader::returnbook(book &b)
{ top--;
}
void reader::showreader()
{cout<<"读者:";
show();
cout<<endl<<"所借图书:"<<endl;
for(int i=0;i<top;i++)
{cout<<" "<<i+1<<":";//5个空格
rent[i].showbook();
cout<<endl;
}
}
void main()
{book b1("C语言",100,"谭浩强"),b2("数据结构",110,"严蔚敏");
reader r1("张山",6666);
r1.rentbook(b1);
r1.rentbook(b2);
r1.showreader();
}
2、
定义一个基类person
姓名、性别、年龄(访问权限设置为保护)
定义带参的构造函数(类中只包含该构造函数)
重载<<运算符,显示person的信息
重载>>运算符,输入person的相关信息
再由基类派生出学生类
增加学号、班级、专业和入学成绩
是否要定义构造函数?
重载<<运算符,显示student的信息(友元关系是不能继承的)
重载>>运算符,输入student的相关信息
参考答案:
#include<iostream>
#include <string>
using namespace std;
class Person
{
protected:
string name;
int age;
string sex;
public:
Person(string name="", int age=0, string sex="") {
this->name=name; this->age=age; this->sex=sex; }
friend istream &operator >>(istream &in, Person & p);
friend ostream& operator <<(ostream &out, Person &p);
};
istream &operator >>(istream &in, Person & p) {
cout<<"name\tage\tsex"<<endl;
in>>p.name>>p.age>>p.sex;
return in;
}
ostream& operator <<(ostream &out, Person &p) {
out<<p.name<<" "<<p.age<<" "<<p.sex<<endl;
return out;
}
class student :public Person{
string no;
string zhuanye;
string t_class;
float score;
public:
student(string name="",int age=0,string sex="",string no="",string zhuanye="", string t_class="", float score=0):Person(name,age,sex)
{
this->no=no;
this->zhuanye=zhuanye;
this->t_class=t_class;
this->score=score;
}
friend istream & operator >>(istream & in , student & s);
friend ostream & operator <<(ostream & out, student & s);
};
istream & operator >>(istream & in , student & s){
cout<<"no\tname\tage\tsex\tzhuanye\tt_class\tscore"<<endl;
in>>s.no>>s.name>>s.age>>s.sex;
in>>s.zhuanye>>s.t_class>>s.score;
return in;
}
ostream & operator <<(ostream & out, student & s) {
out<<s.no<<" "<<s.name<<" "<<s.age<<" "<<s.sex<<" ";
out<<s.zhuanye<<" "<<s.t_class<<" "<<s.score<<endl;
return out;
}
int main()//用做测试的主函数,可以自己任意编写
{
student t1,t2;
cin>>t1>>t2;
cout<<t1<<endl<<t2<<endl;
return 0;
}
3、根据下列描述,编写程序,要求在主函数中测试以下行为:父亲开车;母亲唱歌、在外打工;孩子开车、唱歌和打篮球。具体描述如下:
三口之家,每个人都有姓名,身份证号,出生日期。
父亲会开车。会修电视(只有家里人知道)。
母亲会唱歌。母亲瞒着任何人在外打工。
小孩会开车,唱歌,修电视,此外,小孩还会打球。
提示:采用虚继承。定义一个Person类,为虚基类。从Person类派生出Father类和Mother类,由Father类和Mother类共同派生出Child类。
参考答案:
class Person
{
protected:
string name;
string id;
Date birthday;
public:
Person(int x,int y,int z,string a_name, string a_id):birthday(x,y,z),name(a_name),id(a_id)
{
}
Person()
{}
};
class Father:virtual public Person{
public :
Father(int x,int y,int z,string a_name, string a_id):Person(x,y,z,a_name,a_id) {}
void Drive() {
cout<<name<<" is driving..............."<<endl;
}
Father() {}
friend class Mother;
protected:
void RepairTV() {
cout<<name<<" is repairing TV set.........."<<endl;
}
};
class Mother:virtual public Person{
public:
Mother(int x,int y,int z,string a_name, string a_id):Person(x,y,z,a_name,a_id) { }
Mother() {}
void Sing() {
cout<<name<<"is singing....."<<endl;
}
friend void hourwork(Mother m);
private:
void Work() {
cout<<name<<" is working.........."<<endl;
}
};
void hourwork(Mother m){
m.Work ();
}
class Child:public Father,public Mother
{
public:
Child(int x,int y,int z,string a_name, string a_id):Person(x,y,z,a_name,a_id) {}
void PlayBasketBall()
{
cout<<name<<"is Play pingpong........"<<endl;
}
};
int main()
{
Father f(10,10,1967,"王小二","379008720123234");;
Mother m(10,20,1970,"小龙女","3790070090983");
Child c(2,12,1999,"王五","9800384340234");
f.Drive();
m.Sing();
hourwork(m);
c.PlayBasketBall ();
c.Drive();
c.Sing();
return 0;
}
4、定义猫科动物Animal类,由其派生出猫类(Cat)和豹类(Leopard),二者都包含虚函数speak(),要求根据派生类对象的不同来调用各自重载后的成员函数。
参考答案:
#include<>
class Animal{
public:
virtual void Speak()=0;
};
class Cat:public Animal{
void Speak()
{cout<<"My name is Cat"<<endl;
}
};
class Leopard:public Animal{
void Speak()
{cout<<"My name is Leopard"<<endl;
}
};
int main()
{Animal *pa;
Cat cat;
pa=&cat;
pa->Speak();
Leopard leopard;
pa=&leopard;
pa->Speak();
return 0;
}