极简c++(4)类的静态成员

静态数据成员


::是作用域操作符!

c 复制代码
#include<iostream>
using namespace std;

class Point{
	private:
		int x,y;
	public:
		point(int x = 0,int y = 0):x(x),y(y){}
		~point();
		int getX(){return x;}
		int getY(){return x;}
}


假设需要统计点的个数,考虑添加一个数据成员;

c 复制代码
#include<iostream>
using namespace std;

class Point{
	private:
		int x,y;
		int count;
	public:
		point(int x = 0,int y = 0):x(x),y(y){}
		~point();
		int getX(){return x;}
		int getY(){return x;}
		void addcount(){count++;}
}

这样非常的麻烦!所以我们采用静态属性

c 复制代码
#include<iostream>
using namespace std;

class Point{
	private:
		int x,y;
		static int count;
	public:
		point(int x = 0,int y = 0):x(x),y(y){}
		~point();
		int getX(){return x;}
		int getY(){return x;}
		void showCount(){cout<<count<<endl;}
}
int Point::count = 0;


突出一个共享;

c 复制代码
#include<iostream>
using namespace std;

class Point{
	private:
		int x,y;
		static int count;
	public:
		Point(int x = 0,int y = 0):x(x),y(y){count++;}
		~Point(){count--;};
		int getX(){return x;}
		int getY(){return x;}
		void showCount(){cout<<count<<endl;}
};
int Point::count = 0;
int main(){
	Point a(4,5);
	cout<<"PointA:"<<a.getX()<<","<<a.getY()<<endl;
	a.showCount();
	Point b;
	cout<<"PointB:"<<b.getX()<<","<<b.getY()<<endl;
	b.showCount();
}

运行结果:
PointA:4,4
1
PointB:0,0
2

静态变量一般只用于"统计对象个数"这种必须多个对象共享的场合!

静态成员函数

c 复制代码
#include<iostream>
using namespace std;

class Point{
	private:
		int x,y;
		static int count;
	public:
		Point(int x = 0,int y = 0):x(x),y(y){count++;}
		Point(Point &p) //复制构造函数
		{
			x = p.x;
			y = p.y;
			count++;
		}
		~Point(){count--;};
		int getX(){return x;}
		int getY(){return x;}
		static void showCount(){cout<<count<<endl;}//只能包含静态数据成员
};
int Point::count = 0;
int main(){
	Point a(4,5);
	cout<<"PointA:"<<a.getX()<<","<<a.getY()<<endl;
	Point::showCount();//类名称调用静态函数
	Point b;
	cout<<"PointB:"<<b.getX()<<","<<b.getY()<<endl;
	Point::showCount();
}

思考:为什么在静态函数中只能包含静态变量?

复制构造函数,如果你没有创建,系统会自动为你创建

作业

静态数据成员存储在静态数据区,而不是每个对象的实例中

c 复制代码
#include<iostream>
using namespace std;

class Rectangle{
	private:
		int width, height;
        static int count;
	public:
		Rectangle();//无参构造函数
		Rectangle(int newwidth,int newheight);//有参构造函数
        ~Rectangle();
        static void getCount(){cout<<"矩形个数:"<<count<<endl;}
		//内联函数,设置属性的值,一般只有这种很短的函数才可以写在定义中
		//this 表示的是当前对象的意思
		void setWidth(int width)
		{
			this->width = width;
		}
		void setHeight(int height)
		{
			this->height = height;
		}
		//获得属性的值
		int getWidth()
		{
			return width;
		}
		int getHeight()
		{
			return height;
		}
        int getArea(){
            int area = this->width * this->height;
            return area;
        }
        int getPerimeter(){
            int Perimeter = (this->width + this->height)*2;
            return Perimeter;
        }
		
};

int Rectangle::count = 0;

Rectangle::Rectangle(){
	this->width = 1;
	this->height = 1;
    count++;
}
Rectangle::Rectangle(int newwidth,int newheight){
	this->width = newwidth;
	this->height = newheight;
    count++;
}
Rectangle::~Rectangle(){
    count--;
}


