【C++】实验七 继承与派生


实验七(第八章)实践题一

对本章示范题的用于管理商店商品的实现程序进行完善:完成Wardrobe立柜类的具体定义与使用,并添加“帽子仓库类”以及“立柜仓库类”的定义及使用,以使程序能够对商店的这三种商品(衬衣、帽子、立柜)进行简单的管理与应用。

要对商品实现的操作有:商品的进库(增加某类商品及其库存量),商品的出库(减少某类商品及其库存量),以及某类商品总价格的计算。

【输入形式】

根据提示输入
【输出形式】

把处理后的数据输出

【样例】注意:红色为输入部分,黑色为输出部分。

5 * shirt data in: price/place/material =>60 Tianjin Cotton

3 * shirt data in: price/place/material =>80 Beijing Wool

60 Tianjin Cotton

扫描二维码关注公众号,回复: 8831296 查看本文章

60 Tianjin Cotton

60 Tianjin Cotton

60 Tianjin Cotton

60 Tianjin Cotton

80 Beijing Wool

80 Beijing Wool

80 Beijing Wool

shiSto.TotalPrice()=540

60 Tianjin Cotton

60 Tianjin Cotton

60 Tianjin Cotton

60 Tianjin Cotton

shiSto.TotalPrice()=240

5 * Cap data in: price/place/material/style =>40 Suzhou Cotton M

3 * Cap data in: price/place/material/style =>30 Wuxi Wool S

40 Suzhou Cotton M

40 Suzhou Cotton M

40 Suzhou Cotton M

40 Suzhou Cotton M

40 Suzhou Cotton M

30 Wuxi Wool S

30 Wuxi Wool S

30 Wuxi Wool S

capSto.TotalPrice()=290

40 Suzhou Cotton M

40 Suzhou Cotton M

40 Suzhou Cotton M

40 Suzhou Cotton M

capSto.TotalPrice()=160

5 * Wardrobe data in: price/place/material/color =>160 Guangzhou Pine Yellow

3 * Wardrobe data in: price/place/material/color =>200 Suzhou Oak Brown

160 Guangzhou Pine Yellow

160 Guangzhou Pine Yellow

160 Guangzhou Pine Yellow

160 Guangzhou Pine Yellow

160 Guangzhou Pine Yellow

200 Suzhou Oak Brown

200 Suzhou Oak Brown

200 Suzhou Oak Brown

WarSto.TotalPrice()=1400

160 Guangzhou Pine Yellow

160 Guangzhou Pine Yellow

160 Guangzhou Pine Yellow

160 Guangzhou Pine Yellow

WarSto.TotalPrice()=640

