面向对象程序设计-实验二 类定义和进一步使用

2022/3/27
1. 课堂练习的“家具管理系统”题目。
#pragma once
#include<string>
using namespace std;
class Date {
public:
	Date() {}
	Date(int y, int m, int d) {
		year = y;
		month = m;
		date = d;
	}//构造函数
	Date(const Date& T) {
		year = T.year;
		month = T.month;
		date = T.date;
	}//复制构造函数
	void d(int y, int m, int d);
	void out()const;
private:
	int year = 2019;//年
	int month = 1;//月
	int date = 2;//日
};
class Furniture {
public:
	Furniture(){}
	Furniture(Furniture& T) {
		name = T.name;
		price = T.price;
		number = new int(*T.number);
		stock = T.stock;
		date = T.date;
	}//复制构造函数
	void display(const Date& T);
	void changestock(int s);
	void changenum(int n);
	void changedate(int y, int m, int d);
	Date out();
private:
	string name = "桌子";//名称
	int price = 600;//价格
	int* number = new int(5);//数目
	static int stock;//库存量
	Date date;//日期
};
#include<iostream>
#include<string>
#include"1.h"
void Date::d(int y, int m, int d) {
	year = y;
	month = m;
	date = d;
}//改变日期
void Date::out()const {
	cout << year << " " << month << " " << date << endl;
}
int Furniture::stock = 5;
void Furniture::display(const Date& T) {
	T.out();
	cout << "Name:" << name << endl;
	cout << "Price:" << price << endl;
	cout << "Number:" << *number << endl;
	cout << "Stock:" << stock << endl;
	cout << endl;
}//输出信息
void Furniture::changestock(int s) {
	stock = s;
}//库存量调整
void Furniture::changenum(int n) {
	*number = n;
}//数目调整
void Furniture::changedate(int y, int m, int d) {
	date.d(y, m, d);
}//改变日期
Date Furniture::out() {
	return date;
}//返回日期
int main() {
	Furniture funiture1;//定义家具一
	funiture1.display(funiture1.out());//输出家具一信息
	Furniture funiture2(funiture1);//复制家具二
	Furniture* funiture = &funiture2;//定义对象指针
	funiture->changenum(20);
	funiture->changestock(25);
	funiture->changedate(2022, 3, 27);
	funiture->display(funiture->out());//输出家具二信息
	return 0;
}
2. 阅读下面的程序与输出结果,添加一个拷贝构造函数来完善整个程序
#include<iostream>
using namespace std;  
class Cat 
{ 
public: 
    Cat(); 
    ~Cat(); 
    int getage()const{return *itsage;} 
    void setage(int age){*itsage=age;} 
protected: 
    int *itsage; 
}; 
Cat::Cat() 
{ 
   itsage=new int; 
   *itsage=5; 
} 
Cat::~Cat() 
{ 
    delete itsage; 
    itsage=NULL; 
} 
int main() 
{ 
   Cat frisky; 
   cout<<"frisky's age:"<<frisky.getage()<<endl; 
   cout<<"setting frisky to 6...\n"; 
   frisky.setage(6); 
   cout<<"creating boots from frisky\n"; 
   Cat boots(frisky); 
   cout<<"frisky's age:"<<frisky.getage()<<endl; 
   cout<<"boots'age:"<<boots.getage()<<endl; 
   cout<<"setting frisky to 7...\n"; 
   frisky.setage(7); 
   cout<<"frisky's age:"<<frisky.getage()<<endl; 
   cout<<"boots'age:"<<boots.getage()<<endl; 
return 0;
} 

当添加了拷贝构造函数后,程序的运行结果为:

