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;
}
相关推荐
知然8 小时前
鸿蒙 Native API 的封装库 h2lib_arkbinder
c++·arkts·鸿蒙
粟悟饭&龟波功8 小时前
Java—— ArrayList 和 LinkedList 详解
java·开发语言
冷雨夜中漫步8 小时前
Java中如何使用lambda表达式分类groupby
java·开发语言·windows·llama
十五年专注C++开发8 小时前
Qt .pro配置gcc相关命令(三):-W1、-L、-rpath和-rpath-link
linux·运维·c++·qt·cmake·跨平台编译
a4576368768 小时前
Objective-c Block 面试题
开发语言·macos·objective-c
Cai junhao8 小时前
【Qt】Qt控件
开发语言·c++·笔记·qt
uyeonashi9 小时前
【QT系统相关】QT网络
开发语言·网络·c++·qt
程序猿小D9 小时前
第27节 Node.js Buffer
linux·开发语言·vscode·node.js·c#·编辑器·vim
武昌库里写JAVA10 小时前
【微服务】134:SpringCloud
java·开发语言·spring boot·学习·课程设计
yaoxin52112310 小时前
105. Java 继承 - 静态方法的隐藏
java·开发语言·jvm