#include<iostream>
#include<cstring>
using namespace std;
class Base{
	private:
		double price;
		char place[20];
		int count;
	public:
		Base(double pr,char *pl,int cnt)
		{
			price = pr;
			strcpy(place,pl);
			count = cnt;
		}
		void display()
		{
			cout << price << " " << place<< " ";
		}
		void InSomething(int add_cnt)
		{
			count +=add_cnt;
		}
		void OutSomething(int del_cnt)
		{
			count -= del_cnt;
		}
		double TotalPrice()
		{
			return price;
		}
};
class Shirt:public Base{
	private:
		char material[20];
	public:
		Shirt(double pr,char *pl,int cnt,char *mat):Base(pr,pl,cnt)
		{
			strcpy(material,mat);
		}
		void display()
		{
			Base::display();
			cout <<material<< " ";
		}
};
class Cap:public Shirt{
	private:
		char style;
	public:
		Cap(double pr,char *pl,int cnt,char *mat,char sty):Shirt(pr,pl,cnt,mat)
		{
			style = sty;
		}
		void display (){
			Shirt::display();
			cout <<style;
		}
};
class Wardrobe:public Base{
	private:
		char material[20];
		char color[20];
	public:
		Wardrobe(double pr,char *pl,int cnt,char *mat,char *col):Base(pr,pl,cnt)
		{
			strcpy(material,mat);
			strcpy(color,col);
		}
		void display()
		{
			Base::display();
			cout << material << " " << color;
		}
};
int main()
{
	int price;
	char place[20];
	char material[20];
	char style;
	char color[20];
	cout << "5 * shirt data in: price/place/material =>"<<endl;
	cin >> price >> place >> material;
	Shirt s1(price,place,5,material);
	cout << "3 * shirt data in: price/place/material =>"<<endl;
	cin >> price >> place >> material;
	Shirt s2(price,place,3,material);
	for(int i=0;i<5;i++) 
	{
		s1.display();
		cout << endl;
	}
	for(int i=0;i<3;i++)
	{
		s2.display();
		cout << endl;
	 } 
	cout << "shiSto.TotalPrice()=" << (s1.TotalPrice())*5+(s2.TotalPrice())*3 << endl;
	for(int i=0;i<4;i++)
	{
		s1.display();
		cout << endl;
	}
	cout << "shiSto.TotalPrice()=" << (s1.TotalPrice())*4<<endl;
	cout << "5 * Cap data in: price/place/material/style =>"<<endl;
	cin >> price >> place >> material >> style;
	Cap c1(price,place,5,material,style);
	cout << "3 * Cap data in: price/place/material/style =>"<<endl;
	cin >> price >> place >> material >> style;
	Cap c2(price,place,3,material,style);
	for(int i=0;i<5;i++)
	{
		c1.display();
		cout << endl;
	}
	for(int i=0;i<3;i++)
	{
		c2.display();
		cout << endl;
	 } 
	 cout << "capSto.TotalPrice()=" <<(c1.TotalPrice())*5+(c2.TotalPrice())*3 << endl;
	 for(int i=0;i<4;i++)
	 {
	 	c1.display();
	 	cout << endl;
	 }
	 cout << "capSto.TotalPrice()=" <<(c1.TotalPrice())*4<<endl;
	 cout << "5 * Wardrobe data in: price/place/material/color =>"<<endl;
	 cin >> price >> place >> material>>color;
	 Wardrobe w1(price,place,5,material,color);
	 cout << "3 * Wardrobe data in: price/place/material/color =>"<<endl;
	 cin >> price >> place >> material>>color;
	 Wardrobe w2(price,place,3,material,color);
	 for(int i=0;i<5;i++)
	 {
	 	w1.display();
	 	cout << endl;
	 }
	 for(int i=0;i<3;i++)
	 {
	 	w2.display();
	 	cout << endl;
	 }
	 cout << "WarSto.TotalPrice()="<< (w1.TotalPrice())*5+(w2.TotalPrice())*3 <<endl;
	 for(int i=0;i<4;i++)
	 {
	 	w1.display();
	 	cout << endl;
	 }
	 cout <<"WarSto.TotalPrice()=" << (w1.TotalPrice())*4;
	
	return 0;
}




实验七(第八章) 实践题二

【问题描述】

利用继承性与派生类来管理学生教师档案:由Person(人员)类出发(作为基类),派生出Student(学生)及Teacher(教师)类;而后又由Student(学生)类出发(作为基类),派生出GraduateStudent(研究生)类。可假定这几个类各自具有的数据成员为:

Person(人员)类: 姓名、性别、年龄;

Student(学生)类: 姓名、性别、年龄、学号、系别;

Teacher(教师)类: 姓名、性别、年龄、职称、担任课程;

GraduateStudent(研究生)类: 姓名、性别、年龄、学号、系别、导师。

为简化起见,每个类可只设立构造函数以及显示类对象数据的成员函数Print。而后编制简单的主函数,说明上述有关的类对象,并对其类成员函数进行简单使用(调用)。

【输入形式】
【输出形式】
【样例输入】


【样例输出】

== per1.Display() => name,age,sex

sun 42 M

== stu1.Display() => name,age,sex,Reg_Number,department

guo 22 F 1001 comp

== teach1.Display() => name,age,sex,course,post

fang 38 M english professor

== gStu.Display() => name,age,sex,Reg_Number,department,advisor

wu 25 M 1021 comp wei

#include<iostream>
#include<string>
using namespace std;

class Person{
	protected:
		string name;
		int age;
		string sex;
	public:
		Person(){}
		Person(string n,int a,string s):name(n),age(a),sex(s){}
		void Display()
		{
			cout << name << " " << age << " " << sex;
		}
};
class Student:public Person{
	protected:
		int regnum;
		string department;
	public:
		Student(){}
		Student(string n,int a,string s,int r,string d)
		{
			name=n;age=a;sex=s;regnum=r;department=d;
		}
		void Display()
		{
			Person::Display();
			cout << " " << regnum << " " << department;
		}
};
class GraduateStudent:public Student{
	protected:
		string advisor;
	public:
		GraduateStudent(){};
		GraduateStudent(string n,int a,string s,int r,string d,string ad):Student(n,a,s,r,d),advisor(ad){}
		void Display()
		{
			Student::Display();
			cout << " " << advisor;
		}
};
class Teacher:public Person{
	protected:
		string course;
		string post;
	public:
		Teacher(string n,int a,string s,string c,string p):Person(n,a,s),course(c),post(p){}
		void Display()
		{
			Person::Display();
			cout << " " << course << " " << post;
		}
};

