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;
}
相关推荐
牵牛老人36 分钟前
Qt C++ 复杂界面处理:巧用覆盖层突破复杂界面处理难题之一
数据库·c++·qt
云:鸢37 分钟前
C语言链表设计及应用
c语言·开发语言·数据结构·链表
离越词1 小时前
C++day8作业
开发语言·c++·windows
MMjeaty2 小时前
deque容器
c++
CYRUS_STUDIO2 小时前
如何防止 so 文件被轻松逆向?精准控制符号导出 + JNI 动态注册
android·c++·安全
℃CCCC2 小时前
请求库-axios
开发语言·华为·网络请求·harmonyos·deveco studio·axios请求·arkts编程
CYRUS_STUDIO2 小时前
C&C++ 代码安全再升级:用 OLLVM 给 so 加上字符串加密保护
c++·安全·llvm
ling__i2 小时前
java day18
java·开发语言
矛取矛求2 小时前
日期类的实现
开发语言·c++·算法
大翻哥哥2 小时前
Python 2025:AI工程化与智能代理开发实战
开发语言·人工智能·python