Box Box1; // 声明 Box1,类型为 BoxBox Box2; // 声明 Box2,类型为 Box
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
类的对象的公共数据成员可以使用直接成员访问运算符.来访问
.
Box Box1; // 声明 Box1,类型为 Box Box Box2; // 声明 Box2,类型为 Box Box Box3; // 声明 Box3,类型为 Box double volume = 0.0; // 用于存储体积 // box 1 详述 Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // box 2 详述 Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0;
Box Box3; // 声明 Box3,类型为 Box
double volume = 0.0; // 用于存储体积
// box 1 详述
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 详述
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
需要注意的是,私有的成员和受保护的成员不能使用直接成员访问运算符 (.) 来直接访问。我们将在后续的教程中学习如何访问私有成员和受保护的成员
非常重要
#include <iostream> using namespace std; class Box{ public: double length; void setWidth( double wid ); double getWidth( void ); private: double width;}; // 成员函数定义double Box::getWidth(void){ return width ;} void Box::setWidth( double wid ){ width = wid;} // 程序的主函数int main( ){ Box box; // 不使用成员函数设置长度 box.length = 10.0; // OK: 因为 length 是公有的 cout << "Length of box : " << box.length <<endl; // 不使用成员函数设置宽度 // box.width = 10.0; // Error: 因为 width 是私有的 box.setWidth(10.0); // 使用成员函数设置宽度 cout << "Width of box : " << box.getWidth() <<endl; return 0;}
#include <iostream>
using namespace std;
class Box
{
public:
double length;
void setWidth( double wid );
double getWidth( void );
private:
double width;
};
// 成员函数定义
double Box::getWidth(void)
return width ;
}
void Box::setWidth( double wid )
width = wid;
// 程序的主函数
int main( )
Box box;
// 不使用成员函数设置长度
box.length = 10.0; // OK: 因为 length 是公有的
cout << "Length of box : " << box.length <<endl;
// 不使用成员函数设置宽度
// box.width = 10.0; // Error: 因为 width 是私有的
box.setWidth(10.0); // 使用成员函数设置宽度
cout << "Width of box : " << box.getWidth() <<endl;
return 0;
#include <iostream> using namespace std; class Line{ public: void setLength( double len ); double getLength( void ); Line(double len); // 这是构造函数 private: double length;}; // 成员函数定义,包括构造函数Line::Line( double len){ cout << "Object is being created, length = " << len << endl; length = len;} void Line::setLength( double len ){ length = len;} double Line::getLength( void ){ return length;}// 程序的主函数int main( ){ Line line(10.0); // 获取默认设置的长度 cout << "Length of line : " << line.getLength() <<endl; // 再次设置长度 line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0;}
class Line
void setLength( double len );
double getLength( void );
Line(double len); // 这是构造函数
// 成员函数定义,包括构造函数
Line::Line( double len)
cout << "Object is being created, length = " << len << endl;
length = len;
void Line::setLength( double len )
double Line::getLength( void )
return length;
Line line(10.0);
// 获取默认设置的长度
cout << "Length of line : " << line.getLength() <<endl;
// 再次设置长度
line.setLength(6.0);
使用初始化列表来初始化字段:
Line::Line( double len): length(len){ cout << "Object is being created, length = " << len << endl;}
Line::Line( double len): length(len)
上面的语法等同于如下语法:
Line::Line( double len){ length = len; cout << "Object is being created, length = " << len << endl;}
假设有一个类 C,具有多个字段 X、Y、Z 等需要进行初始化,同理地,您可以使用上面的语法,只需要在不同的字段使用逗号进行分隔,如下所示:
C::C( double a, double b, double c): X(a), Y(b), Z(c){ ....}
C::C( double a, double b, double c): X(a), Y(b), Z(c)
....
#include <iostream> using namespace std; class Line{ public: void setLength( double len ); double getLength( void ); Line(); // 这是构造函数声明 ~Line(); // 这是析构函数声明 private: double length;}; // 成员函数定义,包括构造函数Line::Line(void){ cout << "Object is being created" << endl;}Line::~Line(void){ cout << "Object is being deleted" << endl;} void Line::setLength( double len ){ length = len;} double Line::getLength( void ){ return length;}// 程序的主函数int main( ){ Line line; // 设置长度 line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0;}
Line(); // 这是构造函数声明
~Line(); // 这是析构函数声明
Line::Line(void)
cout << "Object is being created" << endl;
Line::~Line(void)
cout << "Object is being deleted" << endl;
Line line;
// 设置长度
当上面的代码被编译和执行时,它会产生下列结果:
Object is being createdLength of line : 6Object is being deleted
Object is being created
Length of line : 6
Object is being deleted
原文链接:https://www.cnblogs.com/share0956/p/17462315.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728