1.设计矩形类
#include<iostream>
#include<mutex>
#include<stack>
#include<exception>
#include<string>
using namespace std;
class Rectangle
{
private:
int left, top, right, bottom;
public:
Rectangle(int e = 0, int t = 0, int r = 0, int b = 0)
:left(e), top(t), right(r), bottom(b)
{
}
//实现默认构造函数和带参的构造函数
//实现Set函数
void SetLeft(int e)
{
left = e;
}
void SetTop(int t)
{
top = t;
}
void SetRight(int r)
{
right = r;
}
void SetBottom(int b)
{
bottom = b;
}//在构造函数中可以使用列表,在其他函数中不可以使用列表,调动构造函数,创建类型对象,对构造函数成员进行构建,每个对象被构建一次,类里面的成员构建只能有构造函数和拷贝构造函数来构建,其他函数只能使用对对象成员,初始化列表只能存在于构造函数
// 实现Get函数
int GetLeft()const
{
return left;
}
int GetTop()const
{
return top;
}
int GetRight()const
{
return right;
}
int SetBottom()const
{
return bottom;
}
void Show() const
{
cout << "left-top point is (" << left << "," << top << :")" << endl;
cout << "right-bottom point is (" << right << "," << bottom << ")" << endl;
}
};
int main()
{
Rectangle r1;
Rectangle r2(1, 1, 20, 20);
const Rectangle& r = r2;
return 0;
}
public:
2.实现双向函数
#include<iostream>
#include<mutex>
#include<stack>
#include<exception>
#include<string>
using namespace std;
class Object
{
private:
int value;
public:
Object(int x = 0):value(x) {}
~Object() {}
void SetValue(int x) {value = x;}
int GetValue() { return value;}
// 使用一个函数实现 SetValue 和 GetValue() 函数的功能
int& Value()
{
return value;
}
const int& Value()const
{
return value;
}
};
int main()
{
Object obj(10);
obj.GetValue();
const Object objc(20);
objc.Value();
}
3. 实现Stack
#include<iostream>
#include<mutex>
#include<stack>
#include<exception>
#include<string>
using namespace std;
#define SEQ_INIT_SIZE 10
#define SEQ_INC_SIZE 2
class SeqStack
{
private:
int* base;
int* pos;
int maxsize;
public:
SeqStack(int sz = SEQ_INIT_SIZE) :maxsize(sz > SEQ_INIT_SIZE ? sz : SEQ_INIT_SIZE)
{
base = pos = (int*)malloc(sizeof(int) * maxsize);
if (NULL == base) exit(1);
}
//实现函数有:
~SeqStack; // 析构函数
int Get_Size()const // 返回数据的个数
{
return pos - base;
}
Get_Capacity; // 返回容量
bool Is_Empty()const // 判空
{
return base == pos;
//return Get_size==0;
}
Is_Full; // 判满
Push; // 入栈
Pop; // 出栈
Top; // 取栈顶数据 ,但不出栈
};
4. STL
STL string [std::basic_string-cppreference.com]
(https://zh.cppreference.com/w/cpp/string/basic_string)
预习 STL中的string 类型; 熟练掌握string类型中的方法。