frisky's age:5 
setting frisky to 6... 
creating boots from frisky 
frisky's age:6 
boots'age:6 
setting frisky to 7... 
frisky's age:7 
boots'age:6
#include<iostream>
using namespace std;
class Cat{
public:
    Cat();
    Cat(const Cat& T) {
        itsage = new int(*T.itsage);
    }
    ~Cat();
    int getage()const {
        return *itsage;
    }
    void setage(int age) {
        *itsage = age;
    }
protected:
    int* itsage;
};
Cat::Cat(){
    itsage = new int;
    *itsage = 5;
}
Cat::~Cat(){
    delete itsage;
    itsage = NULL;
}
int main(){
    Cat frisky;
    cout << "frisky's age:" << frisky.getage() << endl;
    cout << "setting frisky to 6...\n";
    frisky.setage(6);
    cout << "creating boots from frisky\n";
    Cat boots(frisky);
    cout << "frisky's age:" << frisky.getage() << endl;
    cout << "boots'age:" << boots.getage() << endl;
    cout << "setting frisky to 7...\n";
    frisky.setage(7);
    cout << "frisky's age:" << frisky.getage() << endl;
    cout << "boots'age:" << boots.getage() << endl;
    return 0;
}
3.创建Employee类,该类包含姓名、街道地址、市、省和邮政编码,以及入职时间等属性。要求入职时间定义为一个Date类。(提示:需要注意组合类构造函数正确定义)。

要求:
1)使用string类型定义类的数据成员
2)主程序中动态建立Employee类的对象,完成对象数据成员的输入和输出。
3)通过复制的方法生成一个新对象

#pragma once
#include<string>
using namespace std;
class Date {
public:
	Date() {}
	Date(int y, int m, int d) {
		year = y;
		month = m;
		date = d;
	}//构造函数
	Date(const Date& T) {
		year = T.year;
		month = T.month;
		date = T.date;
	}
	void out();
private:
	int year = 0;//年
	int month = 0;//月
	int date = 0;//日
};
class Employee {
public:
	Employee(string  n, string a, string ci, string p, string co) {
		name = n;
		address = a;
		city = ci;
		province = p;
		code = co;
	}//构造函数
	Employee(const Employee& T) {
		name = T.name;
		address = T.address;
		city = T.city;
		province = T.province;
		code = T.code;
		date = T.date;
	}
	void n(string tmp);
	void a(string tmp);
	void ci(string tmp);
	void p(string tmp);
	void co(string tmp);
	void d(Date x);
	void out();
private:
	string name;//姓名
	string address;//地址
	string city;//城市
	string province;//省份
	string code;//编码
	Date date;//日期
};
#include<iostream>
#include<string>
#include"3.h"
using namespace std;
void Date::out() {
	cout << year << " " << month << " " << date;
}
void Employee::n(string tmp) {
	name = tmp;
}
void Employee::a(string tmp) {
	address = tmp;
}
void Employee::ci(string tmp) {
	city = tmp;
}
void Employee::p(string tmp) {
	province = tmp;
}
void Employee::co(string tmp) {
	code = tmp;
}
void Employee::d(Date x) {
	date = x;
}
void Employee::out() {
	cout << name << " " << address << " " << city << " " << province << " " << code << " ";
	date.out();
}
int main() {
	string tmp;
	int a, b, c;
	Employee* employee1 = new Employee("张三", "朝晖街道", "杭州", "浙江", "310000");
	cout << "请输入姓名:";
	cin >> tmp;
	employee1->n(tmp);
	cout << "请输入地址:";
	cin >> tmp;
	employee1->a(tmp);
	cout << "请输入城市:";
	cin >> tmp;
	employee1->ci(tmp);
	cout << "请输入省份:";
	cin >> tmp;
	employee1->p(tmp);
	cout << "请输入邮编:";
	cin >> tmp;
	employee1->co(tmp);
	cout << "请输入年月日:";
	cin >> a >> b >> c;
	Date* date = new Date(a, b, c);
	employee1->d(*date);
	Employee employee2 =  *employee1;//复制新对象
	cout << "输出:" << endl;
	employee2.out();
	return 0;
}
4. 以char* 形式,完成Employee类中姓名,街道等属性定义,完成第1题中的要求2),3)。
#pragma once
#include<string>
using namespace std;
class Date {
public:
	Date() {}
	Date(int y, int m, int d) {
		year = y;
		month = m;
		date = d;
	}//构造函数
	Date(const Date& T) {
		year = T.year;
		month = T.month;
		date = T.date;
	}
	void out();
private:
	int year = 0;//年
	int month = 0;//月
	int date = 0;//日
};
class Employee {
public:
	Employee(){}
	Employee(char* n, char* a, char* ci, char* p, char* co) {
		strcpy_s(name, n);
		strcpy_s(address, a);
		strcpy_s(city, ci);
		strcpy_s(province, p);
		strcpy_s(code, co);
	}//构造函数
	Employee(const Employee& T) {
		strcpy_s(name, T.name);
		strcpy_s(address, T.address);
		strcpy_s(city, T.city);
		strcpy_s(province, T.province);
		strcpy_s(code, T.code);
		date = T.date;
	}
	void n(char* tmp);
	void a(char* tmp);
	void ci(char* tmp);
	void p(char* tmp);
	void co(char* tmp);
	void d(Date x);
	void out();
private:
	char name[20];//姓名
	char address[20];//地址
	char city[20];//城市
	char province[20];//省份
	char code[20];//编码
	Date date;//日期
};
#include<iostream>
#include<string>
#include"4.h"
using namespace std;
void Date::out() {
	cout << year << " " << month << " " << date;
}
void Employee::n(char* tmp) {
	strcpy_s(name, tmp);
}
void Employee::a(char* tmp) {
	strcpy_s(address, tmp);
}
void Employee::ci(char* tmp) {
	strcpy_s(city, tmp);
}
void Employee::p(char* tmp) {
	strcpy_s(province, tmp);
}
void Employee::co(char* tmp) {
	strcpy_s(code, tmp);
}
void Employee::d(Date x) {
	date = x;
}
void Employee::out() {
	cout << name << " " << address << " " << city << " " << province << " " << code << " ";
	date.out();
}
int main() {
	char tmp[] = "abcdefghijklmnopqrstuvwxyz";
	int a, b, c;
	Employee* employee1 = new Employee();
	cout << "请输入姓名:";
	cin >> tmp;
	employee1->n(tmp);
	cout << "请输入地址:";
	cin >> tmp;
	employee1->a(tmp);
	cout << "请输入城市:";
	cin >> tmp;
	employee1->ci(tmp);
	cout << "请输入省份:";
	cin >> tmp;
	employee1->p(tmp);
	cout << "请输入邮编:";
	cin >> tmp;
	employee1->co(tmp);
	cout << "请输入年月日:";
	cin >> a >> b >> c;
	Date* date = new Date(a, b, c);
	employee1->d(*date);
	Employee employee2 =  *employee1;//复制新对象
	cout << "输出:" << endl;
	employee2.out();
	return 0;
}
5. Josephus(约瑟夫)问题:有n个人围成一个圈,从第1个人开始报数,数到第m个人,让他出局;然后从出局的下一个人重新开始报数,数到第m个人,再让他出局,……,如此反复直到剩下一个人,问此人编号为几?

