C++学习日志33-----栈操作与简单的继承


一、栈操作

#include<iostream>
#include<cassert>
#include"StackOfIntegers.h"
//任务2:创建Stack对象,展示入栈出栈操作
int main()
{
    
    
	StackOfIntegers s1{
    
    };
	for (int i = 0; i < 5; i++)  //底 1 2 3 4 5 顶
	{
    
    
		s1.push(i + 1);
	}
	std::cout << "Stack size=" << s1.getSize() << std::endl;
	std::cout << "Top element is: " << s1.peek() << std::endl;
	const int size = s1.getSize();
	for (int i = 0; i < size; i++)
	{
    
    
		std::cout << s1.pop() << "  ";
	}
	std::cout << std::endl;
	std::cout << "Stack now is empty:"<<s1.empty()<<std::endl;
	std::cin.get();
	return 0;
}

#pragma once
class StackOfIntegers
{
    
    
private:
	int elements[100];
	int size{
    
     0 };
public:
	bool empty();
	int  peek();
	int  push(int value);
	int  pop();
	int getSize();
	StackOfIntegers();


};


#include"StackOfIntegers.h"
StackOfIntegers::StackOfIntegers()
{
    
    
	size = 0;
	for (int& i : elements)
	{
    
    
		i = 0;
	}
}
bool StackOfIntegers::empty()
{
    
    
	return (size == 0 ? true : false);
}

int StackOfIntegers::getSize()
{
    
    
	return size;
}
int StackOfIntegers::peek()
{
    
    
	return elements[size - 1];
}
int StackOfIntegers::pop()
{
    
    
	int temp = elements[size - 1];
	elements[size - 1] = 0;
	size--;
	return temp;
}
int StackOfIntegers::push(int value)
{
    
    
	elements[size] = value;
	size++;
	return value;
}

在这里插入图片描述
结果如上图所示。

二、简单的继承

#include<iostream>
#include<string>
#include"Shape.h"
#include"Circle.h"
#include"Rectangle.h"
//创建Shape/Circle/Rectangle对象
//用子类类对象调用基类函数toString()
int main()
{
    
    
	Shape s1{
    
     Color::blue,false };
	Circle c1{
    
     3.9, Color::green,true };
	Rectangle r1{
    
     4.0,1.0, Color::white,true };

	std::cout << s1.toString() << std::endl;
	std::cout << c1.toString() << std::endl;
	std::cout << r1.toString() << std::endl;
   
	std::cout << "c1 area: " << c1.getArea() << std::endl;
	std::cout << "r1 area: " << c1.getArea() << std::endl;

	std::cin.get();
}


#include"circle.h"
Circle::Circle()
{
    
    
	radius = 1.0;
}
Circle::Circle(double radius_, Color color_, bool filled_) :Shape{
    
     color_, filled_ }
{
    
    
	radius = radius_;
}
double Circle::getArea()
{
    
    
	return (3.14 * radius * radius);

}
double Circle::getRadius() const
{
    
    
	return radius;
}

Circle& Circle::setRadius(double radius)
{
    
    
	this->radius = radius;
	return (*this);
}


#include"rectangle.h"
Rectangle::Rectangle(double w, double h, Color c, bool f) :width{
    
     w }, height{
    
     h }, Shape{
    
    c,f}{
    
    }
double Rectangle::getWidth() const
{
    
    
    return width;
}

void Rectangle::setWidth(double width)
{
    
    
    this->width = width;
}

double Rectangle::getHeight() const
{
    
    
    return height;
}

void Rectangle::setHeight(double height)
{
    
    
    this->height = height;
}

double Rectangle::getarea()
{
    
    
    return width * height;
}


#pragma once
#include"Shape.h"
class Circle:public Shape
{
    
    
private:
	double radius;
public:
	Circle();
	Circle(double radius_, Color color_, bool filled_);
	double getArea();
public:
	double getRadius() const;
	Circle& setRadius(double radius);

};



#pragma once
#include"Shape.h"
//创建Rectangle类,从Shape继承
class Rectangle : public Shape
{
    
    
private:
	double width{
    
     1.0 };
	double height{
    
     1.0 };
public:
	Rectangle() = default;
	Rectangle(double w, double h,Color c,bool f);

    double getWidth() const;
    void setWidth(double width);

    double getHeight() const;
    void setHeight(double height);

	double getarea();
};

#pragma once
#include<iostream>
#include<string>
#include<array>
using std::string;
using namespace std::string_literals;
enum class Color
{
    
    
	white,black,red,green,blue,yellow,

};

class Shape
{
    
    
private:
	Color color{
    
     Color::black };
	bool filled{
    
     false };
public:
	Shape() = default;
	Shape(Color color_, bool filled_)
	{
    
    
		color = color_;
		filled = filled_;
	}
	Color getColor() {
    
     return color; }
	void setColor(Color color_) {
    
     color = color_; }
	bool isFilled() {
    
     return filled; }
	void setFilled(bool filled_) {
    
     filled = filled_; }

	string toString()
	{
    
    
		std::array<string, 6> c{
    
     "white"s,"black"s, "red"s, "green"s, "blue"s, "yellow"s, };
		return "Shape :  " + c[static_cast<int>(color)] + "  " + (filled ? "filled"s : "not filled"s);
	}
};

在这里插入图片描述
结果如上图所示。

猜你喜欢

转载自blog.csdn.net/taiyuezyh/article/details/124233966