运行下面程序,观察输出结果
#include<iostream> using namespace std; int x = 5, y = 7; void myFunction() { int y = 10; cout << "x from myFunction:" << x << "\n"; cout << "y from myFunction: " << y << "\n\n"; } int main() { cout << "x from main:" << x << "\n"; cout << "y from main:" << y << "\n\n"; myFunction(); cout << "Back from myFunction!\n\n"; cout << "x from main:" << x << "\n"; cout << "y from main:" << y << "\n"; return 0; }
题目描述:
定义一个Cat类,拥有静态数据成员numOFCats,记录Cat的个体数目,静态成员函数getNumOFCats(),读取numOFCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法。
#include<iostream> using namespace std; class Cat { private: int a; static int numOFCats; public: Cat(int a) :a(a) { numOFCats++; } static int getnumOFCats() { return numOFCats; } ~Cat() { numOFCats--; } }; int Cat::numOFCats = 0; int mian() { int a; cin >> a; Cat x(a); cout << x.getnumOFCats() << endl; }
标签:静态数据,25,int,numOFCats,成员,Cat,打卡 From: https://www.cnblogs.com/xuechenhao173/p/17432698.html