1.常对象
常对象只能调用常成员函数(也可调用构造和析构函数)
常对象:const 类名 对象名(初值) 或 类名 const 对象名(初值)
常成员函数:函数类型 函数名() const
#include <iostream>
using namespace std;
class Time2
{
private:
int hour;
int min;
int sec;
public:
Time2(int x = 10, int y = 10, int z = 10)
{
hour = x;
min = y;
sec = z;
cout << "Constructor called." << endl;
}
void set()
{
cout << "请输入时分秒:" << endl;
cin >> hour;
cin >> min;
cin >> sec;
}
void show()const
{
cout << hour << ":" << min << ":" << sec << endl;
}
};
int main1()
{
const Time2 t1(1, 2, 3); //常对象
t1.show(); //show为常成员函数,可以引用
Time2 t2;
t2.set();
t2.show();
return 0;
}
2.常数据成员
常数据成员:const 数据类型 数据名
对常数据成员进行初始化只能用构造函数的参数初始化表
#include <iostream>
using namespace std;
class Time2
{
private:
const int hour; //hour为常数据成员
int min;
int sec;
public:
/*Time2(int x, int y, int z)
{
hour = x;
min = y;
sec = z;
}*/ //这样的初始化方法是不被允许的
Time2(int x, int y, int z) :hour(x)//参数列表的形式赋值
{
min = y;
sec = z;
cout << "Constructor called." << endl;
}
~Time2()
{
cout << "Destructor called." << endl;
}
void show()
{
cout << hour << ":" << min << ":" << sec << endl;
}
};
int main2()
{
Time2 t1(1, 2, 3);
t1.show();
return 0;
}
3.常成员函数
常成员函数:函数类型 函数名() const
对于本类中的数据,常成员函数只能引用,不能修改值
常成员函数不能调用另一个非const成员函数
#include <iostream>
using namespace std;
class Time3
{
private:
int hour;
int min;
int sec;
public:
Time3(int x, int y, int z)
{
hour = x;
min = y;
sec = z;
cout << "Constructor called." << endl;
}
~Time3()
{
cout << "Destructor called." << endl;
}
/*void set() const
{
cout << "请输入时分秒:" << endl;
cin >> hour;
cin >> min;
cin >> sec;
}*/ //此函数会报错,因为常成员函数不能修改类的成员变量
void show()
{
cout << hour << ":" << min << ":" << sec << endl;
}
};
int main3()
{
Time3 t1(1, 2, 3);
t1.show();
return 0;
}
4.指向对象的常指针
指向对象的常指针
指向对象的常指针变量: 类名 * const 指针变量名(=对象地址);
例如:Time* const p= &t1;
p指向t1,p的值不能改变,但t1的成员数据的值可以改变
往往用常指针作为函数的形参,目的是不允许在函数执行过程中改变指针变量的值,使其始终指向原来的对象
#include <iostream>
using namespace std;
class Time4
{
private:
public:
int hour;
int min;
int sec;
Time4(int x=1, int y=2, int z=3)
{
hour = x;
min = y;
sec = z;
cout << "Constructor called." << endl;
}
~Time4()
{
cout << "Destructor called." << endl;
}
void set()
{
cout << "请输入时分秒:" << endl;
cin >> hour;
cin >> min;
cin >> sec;
}
void show()
{
cout << hour << ":" << min << ":" << sec << endl;
}
};
void add(Time4 *const q)
{
cout << "时分秒加起来是:" << endl;
cout<<q->hour + q->min + q->sec<<endl;
}
int main4()
{
Time4 t1;
Time4* const p = &t1; //指向对象的常指针
t1.show();
p->show();
t1.set();
t1.show();
p->show();
p->set();
p->show();
Time4 t2;
add(&t2);
return 0;;
}
5.指向常对象的指针变量
const 类型名 * 指针变量名
指针的指向可以改变,不能通过他来改变所指对象的值
(一)若一个变量已被声明为常变量,只能用指向常变量的指针指向它;(二)指向常变量的指针也可指向非const变量
如果形参是非const变量的指针,实参只能用指向普通变量的指针;形参为指向const类型的指针,则实参用两种指针都可
往往将其作为函数的形参,目的是不允许在函数执行过程中改变指针变量的值,使其始终指向原来的对象
#include <iostream>
#include <string.h>
using namespace std;
int main5()
{
const char c1[] = "boy";
const char* p1 = c1;
/* char* p2 = c1; */ //非法,只能用指向常变量的指针来指向常变量
char c2[] = "girl";
const char* p3 = c2;
char* p4 = c2;
char c3 = 'a';
char* p5 = &c3;
*p5 = 'b';
cout << "c3为:" <<c3<<endl;
const char* p6 = &c3;
/* p6 = 'c'; */ //非法,不能通过指向对面的指针变量来改变所指向对象的值,普通的指针可以
return 0;
}
6.对象的常引用
const 类名 & 引用名 = 已有对象名
不能通过引用来修改相应对象的值
#include <iostream>
using namespace std;
class Time6
{
private:
public:
int hour;
int min;
int sec;
Time6(int x, int y, int z)
{
hour = x;
min = y;
sec = z;
cout << "Constructor called." << endl;
}
~Time6()
{
cout << "Destructor called." << endl;
}
};
void fun(Time6& t)
{
t.hour = 8;
}
/* void fun2(const Time6& t)
{
t.min = 9;
} */ //非法,不能通过常引用修改对象的值,普通引用可以
int main6()
{
Time6 t1(1, 2, 3);
fun(t1);
cout << t1.hour << ":" << t1.min << ":" << t1.sec << endl;
return 0;
}