C++实验(1) 类与对象
一、实验目的和要求
1.掌握类、类的数据成员、类的成员函数的定义方式。
2.理解类成员的访问控制方式。
3.掌握对象的定义和操作对象的方法。
4.理解构造函数和析构函数的定义与执行过程。
5.掌握重载构造函数的方法。
6.了解拷贝构造函数的定义方法。
二、实验内容
1.请定义一个矩形类(Rectangle),私有数据成员为矩形的长度( len)和宽度(wid),缺省构造函数置len和wid为0,有参构造函数置len和wid为对应形参的值,另外还包括求矩形周长、求矩形面积、取矩形长度和宽度、修改矩形长度和宽度为对应形参的值、输出矩形尺寸等公有成员函数。要求输出矩形尺寸的格式为“length:长度,width:宽度”。编写主函数对定义的类进行测试。
2.声明一个时间类,时间类中有3个私有数据成员(Hour,Minute,Second)和两个公有成员函数(SetTime和PrintTime)。SetTime根据传递的3个参数为对象设置时间;PrintTime负责将对象表示的时间显示输出,输出格式为“Hour:Minute:Second”。
(1)在主函数中,建立一个时间类的对象,设置时间为9点20分30秒并显示该时间。
(2)使用构造函数代替上面的SetTime成员函数,并在主函数中使用构造函数设置时间为10点40分50秒,并显示该时间。
(3)重载时间类的构造函数(不带参数)使小时、分、秒均为0。
(4)在时间类的析构函数中输出"Good bye!”
(5)定义拷贝构造函数并调用。
3.设计一个用于人事管理的People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中"出生日期"定义为一个"日期"类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、带缺省形参值的成员函数。#include "iostream"
#include "string"
using namespace std;
class Rectangle
{
private: double m_Len, m_Wid;
public:
Rectangle()
{
m_Len = 0;
m_Wid = 0;
}
Rectangle(double len, double wid)
{
m_Len = len;
m_Wid = wid;
}
~Rectangle()
{
}
void Set_Rectangle()
{
cout << "请输入矩形的长度和宽度: ";
cin>> m_Len >> m_Wid;
}
double Get_Rectangle_S()
{
double S;
S = m_Len * m_Wid;
return S;
}
double Get_Rectangle_Length()
{
return m_Len;
}
double Get_Rectangle_Width()
{
return m_Wid;
}
void Rec_Display()
{
cout << "长度: " << m_Len << " 宽度:" << m_Wid << endl;
}
};
int main()
{
Rectangle rectangle;
rectangle.Set_Rectangle();
rectangle.Rec_Display();
cout << "矩形面积: " << rectangle.Get_Rectangle_S() << endl;
return 0;
}
(2)
#include "iostream"
using namespace std;
class Time
{
private: int Hour,Minute,Second;
public:
Time() //重载构造函数
{
Hour = 0;
Minute = 0;
Second = 0;
}
Time(int hour,int minute,int secend) //构造函数
{
Hour = hour;
Minute = minute;
Second = secend;
}
~Time() //析构函数
{
cout << "Good bye! " << endl;
}
void SetTime()//Set时间
{
cout << "Please input time: ";
cin >> Hour >> Minute >> Second;
}
void PrintTime()//输出函数
{
cout << "Time: " << Hour << ":" << Minute << ":" << Second << endl;
}
};
int main()
{
Time time;
time.SetTime();
time.PrintTime();
Time time2(10,40,50);
time2.PrintTime();
Time time3;
time3.PrintTime();
return 0;
}
(3)
#include "iostream"
#include "string"
using namespace std;
class Date
{
public: int m_Year;
int m_Month;
int m_Day;
Date() {}
Date(int year,int month,int day) //构造函数
{
m_Year = year;
m_Month = month;
m_Day = day;
}
~Date() {}
Date(Date &refdate)
{
m_Year = refdate.m_Year;
m_Month = refdate.m_Month;
m_Day = refdate.m_Day;
}
inline void Date_print() //内联函数
{
cout<< m_Year << "-" << m_Month << "-" << m_Day << endl;
}
void Set_Date()
{
cin >> m_Year >> m_Month >> m_Day;
}
};
class People
{
public:int m_Number;
string m_Sex;
int m_Id;
Date Birthday; //类内嵌子对象
People() {}
People(int number,string sex,int id,int year, int month, int day):Birthday(year, month, day)
{
m_Number = number;
m_Sex = sex;
m_Id = id;
}
~People() {}
People(People &refpeople) //拷贝函数
{
m_Number = refpeople.m_Number;
m_Sex = refpeople.m_Sex;
m_Id = refpeople.m_Id;
}
void Set_People()
{
cout << "录入信息:";
cin >> m_Number >> m_Sex >> m_Id;
Birthday.Set_Date();
}
void People_Print()
{
cout << "显示信息: " << m_Number << " " << m_Sex << " " << m_Id << " ";
Birthday.Date_print();
}
};
int main()
{
People people;
people.Set_People();//录入函数
people.People_Print();//显示函数
People people1(124, "男", 411720, 1967, 10, 23);//构造函数
people1.People_Print();
return 0;
}
以上仅作参考,不足之处请指出;