用不同的方法访问私有成员


(1)通过公共函数为私有成员赋值
  1. #include <iostream>  
  2. using namespace std;  
  3. class Test  
  4. {  
  5. private:  
  6.     int x, y;  
  7. public:  
  8.     void setX(int a)  
  9.     {  
  10.         x=a;  
  11.     }  
  12.     void setY(int b)  
  13.     {  
  14.         y=b;  
  15.     }  
  16.     void printXY(void)  
  17.     {  
  18.         cout<<"x="<<x<<'\t'<<"y="<<y<<endl;  
  19.     }  
  20. } ;  
  21. int main()  
  22. {  
  23.     Test p1;  
  24.     p1.setX(3);  
  25.     p1.setY(5);  
  26.     p1.printXY( );  
  27.     return 0;  
  28. }  
#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)利用指针访问私有数据成员
  1. #include <iostream>  
  2. using namespace std;  
  3. class Test  
  4. {  
  5. private:  
  6.     int x,y;  
  7. public:  
  8.     void setX(int a)  
  9.     {  
  10.         x=a;  
  11.     }  
  12.     void setY(int b)  
  13.     {  
  14.         y=b;  
  15.     }  
  16.     void getXY(int *px, int *py)  
  17.     {  
  18.         *px=x;    //提取x,y值  
  19.         *py=y;  
  20.     }  
  21. };  
  22. int main()  
  23. {  
  24.     Test p1;  
  25.     p1.setX(3);  
  26.     p1.setY(5);  
  27.     int a,b;  
  28.     p1.getXY(&a,&b);  //将 a=x, b=y  
  29.     cout<<a<<'\t'<<b<<endl;  
  30.     return 0;  
  31. }  
#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;
}

(3)利用函数访问私有数据成员
  1. #include <iostream>  
  2. using namespace std;  
  3. class Test  
  4. {  
  5. private:  
  6.     int x,y;  
  7. public:  
  8.     void setX(int a)  
  9.     {  
  10.         x=a;  
  11.     }  
  12.     void setY(int b)  
  13.     {  
  14.         y=b;  
  15.     }  
  16.     int getX(void)  
  17.     {  
  18.         return x;   //返回x值  
  19.     }  
  20.     int getY(void)  
  21.     {  
  22.         return y;   //返回y值  
  23.     }  
  24. };  
  25. int main()  
  26. {  
  27.     Test p1;  
  28.     p1.setX(3);  
  29.     p1.setY(5);  
  30.     int a,b;  
  31.     a=p1.getX( );  
  32.     b=p1.getY();  
  33.     cout<<a<<'\t'<<b<<endl;  
  34.     return 0;  
  35. }     
#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;
    p1.setX(3);
    p1.setY(5);
    int a,b;
    a=p1.getX( );
    b=p1.getY();
    cout<<a<<'\t'<<b<<endl;
    return 0;
}	

(4)利用引用访问私有数据成员
  1. #include <iostream>  
  2. using namespace std;  
  3. class Test  
  4. {  
  5. private:  
  6.     int x,y;  
  7. public:  
  8.     void setX(int a)  
  9.     {  
  10.         x=a;  
  11.     }  
  12.     void setY(int b)  
  13.     {  
  14.         y=b;  
  15.     }  
  16.     void getXY(int &px, int &py) //引用  
  17.     {  
  18.         px=x;    //提取x,y值  
  19.         py=y;  
  20.     }  
  21. };  
  22. int main()  
  23. {  
  24.     Test p1,p2;  
  25.     p1.setX(3);  
  26.     p1.setY(5);  
  27.     int a,b;  
  28.     p1.getXY(a, b); //将 a=x, b=y  
  29.     cout<<a<<'\t'<<b<<endl;  
  30.     return 0;  
  31. }  
#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,p2;
    p1.setX(3);
    p1.setY(5);
    int a,b;
    p1.getXY(a, b); //将 a=x, b=y
    cout<<a<<'\t'<<b<<endl;
    return 0;
}

【项目1 - 三角形类1】下面设计一个三角形类,请给出各成员函数的定义 
  1. #include<iostream>  
  2. #include<Cmath>  
  3. using namespace std;  
  4. class Triangle  
  5. {  
  6. public:  
  7.     void setABC(double x, double y, double z);//置三边的值,注意要能成三角形  
  8.     void getABC(double *x, double *y, double *z);//取三边的值  
  9.     double perimeter(void);//计算三角形的周长  
  10.     double area(void);//计算并返回三角形的面积  
  11. private:  
  12.     double a,b,c; //三边为私有成员数据  
  13. };  
  14. int main()  
  15. {  
  16.     Triangle tri1;  //定义三角形类的一个实例(对象)  
  17.     tri1.setABC(4,5,6); //为三边置初值  
  18.     double x,y,z;  
  19.     tri1.getABC (&x,&y,&z);   //将三边的值为x,y,z赋值  
  20.     cout<<"三条边为:"<<x<<'\t'<<y<<'\t'<<z<<endl;  
  21.     cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;  
  22.     return 0;  
  23. }  
  24. //请在下面定义Triangle类中的各个成员函数  
#include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
	void setABC(double x, double y, double z);//置三边的值,注意要能成三角形
	void getABC(double *x, double *y, double *z);//取三边的值
	double perimeter(void);//计算三角形的周长
	double area(void);//计算并返回三角形的面积
private:
	double a,b,c; //三边为私有成员数据
};
int main()
{
	Triangle tri1;	//定义三角形类的一个实例(对象)
	tri1.setABC(4,5,6);	//为三边置初值
	double x,y,z;
	tri1.getABC (&x,&y,&z);   //将三边的值为x,y,z赋值
	cout<<"三条边为:"<<x<<'\t'<<y<<'\t'<<z<<endl;
	cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;
	return 0;
}
//请在下面定义Triangle类中的各个成员函数

【项目2 - 三角形类2】程序功能同项目1,main()函数如下,请重新定义Triangle类,其中逻辑特别简单的set和get成员函数,要处理为内置成员函数,直接在类内定义。
  1. int main()  
  2. {  
  3.     Triangle tri1;  //定义三角形类的一个实例(对象)  
  4.     double x,y,z;  
  5.     cout<<"请输入三角形的三边:";  
  6.     cin>>x>>y>>z;  
  7.     tri1.setA(x);  
  8.     tri1.setB(y);  
  9.     tri1.setC(z);   //为三边置初值  
  10.     if(tri1.isTriangle())  
  11.     {  
  12.         cout<<"三条边为:"<<tri1.getA()<<','<<tri1.getB()<<','<<tri1.getC()<<endl;  
  13.         cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;  
  14.     }  
  15.     else  
  16.         cout<<"不能构成三角形"<<endl;  
  17.     return 0;  
  18. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值