建立约瑟夫问题的合理类,计算出最后获胜的孩子编号。

#pragma once
class Josephus {
public:
	Josephus(int a, int b) {
		n = a;
		m = b;
		left = n;
	}//构造函数
	int circle();//报数
private:
	int n;//总人数
	int m;//出局数
	int left;//剩余人数
};
#include<iostream>
#include<string>
#include"5.h"
using namespace std;
int Josephus::circle() {
	int num = 0, cnt = 0;
	bool flag[101];
	for (int i = 1; i <= m; i++) {
		flag[i] = 1;
	}
	while (left > 1) {
		num = num % n + 1;//轮到序号
		if (flag[num]) {
			cnt = cnt + 1;//报到的数
		}
		if (cnt == m) {
			flag[num] = 0;//出局
			cnt = 0;//报数清零
			left = left - 1;//人数减一
		}
	}
	for (int i = 1; i <= n; i++) {
		if (flag[i]) {
			return i;
		}
	}
}
int main() {
	int n, m;
	cin >> n >> m;
	Josephus x(n, m);
	cout<<"最后的编号为:"<<x.circle();
	return 0;
}
实验总结
  1. 注意const关键字的使用
  2. 注意static关键字在类中的使用
  3. 注意指针和数组的初始化
  4. 注意复制构造函数是否要开辟空间
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻闻闻闻笛声

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值