int main()
{
	cout << "== per1.Display() => name,age,sex" << endl;
	Person per1("sun",42,"M");
	per1.Display();cout << endl;
	cout << "== stu1.Display() => name,age,sex,Reg_Number,department" << endl;
	Student stu1("guo",22,"F",1001,"comp");
	stu1.Display();cout << endl;
	cout << "== teach1.Display() => name,age,sex,course,post" << endl;
	Teacher teach1("fang",38,"M","english","professor");
	teach1.Display();cout << endl;
	cout << "== gStu.Display() => name,age,sex,Reg_Number,department,advisor" << endl;
	GraduateStudent gStu("wu",25,"M",1021,"comp","wei");
	gStu.Display();
	return 0;
}



实验七(第八章) 实践题三

自定义一个日期时间类DateTimeType,它含有类DateType与类TimeType的类对象作为其数据成员,并具有所列的其他几个成员函数。而后编制主函数,说明DateTimeType的类对象,并对其成员函数以及二对象成员所属类的公有成员函数进行使用。

class DateTimeType { //自定义的日期时间类 DateTimeType

DateType date; //类 DateType 的类对象 date 作为其数据成员

TimeType time; //类 TimeType 的类对象 time 作为其另一个数据成员

public:

DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0);

//构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值

DateType& GetDate(){ return date; } //返回本类的私有数据对象 data

TimeType& GetTime(){ return time; } //返回本类的私有数据对象 time

void IncrementSecond(int s); //增加若干秒,注意“进位”问题

void PrintDateTime(); //屏幕输出日期时间对象的有关数据

};

注意,每一个DateTimeType类对象中总包含有一个DateType类对象(对象成员)以及一个TimeType类对象(对象成员),编制与实现本程序时,也必须包含DateType与TimeType自定义类(类型)。

之所以设置了公有的类成员函数GetDate与GetTime,是为类外如主函数处使用该类的私有数据成员date与time提供方便(否则的话,类外无法直接访问该类的私有数据成员)。另外,两成员函数返回的都为引用,为的是可将返回对象当作一个独立变量来使用(如可以继续作左值等)。例如,假设编制了如下形式的主函数:

void main() {

DateTimeType dttm1(1999,12,31,23,59,59), dttm2;

(dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数

cout<<endl;

dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime

dttm2.PrintDateTime();

dttm1.IncrementSecond(30) ; //调用本派生类成员函数

dttm1.PrintDateTime();

}
【样例输出】

1999-12-31

1999-12-31 23:59:59

1-1-1 0:0:0

2000-1-1 0:0:29

#include<iostream>
using namespace std;

class DateType{
	protected:
		int y;
		int m;
		int d;
	public:
		DateType(int yy=0,int mm=0,int dd=0):y(yy),m(mm),d(dd){}
		void PrintDate()
		{
			cout << y << "-" << m << "-" << d;
		}
		void up()
		{
			int g[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
			if(y%4==0&&y%100!=0||y%400==0) g[2] = 29;
			d++;
			if(d>g[m])
			{
				d-=g[m];
				m++;
				if(m>12)
				{
					m-=12;
					y++;
				}
			}
		}
};
class TimeType{
	protected:
		int h;
		int m;
		int s;
	public:
		TimeType(int hh=0,int mm=0,int ss=0):h(hh),m(mm),s(ss){}
		void PrintTime()
		{
			cout << h << ":" << m << ":" << s;
		}
		int up(int ss)
		{
			s+=ss;
			int temp = s/60;
			if(temp)
			{
				m+=temp;
				s-=60*temp;
				temp=m/60;
				if(temp)
				{
					h+=temp;
					m-=60*temp;
					temp=h/24;
					if(temp)
					{
						h-=24*temp;
						return temp;
					}
				}
			}
			return 0;
		}
};
class DateTimeType {  
	DateType date;
	TimeType time;
	public:
		DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0):date(y0,m0,d0),time(hr0,mi0,se0){}
		DateType& GetDate(){ return date; } 
		TimeType& GetTime(){ return time; } 
		void IncrementSecond(int s)
		{
			//如果大于24小时 

			if(time.up(s))
			{
				date.up();//日期增加一天 
			}
		}
		void PrintDateTime()
		{
			date.PrintDate();
			cout << " ";
			time.PrintTime();
			cout << endl;
		}
};

int main(){
	DateTimeType dttm1(1999,12,31,23,59,59), dttm2;
	(dttm1.GetDate()).PrintDate(); 
	cout<<endl;
	dttm1.PrintDateTime(); 
	dttm2.PrintDateTime();
	dttm1.IncrementSecond(30); 
	dttm1.PrintDateTime();
	return 0;
}

发布了38 篇原创文章 · 获赞 4 · 访问量 1645

猜你喜欢

转载自blog.csdn.net/qq_15989473/article/details/103325495