画册设计要求,一个网站拿到手里想做优化第一步怎么做,网站建设服务费税率多少钱,烟台市牟平区建设局网站类属性与类方法
注意#xff1a;这不是教程#xff0c;是学习笔记#xff0c;不适合初学者阅读#xff01;#xff01;
类属性与方法
成员属性与成员方法
在类中定义的方法与属性#xff0c;就叫做成员属性与方法。
类属性与类方法
依然在类里面#xff0c;但区别…类属性与类方法
注意这不是教程是学习笔记不适合初学者阅读
类属性与方法
成员属性与成员方法
在类中定义的方法与属性就叫做成员属性与方法。
类属性与类方法
依然在类里面但区别是
属性A有自己的成员属性B有自己的成员属性C也有自己的成员属性。也就是它们的成员属性都是不一样的每一个人都拥有属于自己的成员属性。而类属性则是指的每个成员所访问的属性空间是一样的大家拥有的是同一份。方法在成员方法中我们可以访问 this 指针如果在类方法中不可以访问 this 指针。
访问类属性的三个方法
通过对象访问通过类的命名空间通过作用域的默认规则
class Point {
public :Point() : x(0), y(0) {}Point(int x, int y) : x(x), y(y) {}void set_x(int x) { this-x x; }void set_y(int y) { this-y y; }int get_x() {// this-get_x_cnt;// get_x_cnt;Point::get_x_cnt; // 每次调用get_xget_x_cnt加1return this-x; }int get_y() { return this-y; }private :// 定义类属性// 现在来实现这样的一个需求:get_x被调用的次数// static的作用是使得该变量不需要依赖对象来访问static int get_x_cnt; // 声明类属性int x, y;
}最不容易让人出错的方式通过命名空间访问 Point::get_x_cnt
类的作用域 成员变量只在类中有效。 代码示例演示了类属性、类方法和作用域
#include iostream
#include fstream
#include string
#include vector
#include map
#include unordered_map
#include set
#include unordered_set
#include list
#include queue
#include stack
#include deque
#include bitset
#include cstdint
#include cstdio
#include cstdlib
#include cmath
#include cstring
#include ctime
#include cassert
#include complex
#include numeric
#include algorithm
#include iomanip
#include climits
#include limits
#include chronousing namespace std;class Point {
public :Point() : x(0), y(0) {}Point(int x, int y) : x(x), y(y) {}void set_x(int x) { this-x x; }void set_y(int y) { this-y y; }int get_x() {// this-get_x_cnt;// get_x_cnt;Point::get_x_cnt; // 每次调用get_xget_x_cnt加1return this-x; }int get_y() { return this-y; }// 类方法static int x_cnt() {return Point::get_x_cnt; // 返回类属性}private :// 定义类属性// 现在来实现这样的一个需求:get_x被调用的次数// static的作用是使得该变量不需要依赖对象来访问static int get_x_cnt; // 声明类属性int x, y;
};int Point::get_x_cnt 0; // 初始化类属性定义int main() {Point p1(3, 4), p2(5, 6);cout p1.get_x() , p1.get_y() endl;cout p2.get_x() , p2.get_y() endl;p1.get_x(), p1.get_x(), p1.get_x();cout x_cnt : Point::x_cnt() endl;return 0;
}