您的位置:首页 > 理论基础

计算机程序设计(C++)第10周编程作业数据的抽象和封装——类(2)——构造函数、析构函数和指向对象的指针

2017-05-12 14:37 555 查看
/* 1定义一个带重载构造函数的日期类
题目内容:
定义一个带重载构造函数的日期类Date,数据成员有年、月、日;成员函数包括:一个带参数的构造函数Date(int,int,int),一个不带参数的构造函数,一个按“年-月-日”格式显示日期的函数,一个对数据成员赋值的函数void
init(int,int,int)。
主函数中对类的测试要求:
1. 分别使用两个不同的重载构造函数创建两个日期类对象(必须为d1,d2,d2初始值为2100-12-12);
2. 按“年-月-日”格式分别显示两个对象的值;
3. 输入数据,用init函数为d1赋值;
2.按“年-月-日”格式显示对象d1的值;。
输入格式:
给d1赋值的数据
输出格式:
d1的默认值
d2的初始值
d1赋值后的
输入样例:
2011 4 29
输出样例:
1900-1-1
2100-12-12
2011-4-29*/
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
class Date
{
int year;
int month;
int day;
public:
Date():year(1900),month(1),day(1){};
Date(int,int,int):year(2100),month(12),day(12){};
void Print();
void init(int,int,int);
};
void Date::Print(){
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
void Date::init(int year,int month,int day){
this->year=year;
this->month=month;
this->day=day;
}
int main()
{
Date *d1=new Date();
Date *d2=new Date(2,1,3);
int year,month,day;
cin>>year>>month>>day;
d1->Print();
d2->Print();
d1->init(year,month,day);
d1->Print();
delete d1,d2;
return 0;
}
/*2动态生成Person类的对象(20分)
题目内容:
编写Person类,数据成员为姓名(20字符长度)、年龄(int)和性别(char)。
编写无参数的构造函数,其中姓名赋值为“XXX”,年龄0,性别m;
编写析构函数,在其中输出字符串“Now
destroying the instance of
Person”;
编写Register成员函数,为数据成员赋值;
编写showme成员函数,显示姓名、年龄和性别。

编写主函数:
用Person类创建2个指针,p1和
p2;
用new创建两个Person对象,分别将指针赋值给p1,p2;
用showme成员函数显示p1,p2所指对象的值;
再输入一组“姓名、年龄和性别”值,用成员函数Register为p1的成员赋值;
将p1所指对象的值赋值给p2所指对象;
用showme显示p1、p2所指对象的值。
删除动态对象。

输入格式:
为p1的成员赋值时使用的数据

输出格式:
person1和person2的默认值
person1和person2的赋值后的值
析构函数输出的信息

输入样例:
Bobs 24 m

输出样例:
person1:XXX 0 m
person2:XXX 0 m
person1:Bobs 24 m
person2:Bobs 24
m
Now destroying the instance of Person
Now destroying the instance of
Person*/
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
char name[20];
int age;
char sex;
public:
Person() {
strcpy(name, "XXX");
age = 0;
sex = 'm';
};
void Register(char _name[], int _age, char _sex);
void ShowMe();
~Person() { cout << "Now destroying the instance of Person" << endl; }
};
void Person::Register(char _name[], int _age, char _sex) {
strcpy(name, _name);
age = _age;
sex = _sex;
}
void Person::ShowMe() {
cout << name << " " << age << " " << sex << endl;
}
int main()
{
Person *person1 = new Person();
Person *person2 = new Person();
char name[20], sex;
int age;
cin >> name >> age >> sex;

cout << "person1:";
person1->ShowMe();
cout << "person2:";
person2->ShowMe();
person1->Register(name, age, sex);
cout << "person1:";
person1->ShowMe();
*person2 = *person1;
cout << "person2:";
person2->ShowMe();
delete person1;
delete person2;
return 0;
}
/*3设计带构造函数的Dog类(20分)
题目内容:
设计一个Dog类,包含name、age、sex和weight等属性,在有参数的构造函数中对数据成员进行初始化。
公有成员函数有:GetName()、GetAge()、GetSex()和GetWeight()可获取名字、年龄、性别和体重。编写成员函数speak()
显示狗的叫声。编写主函数,输入狗的名字、年龄、性别和体重;声明Dog对象并用输入的数据通过构造函数初始化对象,通过成员函数获取狗的属性并显示出来。

输入格式:
狗的信息

输出格式:
狗的信息,外加叫声

输入样例:
Tom 4 m 2.4

输出样例:
Tom
4
m
2.4
Arf!Arf!*/
#include<iostream>
#include<cstring>
using namespace std;
class Dog
{
char name[20];
int age;
char sex;
double weight;
public:
void setdata(char* name, int age,char sex,double weight);
void GetName();
void GetAge();
void GetSex();
void GetWeight();
void speak(){cout<<"Arf!Arf!"<<endl;}
};
void Dog::setdata(char* _name, int _age, char _sex, double _weight)
{
strcpy(name, _name);
age = _age;
sex = _sex;
weight = _weight;
}

void Dog::GetAge()
{
cout << age <<endl;
}

void Dog::GetName()
{
cout << name <<endl;
}

void Dog::GetSex()
{
cout <<sex<< endl;  }
void Dog::GetWeight()
{
cout <<weight<<endl;
}