int main(){
	Rectangle myRectangle1;
	cout<<"利用无参构造函数创建的时间对象"<<endl;
    int W1 = myRectangle1.getWidth();
    int H1 = myRectangle1.getHeight();
    int A1 = myRectangle1.getArea();
    int P1 = myRectangle1.getPerimeter();
	cout<<"矩形的长为:"<<W1<<endl<<"矩形的宽为:"<<H1<<endl<<"矩形的面积为:"<<A1<<endl<<"矩形的周长为:"<<P1<<endl;
    Rectangle::getCount();

    int W2,H2;
    cin>>W2>>H2;
	Rectangle myRectangle2(W2,H2);
	cout<<"利用有参构造函数创建的时间对象"<<endl;
    int A2 = myRectangle2.getArea();
    int P2 = myRectangle2.getPerimeter();
	cout<<"矩形的长为:"<<W2<<endl<<"矩形的宽为:"<<H2<<endl<<"矩形的面积为:"<<A2<<endl<<"矩形的周长为:"<<P2<<endl;
    Rectangle::getCount();
	return 0;
}
c 复制代码
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle{
	private:
		int width, height;
        static int count;
	public:
		Rectangle();//无参构造函数
		Rectangle(int newwidth,int newheight);//有参构造函数
        ~Rectangle();
        static void getCount(){};
		//内联函数,设置属性的值,一般只有这种很短的函数才可以写在定义中
		//this 表示的是当前对象的意思
		void setWidth(int width);
		void setHeight(int height);
		//获得属性的值
		int getWidth();
		int getHeight();
        int getArea();
        int getPerimeter();
};

#endif
c 复制代码
//Rectangle.cpp
#include "Rectangle.h"
#include <iostream>
using namespace std;

int Rectangle::count = 0;

Rectangle::Rectangle(){
	this->width = 1;
	this->height = 1;
    count++;
}
Rectangle::Rectangle(int newwidth,int newheight){
	this->width = newwidth;
	this->height = newheight;
    count++;
}
Rectangle::~Rectangle(){
    count--;
}
void Rectangle::getCount(){cout<<"矩形个数:"<<count<<endl;}
void Rectangle::setWidth(int width){this->width = width;}
void Rectangle::setHeight(int height)
{this->height = height;}
//获得属性的值
int Rectangle::getWidth(){return width;}
int Rectangle::getHeight(){return height;}
int Rectangle::getArea()
{
	int area = this->width * this->height;
	return area;
}
int Rectangle::getPerimeter()
{
	int Perimeter = (this->width + this->height)*2;
	return Perimeter;
}
c 复制代码
//job1.cpp
#include "Rectangle.h"
#include <iostream>
using namespace std;

int main(){
	Rectangle myRectangle1;
	cout<<"利用无参构造函数创建的时间对象"<<endl;
    int W1 = myRectangle1.getWidth();
    int H1 = myRectangle1.getHeight();
    int A1 = myRectangle1.getArea();
    int P1 = myRectangle1.getPerimeter();
	cout<<"矩形的长为:"<<W1<<endl<<"矩形的宽为:"<<H1<<endl<<"矩形的面积为:"<<A1<<endl<<"矩形的周长为:"<<P1<<endl;
    Rectangle::getCount();

    int W2,H2;
    cin>>W2>>H2;
	Rectangle myRectangle2(W2,H2);
	cout<<"利用有参构造函数创建的时间对象"<<endl;
    int A2 = myRectangle2.getArea();
    int P2 = myRectangle2.getPerimeter();
	cout<<"矩形的长为:"<<W2<<endl<<"矩形的宽为:"<<H2<<endl<<"矩形的面积为:"<<A2<<endl<<"矩形的周长为:"<<P2<<endl;
    Rectangle::getCount();
	return 0;
}
相关推荐
2401_857439691 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna1 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_1 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Dream_Snowar2 小时前
速通Python 第三节
开发语言·python
唐诺3 小时前
几种广泛使用的 C++ 编译器
c++·编译器
高山我梦口香糖3 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
冷眼看人间恩怨4 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
信号处理学渣4 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客4 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
Lenyiin4 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin