SCU-C++习题集 第五章部分

设计堆栈类

由给定的数组类派生一个Stack类

#include<iostream>
using namespace std;
class Array
{
protected:
	int *p;
	int size;
public:
	Array(int n)
	{
		size = n;
		p = new int[size];
	}
	~Array()
	{
		if (NULL != p)
		{
			delete[] p;
		}
	}
	int& operator[](int i)
	{
		return p[i];
	}
};

class Stack :public Array {
public:
	Stack(int n) :Array(n) { 
		m_size = 0;
	}
	void push(int i);
	void pop(int &j);


private:
	int m_size;
};

void Stack::push(int i) {
	p[m_size] = i;
	m_size++;
}//入栈

void Stack::pop(int &j) {
	m_size--;
	j = p[m_size];
}//出栈

int main(){
	int v;
	Stack mystack(10);
	for (int i = 0; i < 10; i++)
	{

		cin >> v;
		mystack.push(v);
	}
	for (int i = 0; i < 10; i++)
	{
		mystack.pop(v);
		cout << v << " ";
	}
	cout << endl;
	return 0;
}

设计队列类

由给定的数组类派生一个Queue类

#include<iostream>
using namespace std;
class Array
{
protected:
    int *p;
    int size;
public:
    Array(int n)
    {
        size = n;
        p = new int[size];
    }
    ~Array()
    {
        if (NULL != p)
        {
            delete [] p;
        }
    }
    int& operator[](int i)
    {
        return p[i];
    }
};

class Queue:public Array{
private:
int head;
int tail;
public:
Queue(int n):Array(n){
head=0;
tail=0;};
void in(int item);
void out(int &n);
};//限制队长度
void Queue::in(int item){
if(tail<size){
	p[tail]=item;
	tail++;}
}//尾部入队
void Queue::out(int &n){
		n=p[head];	
	int x=head;
	for(x=0;x<tail;x++){
		p[x]=p[x+1];	
	}
	tail--;//整体前移 包括尾指针
}//头部出队

int main()
{
    int v;
    int n;
    int o;
    cin >> n >> v;
    Queue myqueue(n);
    for (int i = 0; i < n + 1; i++)    // 插入数据比队列多,最后一个数据不能入队
    {
        myqueue.in(v++); // 数据入队
    }
    myqueue.out(o); // 数据出队
    myqueue.out(o); // 数据出队
    for (int i = 0; i < n; i++)
    {
        myqueue.in(v++); // 数据入队
    }
    for (int i = 0; i < n; i++)
    {
        myqueue.out(o); // 数据出队
        cout << o << " ";
    }
    cout << endl;
    return 0;
}

求长方体的表面积和体积

定义一个Rectangle(长方形)类,它包含两个数据成员长和宽;以及包含用于求长方形面积的成员函数Area()。
再定义Rectangle的派生类Rectangular(长方体),它包含一个新数据成员高度和用来求长方体体积的成员函数Volume()。
在main函数中,分别使用这两个类求某个长方形的面积和某个长方体的体积。

#include<iostream>
using namespace std;
class Rectangle {
	public:
	double length;
	double width;
	double sum;
	Rectangle(double length,double width);
double Area();
	
};

Rectangle::Rectangle(double len,double wid){
length=len; width=wid;	
}

double Rectangle::Area(){
	sum=length*width;
	return sum;
}

class Rectangular:public Rectangle{
private:
	double height;
	double vo=0.0;
	public:
Rectangular(double len,double wid,double hei);
double Volume();
};

Rectangular::Rectangular(double len,double wid,double hei):Rectangle(len,wid){
height=hei;
};

double Rectangular::Volume(){
	vo=length*width*height;
	return vo;
}

int main()
{
    double length, width, height;
    cin >> length >> width;
    Rectangle rectangle(length, width);
    cout << rectangle.Area() << endl;
    cin >> length >> width >> height;
    Rectangular rectangular(length, width, height);
    cout << rectangular.Volume() << endl;
    return 0;
}

设计昆虫类

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

class Bug{
public:
	Bug(int legs, string color);
	~Bug();
	void PrintBug();
private:
	int nLegs;
	string strColor;
};

class FlyBug : public Bug{
public:
	FlyBug(int legs, string color, int wings);
	~FlyBug();
	void PrintBug();
private:
	int nWings;
};

// 你的代码添加在这里
Bug::Bug(int legs, string color) {
	nLegs = legs;
	strColor = color;
	cout << "Bug::Bug()" << endl;
}
Bug::~Bug(){ 
	cout << "Bug::~Bug()" << endl;
}
void Bug::PrintBug() {
	cout << nLegs << "条腿,颜色为:" << strColor << endl;
}

FlyBug::FlyBug(int legs, string color, int wings):Bug(legs,color) {
	nWings = wings;
	cout << "FlyBug::FlyBug()" << endl;
}

FlyBug::~FlyBug() {
	cout << "FlyBug::~FlyBug()" << endl;
}

void FlyBug::PrintBug() {
	Bug::PrintBug();
	cout << "翅膀的个数为:" << nWings << endl;
}

int main()
{
	FlyBug fb(2, "红", 4);
	fb.PrintBug();
	return 0;
}

设计雇员类

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

class Date
{
public:
    Date(int y, int m, int d);
    Date(){};//定义默认构造函数
    friend ostream& operator<<(ostream& o, const Date& d);
private:
    int year, month, day;
};

class Employee
{
public:
    Employee(int id, string name, const Date& join);
    void Display();
private:
    int e_id;
    string e_name;
    Date e_join_time;
};

class Manager : public Employee
{
public:
    Manager(int id, string name, const Date& join, const Date& mantime);
    void Display();
private:
    Date m_man_time;
};

// 你的代码添加在这里
Date::Date(int y, int m, int d){
	year=y;
	month=m;
	day=d;
} //带参构造函数
Employee::Employee(int id, string name, const Date& join){
	e_id= id;
	e_name= name;
    e_join_time=join;
}

Manager::Manager(int id, string name, const Date& join, const Date& mantime):Employee(id,name,join){
m_man_time=mantime;	
} //含子对象的构造函数

ostream& operator << (ostream& o, const Date& d)
{o <<d.year << "-" << d.month << "-" << d.day;
return o;}//当使用date的对象,使用重载的流提取

void Employee::Display(){
	cout<<"id:"<<e_id<<endl;
	cout<<"name:"<<e_name<<endl;
	cout<<"join time:"<<e_join_time<<endl;	
}

void Manager::Display(){
	Employee::Display();//继承基类的函数
	cout<<"manager time:"<<m_man_time;
}

int main()
{
    Manager m(100, "张三", Date(1999,1,1), Date(2018, 5, 28));
    m.Display();
    return 0;
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值