int main()
{
Dog dog;
char name[20], sex;
int age;
double weight;
cin>>name>>age>>sex>>weight;
dog.setdata( name, age, sex, weight);
dog.GetName();
dog.GetAge();
dog.GetSex();
dog.GetWeight();
dog.speak();
return 0;
}
/*4设计并测试一个椭圆类(20分)
题目内容:
设计并测试一个名为Ellipse的椭圆类,其属性为圆心坐标及长半轴和短半轴的长度。设计一个构造函数(Ellipse(int,int,double,double))对这些属性进行初始化,并通过成员函数计算出椭圆的面积(double
Area())。

S(椭圆面积)=PI(圆周率)×a(长半轴)×b(短半轴)
其中PI取3.14

输入格式:
圆心坐标、长半轴和短半轴的长度

输出格式:
椭圆的面积

输入样例:
1 1 1 2

输出样例:
6.28*/
#include<iostream>
#include<cmath>
using namespace std;
class Ellipse
{
int x,y;
double a,b;
public:
Ellipse() :x(0), y(0), a(0), b(0) {};
Ellipse(int,int,double,double);
double Area();
};
Ellipse::Ellipse(int _x,int _y,double _a,double _b){
x=_x;
y=_y;
a=_a;
b=_b;
};
double Ellipse::Area()
{
return 3.14*a*b;
}
int main()
{
int _x,_y;
double _a,_b,s;
cin>>_x>>_y>>_a>>_b;
Ellipse e(_x,_y,_a,_b);
s=e.Area();
cout<<s<<endl;
return 0;
}

/*5设计一个多功能的MyTime类(20分)
题目内容:
设计一个多功能的MyTime类,设计多个重载的构造函数,可以设置时间,进行时间的加减运算,按各种可能的格式(24小时制、12小时制)输出时间。
注意:
(1)请考虑设置的时间的合理性(时0-23; 分0-59;秒0-59)。
(2)12小时制中,12:00:00前为AM, 12:00:00及以后为PM
(3)加减运算的加数、减数是一个时间的长度,单位为“时、分、秒”
(4)构造函数:没参数时,设置时间为0时 0分 0秒;有参数时,设置成给定的时、分、秒。
在主函数中
(1)声明两个对象t1,t2,并通过构造函数初始化它们(t2初始化为为8:10:30)
(2)显示按12、14小时制显示t1、t2的时间。
(3)再设置t1的时间,数据由用户输入。
(4)再输入待加减的时间。
(5)t1加输入的时间,并按12小时和24小时制显示。
(6)t2减输入的时间,并按12小时和24小时制显示。

输入格式:
第一行为t1的时间,第二行为待加减的时间

输出格式:
显示按12、14小时制显示t1、t2的初始时间
t1加输入的待加减的时间按12小时和24小时制显示。
t2减输入的待加减的时间按12小时和24小时制显示

输入样例:
11 30 30
5 15 20

输出样例:
00:00:00 AM
00:00:00
08:10:30 AM
08:10:30
04:45:50 PM
16:45:50
02:55:10 AM
02:55:10*/
#include <iostream>
#include<cmath>
using namespace  std;
class Mytime
{
private:
int h, m, s;
public:
Mytime():h(0),m(0),s(0){};
Mytime(int,int,int);
void SetTime(int _h,int _m,int _s);
void print12();
void print24();
void plustime(int,int,int);
void minustime(int,int,int);
};
Mytime::Mytime(int _h,int _m,int _s)
{      h=_h;
m=_m;
s=_s;
}
void Mytime::SetTime(int _h, int _m, int _s)
{
h = _h; m = _m; s = _s;
}
void Mytime::plustime(int h1,int m1,int s1){
if(s+s1>=60){
s=s+s1-60;
m=m+1;
}
else s=s+s1;
if(m+m1>=60){
m=(m+m1)-60;
h=h+1;
}
else m=m+m1;
h=h+h1;
}
void Mytime::minustime(int h1,int m1,int s1){
if(s>=s1)
s=s-s1;
else{
m--;
s=s+60-s1;
}
if(m>=m1)
m=m-m1;
else{
h--;
m=m+60-m1;
}
if(h>=h1)
h=h-h1;
else
h=h+24-h1;
}
void Mytime::print12()
{
int h_temp;
(h - 12) >= 0 ? h_temp = h - 12 : h_temp = h;
if(h-24>=0)  h_temp=h-24;
if (h_temp<10)
{
cout << '0';
}
cout << h_temp << ':';
if (m<10)
{
cout << '0';
}
cout << m << ':';
if (s<10)
{
cout << '0';
}
cout << s << ' ';
if (h>=12&&h<24)
{
cout << "PM" << endl;
}
else
{
cout << "AM" << endl;
}
}

void Mytime::print24()
{
if(h>=24)
h=h-24;
if (h < 10)
{
cout << '0';
}
cout << h << ':';
if (m < 10)
{
cout << '0';
}
cout << m << ':';
if (s < 10)
{
cout << '0';
}
cout << s;
cout << endl;
}
int main()
{
Mytime time1;
int h1, m1, s1,h2,m2,s2;
cin >> h1 >> m1 >> s1;
cin >> h2 >> m2 >> s2;
time1.print12();
time1.print24();
Mytime time2(8,10,30);
time2.print12();
time2.print24();
time1.SetTime(h1, m1, s1);
time1.plustime(h2,m2,s2);
time1.print12();
time1.print24();
time2.minustime(h2,m2,s2);
time2.print12();
time2.print24();
return 0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