(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;
- }
#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)利用指针访问私有数据成员
- #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;
}
(3)利用函数访问私有数据成员
- #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;
- }
#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)利用引用访问私有数据成员
- #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;
- }
#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】下面设计一个三角形类,请给出各成员函数的定义
- #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类中的各个成员函数
#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成员函数,要处理为内置成员函数,直接在类内定义。
- int main()
- {
- Triangle tri1; //定义三角形类的一个实例(对象)
- double x,y,z;
- cout<<"请输入三角形的三边:";
- cin>>x>>y>>z;
- tri1.setA(x);
- tri1.setB(y);
- tri1.setC(z); //为三边置初值
- if(tri1.isTriangle())
- {
- cout<<"三条边为:"<<tri1.getA()<<','<<tri1.getB()<<','<<tri1.getC()<<endl;
- cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;
- }
- else
- cout<<"不能构成三角形"<<endl;
- return 0;
- }