【C++实践】第七章 继承与派生

8 篇文章 2 订阅

实践题1

【问题描述】

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

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

【输入形式】

根据提示输入

【输出形式】

把处理后的数据输出

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

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

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

60 Tianjin Cotton

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;
}

实践题2

【问题描述】

利用继承性与派生类来管理学生教师档案:由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.h>
#pragma warning(disable : 4996)
using namespace std;
class Person
{
private:
	char name[20];
	char sex;
	int age;
public:
	Person(const char* n, char s, int a)
	{
		strcpy(name, n);
		sex = s;
		age = a;
	}
	void Print();
	char* Getname()
	{
		return name;
	}
	int Getage()
	{
		return age;
	}
	char Getsex()
	{
		return sex;
	}
};
void Person::Print()
{
	cout << name << " " << age << " " << sex << endl;

}
class Student :public Person
{
private:
	char major[10];
	int num;
public:
	Student(const char* n, char s, int a, int number, const char* m) :Person(n, s, a)
	{
		strcpy(major, m);
		num = number;
	}
	void Print()
	{
		cout << Getname() << " ";
		cout << Getage() << " ";
		cout << Getsex() << " ";
		cout << num << " " << major << endl;
	}
	int Getnumber()
	{
		return num;
	}
	char* Getmajor()
	{
		return major;
	}
};
class Teacher :public Person
{
private:
	char course[10];
	char post[10];
public:
	Teacher(char* n, char s, int a, const char* cor, const char* p) :Person(n, s, a)
	{
		strcpy(course, cor);
		strcpy(post, p);
	}
	void Print()
	{
		cout << Getname() << " ";
		cout << Getage() << " ";
		cout << Getsex() << " ";
		cout << course << " " << post << endl;
	}
};
class GraduateStudent :public Student
{
private:
	char advisor[10];
public:
	GraduateStudent(char n[10], char s, int a, int number, char* m, const char* adv) :Student(n, s, a, number, m)
	{
		strcpy(advisor, adv);
	}
	void Print()
	{
		cout << Getname() << " ";
		cout << Getage() << " ";
		cout << Getsex() << " ";
		cout << Getnumber() << " ";
		cout << Getmajor() << " ";
		cout << advisor << endl;
	}
};
int main()
{
	cout << "== per1.Display() => name,age,sex" << endl;
	Person per("sun", 'M', 42);
	per.Print();
	cout << "== stu1.Display() =>name,age,sex,Reg_Number,department" << endl;
	Student stu("guo", 'F', 22, 1001, (char*)"comp");
	stu.Print();
	cout << "== teach1.Display() =>name,age,sex,course,post" << endl;
	Teacher teach((char*)"fang", 'M', 38, (char*)"english", (char*)"professor");
	teach.Print();
	cout << "== gStu.Display() =>name,age,sex,Reg_Number,department,advisor" << endl;
	GraduateStudent gstu((char*)"wu", 'M', 25, 1021, (char*)"comp", (char*)"wei");
	gstu.Print();

}

实践题3

【问题描述】

自定义一个日期时间类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
{
public:
	int year;
	int month;
	int day;
	DateType(int y, int m, int d)
	{
		year = y; month = m; day = d;
	}
	void PrintDate()
	{
		cout << year << "-" << month << "-" << day << endl;
	}
};
class TimeType
{
public:
	int hour;
	int minute;
	int second;
	TimeType(int h, int m, int s)
	{
		hour = h; minute = m; second = s;
	}
};
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);
void PrintDateTime()
{
	cout << date.year << "-" << date.month << "-" << date.day << " " << time.hour << ":" << time.minute << ":" << time.second << endl;
}
};
void DateTimeType::IncrementSecond(int s)
{
	int& m = date.month;
	time.second += s;
	if (time.second >= 60)
	{
		time.minute += time.second / 60;
		time.second = time.second % 60;
		if (time.minute >= 60)
		{
			time.hour+=time.minute/60;
			time.minute=time.minute%60;
			if (time.hour > 23)
			{
				time.hour = time.hour - 24;
				date.day++;
				if ((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10) && date.day == 32)
				{
					m++;
					date.day = 1;
				}
				if ((m == 4 || m == 6 || m == 9 || m == 11) && date.day == 31)
				{
					m++;
					date.day = 1;
				}
				if (m == 12 && date.day == 32)
				{
					m = 1;
					date.year++;
					date.day = 1;
				}
				if ((date.year % 4 == 0 && date.year % 100 != 0) || date.year % 400 == 0)
				{
					if (m == 2 && date.day == 30)
					{
						m++;
						date.day = 1;
					}
				}
				else
				{
					if (m == 2 && date.day == 29)
					{
						m++;
						date.day = 1;
					}
				}
			}
		}
	}
}
int main()
{
	DateTimeType dttm1(1999, 12, 31, 23, 59, 59), dttm2;
	(dttm1.GetDate()).PrintDate();
	dttm1.PrintDateTime();
	dttm2.PrintDateTime();
	dttm1.IncrementSecond(30);
	dttm1.PrintDateTime();
}
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值