C++语言基础 例程 对象成员的引用

简介: 贺老师的教学链接  本课讲解通过对象名和成员运算符访问对象中的成员#include <iostream>using namespace std;class Time{public: void set_time( ); void show_time( ); private: int hour; int mi

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


通过对象名和成员运算符访问对象中的成员

#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );   
    void show_time( );  
private:           
    int hour;
    int minute;
    int sec;
};
int main( )
{
    Time t1;        
    t1.set_time( );    
    t1.show_time( );  
    Time t2;      
    t2.set_time( );  
    t2.show_time( );  
    return 0;
}

void Time::set_time( ) 
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}

void Time::show_time( )      
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}


通过指向对象的指针访问对象中的成员
#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );   
    void show_time( );  
private:           
    int hour;
    int minute;
    int sec;
};
int main( )
{
    Time t1, *p;  
    p=&t1;
    t1.set_time( ); 
    (*p).show_time( );   
    p->show_time( );  
    return 0;
}

void Time::set_time( ) 
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}

void Time::show_time( )      
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}


通过对象的引用变量访问对象中的成员
#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );   
    void show_time( );  
private:           
    int hour;
    int minute;
    int sec;
};
int main( )
{
    Time t1;  
    Time &t2=t1;
    t1.set_time( );   
    t2.show_time( );
    return 0;
}


void Time::set_time( ) 
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}


void Time::show_time( )      
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}

访问私有数据成员的常用方法
(1)通过公共函数访问私有成员
#include <iostream>
using namespace std;
class Test
{
private:
    int x, y;
public:
    void setX(int a)
    {
        x=a;
    }
    void setY(int b)
    {
        y=b;
    }
    void printXY(void)
    {
        cout<<"x="<<x<<'\t'<<"y="<<y<<endl;
    }
} ;
int main()
{
    Test p1;
    p1.setX(3);
    p1.setY(5);
    p1.printXY( );
    return 0;
}


(2)(惯例)利用set和get函数访问私有数据成员
#include <iostream>
using namespace std;
class Test
{
private:
    int x,y;
 public:
    void setX(int a) {x=a;}
    void setY(int b) {y=b;}
    int getX(void) {return x;} //返回x值
    int getY(void) {return y;} //返回y值
};
int main()
{
    Test p1,p2;
    p1.setX(3);p1.setY(5);
    int a,b;
    a=p1.getX( ); b=p1.getY(); 
    cout<<a<<'\t'<<b<<endl; 
}


利用指针将私有数据成员的值提取到类外
#include <iostream>
using namespace std;
class Test
{
private:
    int x,y;
public:
    void setX(int a)
    {
        x=a;
    }
    void setY(int b)
    {
        y=b;
    }
    void getXY(int *px, int *py)
    {
        *px=x;    //提取x,y值
        *py=y;
    }
};
int main()
{
    Test p1;
    p1.setX(3);
    p1.setY(5);
    int a,b;
    p1.getXY(&a,&b);  //将 a=x, b=y
    cout<<a<<'\t'<<b<<endl;
    return 0;
}


利用引用将私有数据成员的值提取到类外
#include <iostream>
using namespace std;
class Test
{
private:
    int x,y;
public:
    void setX(int a)
    {
        x=a;
    }
    void setY(int b)
    {
        y=b;
    }
    void getXY(int &px, int &py) //引用
    {
        px=x;    //提取x,y值
        py=y;
    }
};
int main()
{
    Test p1;
    p1.setX(3);
    p1.setY(5);
    int a,b;
    p1.getXY(a, b); //将 a=x, b=y
    cout<<a<<'\t'<<b<<endl;
    return 0;
}


目录
相关文章
|
26天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
32 0
|
1天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
14 0
|
1天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
11 0
|
6天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
6天前
|
存储 编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
|
6天前
|
缓存 编译器 API
NumPy与其他语言(如C/C++)的接口实践
【4月更文挑战第17天】本文介绍了NumPy与C/C++的接口实践,包括Python与C/C++交互基础、NumPy的C API和Cython的使用。通过案例展示了如何将C++函数与NumPy数组结合,强调了内存管理、类型匹配、错误处理和性能优化的最佳实践。掌握这些技能对于跨语言交互和集成至关重要。
|
7天前
|
C++
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
|
7天前
|
存储 编译器 C++
【C++成长记】C++入门 | 类和对象(中) |拷贝构造函数、赋值运算符重载、const成员函数、 取地址及const取地址操作符重载
【C++成长记】C++入门 | 类和对象(中) |拷贝构造函数、赋值运算符重载、const成员函数、 取地址及const取地址操作符重载
|
13天前
|
存储 编译器 C语言
C++类与对象
C++类与对象
15 0
|
15天前
|
程序员 C++
C++语言模板学习应用案例
C++模板实现通用代码,以适应多种数据类型。示例展示了一个计算两数之和的模板函数`add&lt;T&gt;`,可处理整数和浮点数。在`main`函数中,展示了对`add`模板的调用,分别计算整数和浮点数的和,输出结果。
11 2

热门文章

最新文章