#include <iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity)
{
_array = (DataType*)malloc(sizeof(DataType) * capacity);
if (NULL == _array)
{
perror("malloc申请空间失败!!!");
return;
}
_capacity = capacity;
_size = 0;
}
void Push(DataType data)
{
//CheckCapacity();
_array[_size] = data;
_size++;
}
//其他方法...
~Stack()
{
if (_array)
{
free(_array);
_array = NULL;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _array;
int _capacity;
int _size;
};
class MyQueue
{
public:
//Stack不具备默认构造,MyQueue也无法生成默认构造
//初始化列表
//初始化列表本质可以理解为每个对象中成员定义的地方
//所有的成员,你可以在初始化列表初始化,也可以在函数体内初始化
//以下三种情况必须在初始化列表初始化
//1、引用 2、const 3、没有默认构造的自定义类型成员(必须显式传参调构造)
MyQueue(int n, int& rr)
:_pushst(n)
, _popst(n)
, _x(1)
, _ref(rr)
{
_size = 0;
//_x = 1;//err
}
private:
//声明
Stack _pushst;
Stack _popst;
int _size;
//必须在定义时初始化
const int _x;
//必须在定义时初始化
int& _ref;
};
int main()
{
int xx = 0;
MyQueue q1(10, xx);
/*const int y;
y = 1;*/ //err
const int y = 1;
//int& ry;//err
int& ry = xx;
return 0;
}
#include <iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 4)
{
_array = (DataType*)malloc(sizeof(DataType) * capacity);
if (NULL == _array)
{
perror("malloc申请空间失败!!!");
return;
}
_capacity = capacity;
_size = 0;
}
void Push(DataType data)
{
//CheckCapacity();
_array[_size] = data;
_size++;
}
//其他方法...
~Stack()
{
if (_array)
{
free(_array);
_array = NULL;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _array;
int _capacity;
int _size;
};
class MyQueue
{
public:
//初始化列表,不管你写不写,每个成员变量都会先走一遍
//自定义类型的成员会调用默认构造(没有默认构造就编译报错)
//内置类型有缺省值用缺省值,没有的话,不确定,要看编译器,有的编译器会处理,有的不会处理
//先走初始化列表 + 再走函数体
//实践中:尽可能使用初始化列表初始化,不方便的再使用函数体初始化
MyQueue()
:_size(1)
,_pushst(10)
,_ptr((int*)malloc(40))
{
memset(_ptr, 0, 40);
}
private:
//声明
Stack _pushst;
Stack _popst;
// 缺省值 给初始化列表用的
int _size = 0;
const int _x = 10;
int* _ptr;
};
int main()
{
MyQueue q;
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
A(int a)
:_a1(a)
,_a2(_a1)
{}
void Print()
{
cout << _a1 << " " << _a2 << endl;//1 随机值
}
private:
int _a2;
int _a1;
};
int main()
{
A aa(1);
aa.Print();
return 0;
}
初始化列表总结:
#include <iostream>
using namespace std;
class A
{
public:
//单参数构造函数
//explicit A(int a)
A(int a)
:_a(a)
{
cout << "A(int a)" << endl;
}
A(const A& aa)
:_a(aa._a)
{
cout << "A(const A& aa)" << endl;
}
private:
int _a;
};
int main()
{
A aa1(1);
//拷贝构造
A aa2 = aa1;
//隐式类型转换
//内置类型转换为自定义类型
//3构造一个A的临时对象,再用这个临时对象拷贝构造aa3
//编译器遇到连续构造 + 拷贝构造 -> 优化为直接构造
A aa3 = 3;
//raa引用的是类型转换中用3构造的临时对象
const A& raa = 3;
A aa4 = 3.3;
//A aa5 = "1111111";//err
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
A(int a1, int a2)
:_a(0)
,_a1(a1)
,_a2(a2)
{}
private:
int _a;
int _a1;
int _a2;
};
int main()
{
A aaa1(1, 2);
A aaa2 = { 1, 2 };
const A& aaa3 = { 1, 2 };
return 0;
}
使用场景的举例:
#include <iostream>
#include <list>
using namespace std;
class A
{
public:
//单参数构造函数
//explicit A(int a)
A(int a)
:_a(a)
{
cout << "A(int a)" << endl;
}
//多参数构造函数
A(int a1, int a2)
:_a(0)
, _a1(a1)
, _a2(a2)
{}
A(const A& aa)
:_a(aa._a)
{
cout << "A(const A& aa)" << endl;
}
private:
int _a;
int _a1;
int _a2;
};
class Stack
{
public:
void Push(const A& aa)
{
//...
}
//...
};
int main()
{
Stack st;
A a1(1);
st.Push(a1);
A a2(2);
st.Push(a2);
st.Push(2);
st.Push(4);
A aa1(1, 2);
st.Push(aa1);
st.Push({ 1,2 });
list<string> lt;
string s1("111");
lt.push_back(s1);
lt.push_back("1111");
return 0;
}
补充:
成员变量缺省值的各种写法:
#include <iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 4)
{
_array = (DataType*)malloc(sizeof(DataType) * capacity);
if (NULL == _array)
{
perror("malloc申请空间失败!!!");
return;
}
_capacity = capacity;
_size = 0;
}
void Push(DataType data)
{
//CheckCapacity();
_array[_size] = data;
_size++;
}
//其他方法...
~Stack()
{
if (_array)
{
free(_array);
_array = NULL;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _array;
int _capacity;
int _size;
};
class A
{
public:
//单参数构造函数
//explicit A(int a)
A(int a)
:_a(a)
{
cout << "A(int a)" << endl;
}
//多参数构造函数
A(int a1, int a2)
:_a(0)
, _a1(a1)
, _a2(a2)
{}
A(const A& aa)
:_a(aa._a)
{
cout << "A(const A& aa)" << endl;
}
private:
int _a;
int _a1;
int _a2;
};
class BB
{
public:
BB()
{
}
private:
//声明给缺省值
int _b1 = 1;
int* _ptr = (int*)malloc(40);
Stack _pushst = 10;
A _a1 = 1;
A _a2 = { 1, 2 };
A _a3 = _a2;
};
int main()
{
BB bb;
return 0;
}
//实现一个类,计算程序中创建出了多少个类对象
#include<iostream>
using namespace std;
class A
{
public:
A()
{
++_scount;
}
A(const A& t)
{
++_scount;
}
~A()
{
--_scount;
}
//没有this指针,只能访问静态成员
static int GetCount()
{
return _scount;
}
private:
//声明
int _a1 = 1;
int _a2 = 1;
//public:
//声明
//静态区,不存在对象中
//不能给缺省值,因为缺省值是给初始化列表
//它在静态区不在对象中,不走初始化列表
//属于整个类,属于所有对象
static int _scount;
};
//定义
int A::_scount = 0;
A func()
{
A aa4;
return aa4;
}
int main()
{
A aa1;
cout << sizeof(aa1) << endl;//8
A aa2;
A aa3(aa1);
func();
//aa1._scount++;
//cout << A::_scount << endl;
cout << A::GetCount() << endl;
return 0;
}
例题:
class Sum
{
public:
Sum()
{
_ret += _i;
++_i;
}
static int GetRet()
{
return _ret;
}
private:
static int _i;
static int _ret;
};
int Sum::_i = 1;
int Sum::_ret = 0;
class Solution
{
public:
int Sum_Solution(int n)
{
//变长数组
Sum arr[n];
return Sum::GetRet();
}
};
class Time
{
//声明 Date是Time的友元
//Date中可以访问Time的私有
//但是Time中不能访问Date的私有
friend class Date;
public:
Time(int hour = 0, int minute = 0, int second = 0)
:_hour(hour)
, _minute(minute)
, _second(second)
{}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
,_month(month)
,_day(day)
{}
void SetTimeOfDay(int hour, int minute, int second)
{
//直接访问时间类私有的成员变量
_t._hour = hour;
_t._minute = minute;
_t._second = second;
}
private:
int _year;
int _month;
int _day;
Time _t;
};
#include <iostream>
using namespace std;
class A
{
private:
static int k;
int h;
public:
void fun()
{}
//内部类
//独立的类,放到A里面
//仅仅受到类域限制
class B //B天生就是A的友元
{
public:
void foo(const A& a)
{
cout << k << endl;//OK
cout << a.h << endl;//OK
}
private:
int _b;
};
};
int main()
{
cout << sizeof(A) << endl;//4
A a1;
A::B b1;
return 0;
}
因此,上面讲到的例题可以这样修改:
//1.Sum变成Solution的专属类,封装起来
//2.内部类是外部类的友元
class Solution
{
class Sum
{
public:
Sum()
{
_ret += _i;
++_i;
}
};
static int _i;
static int _ret;
public:
int Sum_Solution(int n)
{
//变长数组
Sum arr[n];
return _ret;
}
};
int Solution::_i = 1;
int Solution::_ret = 0;
#include <iostream>
using namespace std;
class A
{
public:
A(int a = 0)
:_a(a)
{
cout << "A(int a)" << endl;
}
~A()
{
cout << "~A()" << endl;
}
private:
int _a;
};
class Solution
{
public:
int Sum_Solution(int n)
{
//...
return n;
}
};
int main()
{
A aa1;
A aa2(1);
//匿名对象,生命周期只在当前这一行
A(2);
A aa3(1);
Solution s1;
s1.Sum_Solution(10);
Solution().Sum_Solution(10);
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
A(int a = 0)
:_a(a)
{
cout << "A(int a)" << endl;
}
A(const A& aa)
:_a(aa._a)
{
cout << "A(const A& aa)" << endl;
}
A& operator=(const A& aa)
{
cout << "A& operator=(const A& aa)" << endl;
if (this != &aa)
{
_a = aa._a;
}
return *this;
}
~A()
{
cout << "~A()" << endl;
}
private:
int _a;
};
//void f1(const A& aa)
//{}
//
//int main()
//{
// //引用传参
// A aa1;
// f1(aa1);
// cout << endl;
//
// f1(A(2));
// cout << endl;
//
// f1(2);
// cout << endl;
//
// return 0;
//}
//void f1(A aa)
//{}
//
//int main()
//{
// //传值传参
// A aa1;
// f1(aa1);
// cout << endl;
//
// //连续一个表达式中,构造 + 拷贝构造 -> 优化为直接构造
// f1(A(2));
// cout << endl;
//
// f1(2);
// cout << endl;
//
// A aa3 = 3;
// cout << endl;
//
// return 0;
//}
A f2()
{
A aa;
return aa;
}
int main()
{
//连续一个表达式中,拷贝构造 + 拷贝构造 -> 优化为一个拷贝构造
A ret = f2();
cout << endl;
//无法优化,建议尽量不要这样写
A ret2;
ret2 = f2();//赋值运算符重载
cout << endl;
return 0;
}
补充一个题:
这里用到的方法类似于我们实现日期类时的GetMonthDay函数:
#include <iostream>
using namespace std;
int main()
{
int year, month, day;
cin >> year >> month >> day;
//计算1-N月的累计天数
int monthDay1_N[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
int n = monthDay1_N[month - 1] + day;
if (month > 2 && ((0 == year % 4 && year % 100 != 0) || (0 == year % 400)))
{
++n;
}
cout << n << endl;
return 0;
}