C++类与对象二

目录

一、类的嵌套

二、对象引用私有数据成员

通过公有函数为私有成员赋值

利用指针访问私有数据成员

利用函数访问私有数据成员

利用引用访问私有数据成员

三、成员函数重载

四、this指针


一、类的嵌套

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

class CC1
{
public:
    int x;
    void Func();
    
    class CC2 
    {
    public:
        int x;
        void Func();
    }obc;
};

void CC1::Func()
{
    x = 3000;
    cout << "x=" << x << endl;
}

void CC1 :: CC2::Func()
{
    x = 4000;
    cout << "x=" << x << endl;
}

int main()
{ 
    CC1 obj;
    obj.Func();
    obj.obc.Func();

    cout << endl;
    cout << "2:x=" << obj.x << endl;
    cout << "2:x=" << obj.obc.x << endl;

    return 0;
}

二、对象引用私有数据成员

通过公有函数为私有成员赋值

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

//通过仅有函数为私有数据成员赋值
class CTest {
    int x, y;
public:
    void setxy(int a, int b)
    {
        x = a;
        y = b;
    }
    void dispxy()
    {
        cout << "x=" << x << ",y=" << y << endl;
    }
};

int main()
{
    CTest obj1;
    obj1.setxy(1, 2);
    obj1.dispxy();
}

利用指针访问私有数据成员

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

//利用指针访问私有数据成员
class CTest {
    int x, y;
public:
    void setxy(int a, int b){
        x = a;
        y = b;
    }

    void printxy() {
        cout << "x=" << x << ",y=" << y << endl;
    }

    void getxy(int* px, int* py) {   //提取x y的值
        *px = x;
        *py = y;
    }

};

int main()
{
    CTest obj;
    obj.setxy(100, 200);
    obj.printxy();

    int m, n;
    obj.getxy(&m, &n);
    cout << "m=" << m << ",n=" << n << endl;
}

利用函数访问私有数据成员

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

//利用函数访问私有数据成员
class CTest {
    int x, y;
public:
    void setxy(int a, int b){
        x = a;
        y = b;
    }

    void printxy() {
        cout << "x=" << x << ",y=" << y << endl;
    }

    int getx() { return x; }
    int gety() { return y; }

};

int main()
{
    CTest obj;
    obj.setxy(100, 200);
    obj.printxy();
    cout << "x=" << obj.getx() << endl;
    cout << "y=" << obj.gety() << endl;

}

利用引用访问私有数据成员

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

//利用引用访问私有数据成员
class CTest {
    int x, y;
public:
    void setxy(int a, int b){
        x = a;
        y = b;
    }

    void printxy() {
        cout << "x=" << x << ",y=" << y << endl;
    }

    void getxy(int& px, int& py) {
        px = x;
        py = y;
    }

};

int main()
{
    CTest obj;
    obj.setxy(100, 200);
    obj.printxy();
   
    int m, n;
    obj.getxy(m, n);
    cout << "m=" << m << endl;
    cout << "n=" << n << endl;
}

三、成员函数重载

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

class CTest
{
    int x, y;
    int m, n;
public:
    void setxy(int a, int b) {
        x = a;
        y = b;
    }
    void setxy(int a, int b, int c, int d) {
        x = a;
        y = b;
        m = c;
        n = d;
    }

    void dispxy(int x) {
        cout << x << "," << y << endl;
    }
    void dispxymn() {
        cout << x << "," << y << "," << m << "," << n << endl;
    }
    
};
int main()
{
    CTest obj1, obj2;

    obj1.setxy(10, 20);
    obj2.setxy(10, 20, 30, 40);

    obj1.dispxy(666);
    obj2.dispxymn();
    return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;

class CTest
{
    int x, y;

public:
    void setxy(int a, int b) {
        x = a;
        y = b;
    }
    void dispxy()
    {
        cout << "x=" << x << "y=" <<y<< endl;
    }
    int sum()
    {
        return x + y;
    }

};
int main()
{
    CTest obj1, obj2;//定义对象
    CTest* pobj;     //对象类的指针(对象指针)
    pobj = &obj1;
    pobj->setxy(3, 4);
    pobj->dispxy();
    cout << "x+y=" << pobj->sum() << endl;
    return 0;
}

四、this指针

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

class CTest
{
private:
    int x;
public:
    int getx() const {
        return x;
    }
    void setx(int x) {
        this->x=x;
        cout << "this指针存储的内存地址为:" << this << endl;
    }
};
int main()
{
    CTest obj;
    obj.setx(888);
    cout << "对象obj在内存的地址为:" << &obj << endl;
    cout << "对象obj所保存的值为:" << obj.getx() << endl;

    return 0;
}
相关推荐
南玖yy9 分钟前
C语言:结构体
c语言·开发语言
〖是♂我〗2 小时前
自定义数据集 使用scikit-learn中svm的包实现svm分类
开发语言·python
南玖yy2 小时前
C语言:整型提升
c语言·开发语言
iqay2 小时前
【C语言】填空题/程序填空题1
c语言·开发语言·数据结构·c++·算法·c#
程序猿编码2 小时前
自定义命令执行器:C++中命令封装的深度探索(C/C++实现)
linux·c语言·c++·网络安全·shell·命令行
lsx2024062 小时前
ECharts 样式设置
开发语言
沈韶珺3 小时前
Elixir语言的安全开发
开发语言·后端·golang
wen__xvn3 小时前
每日一题洛谷B3865 [GESP202309 二级] 小杨的 X 字矩阵c++
c++·算法·矩阵
makabaka_T_T3 小时前
25寒假算法刷题 | Day1 | LeetCode 240. 搜索二维矩阵 II,148. 排序链表
数据结构·c++·算法·leetcode·链表·矩阵
go54631584654 小时前
python 从知网的期刊导航页面抓取与农业科技相关的数据
开发语言·python·科技