实验五

1.      实验名称:实验五

1. 编写一个程序由类Date和类Time派生出类DateTime的实例

2.编写一个程序由类People派生出类Student的实例

2.实验时间:2008-4-29

3.实验目的:编程用面向对象设计思想分析、设计并实现实验五的内容。

4.实验内容:

1.编写一个程序,程序中首先声明日期(年、月、日)类Date和时间(时、分、秒)类Time,并由这两个基类派生出日期和时间类DateTime,最后在主函数中分别利用类的默认构造函数和重载构造函数声明类DateTime 的对象,测试类声明的正确性。

2.编写一个程序,程序中首先声明人(姓名、年龄、性别)类People,并由它派生出学生(增加系号、专业、学号属性)类Student,最后在主函数中分别利用类的重载构造函数、拷贝构造函数(对象属性仅姓名和学号与用重载构造函数声明的不同)和默认构造函数声明类Student的对象,测试类声明的正确性。

/*源代码*/

/*DateTime.cpp*/

#include <iostream.h>

class Date

{

private:

       int year;

       int month;

       int day;

public:

       Date()

       {

              year=2010;

              month=7;

              day=9;

       }

       Date(int y,int m,int d)

       {

              year=y;

              month=m;

              day=d;

       }

       PrintDate()

       {

              cout<<year<<""<<month<<""<<day<<""<<endl;

       }

};

class Time

{

private:

       int hour;

       int minute;

       int second;

public:

       Time()

       {

              hour=12;

              minute=15;

              second=30;

       }

       Time(int h,int m,int s)

       {

              hour=h;

              minute=m;

              second=s;

       }

       PrintTime()

       {

              cout<<hour<<""<<minute<<""<<second<<""<<endl;

       }

};

class DateTime:public Date,public Time

{

public:

       DateTime(){}

       DateTime(int a,int b,int c,int d,int e,int f):Date(a,b,c),Time(d,e,f)

       {

       }

       PrintDateTime()

       {

              cout<<"该对象对应的时间是:"<<endl;

              Date::PrintDate();

              Time::PrintTime();

       }

};

void main()

{

       //利用类的默认构造函数声明类DateTime的对象,并且测试类声明的正确性

       cout<<"//利用类的默认构造函数声明类DateTime的对象,并且测试类声明的正确性"<<endl;

       DateTime DTdefault;

       DTdefault.PrintDateTime();

       //利用类的重载构造函数声明类DateTime的对象,并且测试类声明的正确性

       cout<<"//利用类的默认构造函数声明类DateTime的对象,并且测试类声明的正确性"<<endl;

       DateTime DT(2008,4,29,15,14,30);

       DT.PrintDateTime();

}

//实验结果

//利用类的默认构造函数声明类DateTime的对象,并且测试类声明的正确性

该对象对应的时间是:

201079

121530

//利用类的默认构造函数声明类DateTime的对象,并且测试类声明的正确性

该对象对应的时间是:

2008429

151430

Press any key to continue

/*Student.cpp*/

#include <iostream.h>

#include <string.h>

#define Man 1

#define Female 0

class People

{

private:

       char name[10];

       int age;

       int sex;

public:

       People(){}

       People(char *n,int a,int s)

       {

              strcpy(name,n);

              name[9]='/0';

              age=a;

              sex=s;

       }

       void setitsname(char *itsname)

       {

              strcpy(name,itsname);

              name[9]='/0';

       }

       void setitsage(int itsage){age=itsage;}

       void setitssex(char *itssex)

       {

        if(itssex=="")

                     sex=0;

              else

                  sex=1;          

       }

       char *getitsname(){return name;}

       int getitsage(){return age;}

       char *getitssex()

       {

              if(sex==0)

                     return "";

              else

                  return "";

       }

};

class Student:public People

{

private:

       char Departnum[10];

       char Major[21];

       char Xuehao[13];

public:

       Student()

       {

        strcpy(Departnum,"计教系");

              strcpy(Major,"计算机科学与技术专业");

              strcpy(Xuehao,"200624100003");

              People::setitsname("王五");

              People::setitsage(20);

              People::setitssex("");

       }

       Student(char *st_dnum,char *st_m,char *st_num,char *st_name,int st_age,int st_sex):People(st_name,st_age,st_sex)

       {

              strcpy(Departnum,st_dnum);

              Departnum[9]='/0';

              strcpy(Major,st_m);

              Major[20]='/0';

              strcpy(Xuehao,st_num);

              Xuehao[12]='/0';

       }

       Student(Student& st)

       {

              strcpy(Departnum,st.Departnum);

              Departnum[9]='/0';

              strcpy(Major,st.Major);

              Major[20]='/0';

              strcpy(Xuehao,st.Xuehao);

              Xuehao[12]='/0';

              People::setitsname(st.getitsname());

              People::setitsage(st.getitsage());

              People::setitssex(st.getitssex());

       }

       void copy_obj_setData(char *itsname,char *xuehao)

       {

        strcpy(Xuehao,xuehao);

              Xuehao[12]='/0';

              People::setitsname(itsname);

       }

       void Print()

       {

              cout<<"该学生的系号:"<<Departnum<<endl;

              cout<<"该学生的专业:"<<Major<<endl;

              cout<<"该学生的学号:"<<Xuehao<<endl;

              cout<<"该学生的名字:"<<People::getitsname()<<endl;

              cout<<"该学生的年龄:"<<People::getitsage()<<endl;

              cout<<"该学生的性别:"<<People::getitssex()<<endl;

       }

};

void main()

{

       //利用类的重载构造函数声明类Student的对象,测试类声明的正确性

       Student Zhangsan("计教系","计算机科学与技术专业","200624100001","张三",21,Man);

       Zhangsan.Print();

       //利用类的拷贝构造函数(对象属性仅姓名和学号与用重载构造函数声明的不同)声明类Student的对象,测试类声明的正确性

       Student Lisi(Zhangsan);

       Lisi.copy_obj_setData("李四","200624100002");

       Lisi.Print();

       //利用类的默认构造函数声明类Student的对象,测试类声明的正确性

       Student Wangwu;

       Wangwu.Print();

}

/**************************************************************************

 * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *

 * Pearson Education, Inc. All Rights Reserved.                           *

 *                                                                        *

 * DISCLAIMER: The authors and publisher of this book have used their     *

 * best efforts in preparing the book. These efforts include the          *

 * development, research, and testing of the theories and programs        *

 * to determine their effectiveness. The authors and publisher make       *

 * no warranty of any kind, expressed or implied, with regard to these    *

 * programs or to the documentation contained in these books. The authors *

 * and publisher shall not be liable in any event for incidental or       *

 * consequential damages in connection with, or arising out of, the       *

 * furnishing, performance, or use of these programs.                     *

 **************************************************************************/

//实验结果

该学生的系号:计教系

该学生的专业:计算机科学与技术专业

该学生的学号:200624100001

该学生的名字:张三

该学生的年龄:21

该学生的性别:

该学生的系号:计教系

该学生的专业:计算机科学与技术专业

该学生的学号:200624100002

该学生的名字:李四

该学生的年龄:21

该学生的性别:

该学生的系号:计教系

该学生的专业:计算机科学与技术专业

该学生的学号:200624100003

该学生的名字:王五

该学生的年龄:20

该学生的性别:

Press any key to continue

5.实验总结:通过对类的继承的应用,并且在主函数中分别利用类的重载构造函数、拷贝构造函数(对象属性仅姓名和学号与用重载构造函数声明的不同)和默认构造函数声明类Student的对象,测试类声明的正确性,更加熟练的掌握面向对象中类的用法和思想。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值