0
点赞
收藏
分享

微信扫一扫

c++教材119页程序

舟海君 2022-11-20 阅读 71

源程序:

#include <iostream>
using namespace std;

class constClass
{
private:
    const int conMbr;
    int Mbr;
public:
    constClass():conMbr(0),Mbr(100){};
    constClass(int i):conMbr(i)
    {
        Mbr=230;
    }
    void printConst()
    {
        cout<<"conMbr="<<conMbr<<",Mbr="<<Mbr<<endl;
    }
    int getConst()
    {
        cout<<"调用非常量函数"<<endl;
        return conMbr;
    }
    int getConst() const
    {
        cout<<"调用常量函数"<<endl;
        return conMbr;
    }
    int getValue()
    {
        return Mbr;
    }
    void processConst()
    {
        cout<<"--在processConst函数中 非常量.."<<endl;
        int x=2*conMbr+1;

        cout<<"x=2*conMbr+1"<<x<<endl;
        //conMbr++;
        Mbr++;
        cout<<"Mbr="<<Mbr<<endl;
    }

    void processConst() const
    {
        cout<<"-- 在processConst函数中 常量--"<<endl;
        int x=conMbr+1;
        cout<<"x=conMbr+1="<<x<<endl;
        //conMbr++;
        //Mbr++;
        cout<<"Mbr="<<Mbr<<endl;
    }
};

int main()
{
    constClass ob1(123),ob2;
    ob1.printConst();
    cout<<"ob2.getConst()="<<ob2.getConst()<<endl;
    ob2.processConst();
    const constClass ob3(20);
    cout<<"ob2.getConst()="<<ob3.getConst()<<endl;
    ob3.processConst();
    return 1;
}
运行结果 :

 



举报

相关推荐

C++程序

C++程序基础

C++程序制表

c++程序分析

c++程序练习

C++经典程序

C语言教材摘要

0 条评论