C++语言基础 例程 静态成员

简介: 贺老师的教学链接  本课讲解问题的提出现状:n个同类的对象,每一个对象都分别有自己的数据成员,各自有值,互不相干。期望:希望有某一个或几个数据成员为某类所有对象所共有,以实现数据共享。方案:用全局变量#include<iostream>using namespace std;int N = 0;class Class{private: int a;publi

贺老师的教学链接  本课讲解



问题的提出
现状:n个同类的对象,每一个对象都分别有自己的数据成员,各自有值,互不相干。
期望:希望有某一个或几个数据成员为某类所有对象所共有,以实现数据共享。
方案:用全局变量
#include<iostream>
using namespace std;
int N = 0;
class Class
{
private:
   int a;
public:
   Class(){N++;a=0;}
   void add(){N++;}
};


int main( )
{
    Class c1, c2;
    N=300;
    c1.add();
    cout<<N<<endl;
    return 0;
}


例 引用静态数据成员
#include <iostream>
using namespace std;
class Student
{
public:
    Student(int n,string nam, int a):
            num(n),name(nam),age(a) { ++count; }
    ~Student() { --count; }
    int getCount()   { return count; }
private:
    static int count;
    int num;
    string name;
    int age;
};


int Student::count=0;
int main( )
{
    Student stu1(1001,"He",40);
    cout<<stu1.getCount()<<endl;
    Student *pt=new Student(1001,"You",20);
    cout<<pt->getCount()<<endl;
    cout<<stu1.getCount()<<endl;
    delete pt;
    cout<<stu1.getCount()<<endl;
    return 0;
}


用类名访问静态数据成员
#include <iostream>
using namespace std;
class Student
{
public:
    Student(int n,string nam, int a):
            num(n),name(nam),age(a) { ++count; }
    ~Student() { --count; }
    int getCount()   { return count; }
    static int count;
private:
    int num;
    string name;
    int age;
};


int Student::count=0;
int main( )
{
    cout<<Student::count<<endl;
    Student *pt=new Student(1001,"You",20);
    cout<<pt->getCount()<<endl;
    delete pt;
    cout<<Student::count<<endl;
    return 0;
}


为什么出错?
#include <iostream>
using namespace std;
class Student
{
public:
    Student(int n,string nam, int a):
            num(n),name(nam),age(a) { ++count; }
    ~Student() { --count; }
    int getCount()   { return count; }
private:
    static int count;
    int num;
    string name;
    int age;
};
int Student::count=0;
int main( )
{
    cout<<Student::getCount()<<endl; 
    cout<<Student::count<<endl;
    Student *pt=new Student(1001,"You",20);
    cout<<pt->getCount()<<endl;
    delete pt;
    return 0;
}


静态成员函数
#include <iostream>
using namespace std;
class Student
{
public:
    Student(int n,string nam, int a):
            num(n),name(nam),age(a) { ++count; }
    ~Student() { --count; }
    static int getCount()   { return count; }
private:
    static int count;
    int num;
    string name;
    int age;
};
int Student::count=0;
int main( )
{
    cout<<Student::getCount()<<endl; 
    Student *pt=new Student(1001,"You",20);
    cout<<pt->getCount()<<endl;
    delete pt;
    return 0;
}


静态成员函数不能处理非静态数据成员
#include <iostream>
using namespace std;
class Student
{
public:
    Student(int n,string nam, int a):
            num(n),name(nam),age(a) { ++count; }
    ~Student() { --count; }
    static int getCount()   
        { age++; return count; }
private:
    static int count;
    int num;
    string name;
    int age;
};
int Student::count=0;
int main( )
{
    cout<<Student::getCount()<<endl; 
    Student *pt=new Student(1001,"You",20);
    cout<<pt->getCount()<<endl;
    delete pt;
    return 0;
}


目录
相关文章
|
1月前
|
编译器 C++
C++语言预处理器学习应用案例
【4月更文挑战第8天】C++预处理器包括条件编译、宏定义和文件包含等功能。例如,条件编译用于根据平台选择不同代码实现,宏定义可简化常量和变量名,文件包含则用于整合多个源文件。示例中展示了如何使用`#ifdef`等指令进行条件编译,当`DEBUG`宏定义时,`PRINT_LOG`会打印调试信息,否则不执行。
14 1
|
15天前
|
Linux 程序员 图形学
C++语言在现代软件开发中的应用与实践
C++语言在现代软件开发中的应用与实践
20 2
|
15天前
|
存储 程序员 C语言
深入理解C++:从语言特性到实践应用
深入理解C++:从语言特性到实践应用
24 3
|
15天前
|
存储 算法 安全
C++语言深度探索:从基础到实践
C++语言深度探索:从基础到实践
14 2
|
27天前
|
机器学习/深度学习 人工智能 大数据
开发语言漫谈-C++
C++最初的名字为“带类的C”
|
27天前
|
缓存 编译器 API
NumPy与其他语言(如C/C++)的接口实践
【4月更文挑战第17天】本文介绍了NumPy与C/C++的接口实践,包括Python与C/C++交互基础、NumPy的C API和Cython的使用。通过案例展示了如何将C++函数与NumPy数组结合,强调了内存管理、类型匹配、错误处理和性能优化的最佳实践。掌握这些技能对于跨语言交互和集成至关重要。
|
1月前
|
存储 C++
C++语言学习指针和引用应用案例
C++中的指针和引用用于高效操作内存。示例展示指针和引用的基本用法:指针`*p`存储变量`a`的地址,引用`&x`在函数调用中实现值交换而无需复制。此外,引用`update(&x)`可直接修改原变量,指针`p`在数组操作中用于遍历和访问不同部分。
12 2
|
1月前
|
C++
C++语言学习数组和字符串应用案例
【4月更文挑战第8天】该文展示了C++中数组和字符串的应用案例。数组示例定义了一个整数数组并访问、修改其元素,计算了元素之和。字符串示例中,定义了一个字符串并遍历、修改字符,进行了字符串拼接、查找子字符串及替换操作。
12 3
|
1月前
|
C++
C++语言学习文件操作应用案例
C++文件操作示例:创建`ofstream`对象写入&quot;Hello, World!&quot;到`output.txt`,刷新缓冲区,然后使用`ifstream`读取并打印文件内容。如果文件打开失败,程序将显示错误信息并返回1。
12 3
|
1月前
|
C++
C++语言异常处理学习应用案例
C++异常处理保证程序在运行时遇到错误(如除数为0)时不崩溃。以下是一个示例:程序接收用户输入的两个整数并进行除法运算。若除数为0,则抛出`std::runtime_error`异常。`try-catch`结构用来捕获并处理异常,当出现异常时,输出错误信息,使程序能继续执行。
17 4