参数的初始化:
#include<iostream>
using namespace std;
class Time
{
public:
int hour;
//int hour = 15;//C++11里才可以(类没有存储的地方,故不能这么做)
private:
};
class Time2
{
public:
void display()
{cout << this->hour << endl;}
private:
int hour;
};
int main()
{
Time t1 = { 1 };//只适用于public的成员
Time2 t2;
cout << t1.hour << endl;
//cout << t2.hour << endl;//私有成员不能这么用了
t2.display();
return 0;
}
构造函数、析构函数、参数初始化表:
#include<iostream>
using namespace std;
class Time2
{
public:
//构造函数名字与本类名Time2相同;而且没有类型,没有返回值
Time2()//无】参数的构造函数
{
hour = -1;
min = -1;
}
Time2(int h)//有】参数的构造函数(重载1)
{
hour = h;
}
Time2(int h, int m) :hour(h), min(m) {}//参数初始化表
//有】参数的构造函数(重载2)
~Time2()//只能有一个析构函数,(构造函数可以多个
{ cout << "析构函数已执行" ;
display();
cout << endl;
}
void display()
{cout << this->hour <<':'<< this->min << endl;}
private:
int hour;
int min;
};
int main()
{ //所有的Time2类会有析构函数的执行
Time2 t1;//因构造函数赋值为-1
Time2 t2(2);
Time2 t3(3,15);
return 0;
}
构造函数包括:
默认构造函数
用于初始化的构造函数
用于赋值对象的构造函数
转换类型的构造函数(详见我的另一篇c++重载的博文https://blog.csdn.net/sinat_27382047/article/details/73838734)
对象的复制:
#include<iostream>
using namespace std;
class Time
{
public:
Time()
{
hour = -1;
min = -1;
}
void set()
{
hour = 10;
min = 10;
}
void display()
{cout << this->hour <<':'<< this->min << endl;}
private:
int hour;
int min;
};
int main()
{
Time t1;
Time t2;
t2.set();//t2设置
t1 = t2;//对象的赋值【对象的“整体”赋值】
cout << "t1:" << endl;
t1.display();
Time t4(t2);//从t2复制出t4【对象的复制】
cout << "t4:" << endl;
t4.display();
return 0;
}
友元、友元类
#include <iostream>
using namespace std;
class Date; // 对Date类的提前引用声明
class Time
{
public:
Time(int, int, int);
void display(Date&);
// display()是成员函数,形参是Date类对象的引用
private:
int hour;
int minute;
int sec;
};
class Date // 声明Date类
{
public:
Date(int, int, int);
friend void Time::display(Date &);
// 声明Time类中的display函数为本类的【友元成员函数】Time可以用Date的这个
private:
int month;
int day;
int year;
};
Time::Time(int h, int m, int s) // 定义Time类的构造函数
{
hour = h;
minute = m;
sec = s;
}
void Time::display(Date &d) //定义display函数
{
cout << d.month << " / " << d.day << " / " << d.year << endl;
// 输出年月日
cout << hour << ":" << minute << " : " << sec << endl;
// 输出时间
}
Date::Date(int m, int d, int y) //类Date的构造函数
{
month = m;
day = d;
year = y;
}
int main()
{
Time t1(10, 13, 56);
Date d1(12, 25, 2004);
t1.display(d1);
return 0;
}
类模板:
格式:
类模板名 <实际的类型名> 对象名(参数);
如,Compare <int> cmp(4, 7);
#include <iostream>
using namespace std;
template<class numtype>
class Compare // 定义类模板,名为Compare
{
public:
Compare(numtype a, numtype b)
{
x = a; y = b;
}
numtype max()
{
return (x>y) ? x : y;
}
numtype min()
{
return (x<y) ? x : y;
}
private:
numtype x, y;
};
int main()
{
Compare <int> cmp1(3, 7); // 定义cmp1,操作两个整数
cout << cmp1.max() << " is the Maximum of two inteder numbers."<<endl;
cout << cmp1.min() << " is the Minimum of two inteder numbers." << endl << endl;
Compare<float> cmp2(45.78, 93.6);
// 定义cmp2,操作两个浮点数
cout << cmp2.max() << " is the Maximum of two float numbers."<<endl;
cout << cmp2.min() << " is the Minimum of two float numbers."<<endl<<endl;
Compare <char> cmp3('a', 'A'); // 定义cmp3,操作两个字符
cout << cmp3.max() << " is the Maximum of two characters."<<endl;
cout << cmp3.min() << " is the Minimum of two characters."<<endl;
return 0;
}
const类型小结
(书上的图)