mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-04-13 17:50:59 +08:00
support bazel complie this project and format code.
This commit is contained in:
40
practical_exercises/10_day_practice/day4/const/obj_func.cpp
Normal file
40
practical_exercises/10_day_practice/day4/const/obj_func.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class R {
|
||||
public:
|
||||
R(int r1, int r2) {
|
||||
R1 = r1;
|
||||
R2 = r2;
|
||||
}
|
||||
// const区分成员重载函数
|
||||
void print();
|
||||
void print() const;
|
||||
|
||||
private:
|
||||
int R1, R2;
|
||||
};
|
||||
/*
|
||||
常成员函数说明格式:类型说明符 函数名(参数表)const;
|
||||
这里,const是函数类型的一个组成部分,因此在实现部分也要带const关键字。
|
||||
const关键字可以被用于参与对重载函数的区分
|
||||
通过常对象只能调用它的常成员函数
|
||||
*/
|
||||
|
||||
void R::print() {
|
||||
cout << "普通调用" << endl;
|
||||
cout << R1 << ":" << R2 << endl;
|
||||
}
|
||||
//实例化也需要带上
|
||||
void R::print() const {
|
||||
cout << "常对象调用" << endl;
|
||||
cout << R1 << ";" << R2 << endl;
|
||||
}
|
||||
int main() {
|
||||
R a(5, 4);
|
||||
a.print(); //调用void print()
|
||||
//通过常对象只能调用它的常成员函数
|
||||
const R b(20, 52);
|
||||
b.print(); //调用void print() const
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user