C++教程 第8页

C++ 中指向类的指针-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 中指向类的指针

C++ 指向类的指针 C++ 类 & 对象 一个指向 C++ 类的指针与指向结构的指针类似,访问指向类的指针的成员,需要使用成员访问运算符 ->,就像访问指向结构的指针一样。与所有的指针一样,您必须在使用指针之前,对指针进行初始化。 下面的实例有助于更好地理解指向类的指针的概念: #include <iostream> using namespace std; class Box { public: // 构造函数定义 Box(double l=2.0, double b=2.0, double h=2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; } double Volume() { return length * breadth * height; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; int main(void) { Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 Box *ptrBox; // Declare pointer to a class. // 保存第一个对象的地址 ptrBox = &Box1; // 现在尝试使用成员访问运算符来访问成员 cout << "Volume of Box1: " << ptrBox->Volume() << endl; //...

C++ 递增递减运算符重载-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 递增递减运算符重载

C++ ++ 和 — 运算符重载 C++ 重载运算符和重载函数 递增运算符( ++ )和递减运算符( — )是 C++ 语言中两个重要的一元运算符。 下面的实例演示了如何重载递增运算符( ++ ),包括前缀和后缀两种用法。类似地,您也可以尝试重载递减运算符( — )。 #include <iostream> using namespace std; class Time { private: int hours; // 0 到 23 int minutes; // 0 到 59 public: // 所需的构造函数 Time(){ hours = 0; minutes = 0; } Time(int h, int m){ hours = h; minutes = m; } // 显示时间的方法 void displayTime() { cout << "H: " << hours << " M:" << minutes <<endl; } // 重载前缀递增运算符( ++ ) Time operator++ () { ++minutes; // 对象加 1 if(minutes >= 60) { ++hours; minutes -= 60; } return Time(hours, minutes); } // 重载后缀递增运算符( ++ ) Time operator++( int ) { // 保存原始值 Time T(hours, minutes); //...

C++ 赋值运算符重载-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 赋值运算符重载

C++ 赋值运算符重载 C++ 重载运算符和重载函数 就像其他运算符一样,您可以重载赋值运算符( = ),用于创建一个对象,比如拷贝构造函数。 下面的实例演示了如何重载赋值运算符。 #include <iostream> using namespace std; class Distance { private: int feet; // 0 到无穷 int inches; // 0 到 12 public: // 所需的构造函数 Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } void operator=(const Distance &D ) { feet = D.feet; inches = D.inches; } // 显示距离的方法 void displayDistance() { cout << "F: " << feet << " I:" << inches << endl; } }; int main() { Distance D1(11, 10), D2(5, 11); cout << "First Distance : "; D1.displayDistance(); cout << "Second Distance :"; D2.displayDistance(); // 使用赋值运算符 D1 = D2; cout << "First Distance :"; D1.displayDistance(); return 0; } 当上面的代码被编译和执行时,它会产生下列结果: First...

C++ 友元函数-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 友元函数

C++ 友元函数 C++ 类 & 对象 类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。 友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友元。 如果要声明函数为一个类的友元,需要在类定义中该函数原型前使用关键字 friend,如下所示: class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); }; 声明类 ClassTwo 的所有成员函数作为类 ClassOne 的友元,需要在类 ClassOne 的定义中放置如下声明: friend class ClassTwo; 请看下面的程序: #include <iostream> using namespace std; class Box { double width; public: friend void printWidth( Box box ); void setWidth( double wid ); }; // 成员函数定义 void Box::setWidth( double wid ) { width = wid; } // 请注意:printWidth() 不是任何类的成员函数 void printWidth( Box box ) { /* 因为 printWidth() 是 Box 的友元,它可以直接访问该类的任何成员 */ cout << "Width of box : " << box.width <<endl; } // 程序的主函数 int main( ) { Box box; // 使用成员函数设置宽度 box.setWidth(10.0); // 使用友元函数输出宽度 printWidth( box ); return...

C++ 函数调用运算符 () 重载-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 函数调用运算符 () 重载

C++ 函数调用运算符 () 重载 C++ 重载运算符和重载函数 函数调用运算符 () 可以被重载用于类的对象。当重载 () 时,您不是创造了一种新的调用函数的方式,相反地,这是创建一个可以传递任意数目参数的运算符函数。 下面的实例演示了如何重载函数调用运算符 ()。 #include <iostream> using namespace std; class Distance { private: int feet; // 0 到无穷 int inches; // 0 到 12 public: // 所需的构造函数 Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } // 重载函数调用运算符 Distance operator()(int a, int b, int c) { Distance D; // 进行随机计算 D.feet = a + c + 10; D.inches = b + c + 100 ; return D; } // 显示距离的方法 void displayDistance() { cout << "F: " << feet << " I:" << inches << endl; } }; int main() { Distance D1(11, 10), D2; cout << "First Distance : ";...

C++ 类构造函数 & 析构函数-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 类构造函数 & 析构函数

C++ 类构造函数 & 析构函数 C++ 类 & 对象 类的构造函数 类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。 构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。构造函数可用于为某些成员变量设置初始值。 下面的实例有助于更好地理解构造函数的概念: #include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); // 这是构造函数 private: double length; }; // 成员函数定义,包括构造函数 Line::Line(void) { cout << "Object is being created" << 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; } 当上面的代码被编译和执行时,它会产生下列结果: Object is being created Length of line : 6 带参数的构造函数 默认的构造函数没有任何参数,但如果需要,构造函数也可以带有参数。这样在创建对象时就会给对象赋初始值,如下面的例子所示: #include <iostream> using namespace std; class Line { public: void setLength( double len ); double...

C++ 类访问修饰符-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 类访问修饰符

C++ 类访问修饰符 C++ 类 & 对象 数据隐藏是面向对象编程的一个重要特点,它防止函数直接访问类类型的内部成员。类成员的访问限制是通过在类主体内部对各个区域标记 public、private、protected 来指定的。关键字 public、private、protected 称为访问说明符。 一个类可以有多个 public、protected 或 private 标记区域。每个标记区域在下一个标记区域开始之前或者在遇到类主体结束右括号之前都是有效的。成员和类的默认访问修饰符是 private。 class Base { public: // public members go here protected: // protected members go here private: // private members go here }; 公有(public)成员 公有成员在程序中类的外部是可访问的。您可以不使用任何成员函数来设置和获取公有变量的值,如下所示: #include <iostream> using namespace std; class Line { public: double length; void setLength( double len ); double getLength( void ); }; // 成员函数定义 double Line::getLength(void) { return length ; } void Line::setLength( double len ) { length = len; } // 程序的主函数 int main( ) { Line line; // 设置长度 line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; // 不使用成员函数设置长度 line.length = 10.0; // OK: 因为 length 是公有的 cout << "Length of line : " <<...

C++ 关系运算符重载-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 关系运算符重载

C++ 关系运算符重载 C++ 重载运算符和重载函数 C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等),它们可用于比较 C++ 内置的数据类型。 您可以重载任何一个关系运算符,重载后的关系运算符可用于比较类的对象。 下面的实例演示了如何重载 < 运算符,类似地,您也可以尝试重载其他的关系运算符。 #include <iostream> using namespace std; class Distance { private: int feet; // 0 到无穷 int inches; // 0 到 12 public: // 所需的构造函数 Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } // 显示距离的方法 void displayDistance() { cout << "F: " << feet << " I:" << inches <<endl; } // 重载负运算符( - ) Distance operator- () { feet = -feet; inches = -inches; return Distance(feet, inches); } // 重载小于运算符( < ) bool operator <(const Distance& d) { if(feet < d.feet) { return true; } if(feet == d.feet...

C++ 把引用作为参数-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 把引用作为参数

C++ 把引用作为参数 C++ 引用 我们已经讨论了如何使用指针来实现引用调用函数。下面的实例使用了引用来实现引用调用函数。 #include <iostream> using namespace std; // 函数声明 void swap(int& x, int& y); int main () { // 局部变量声明 int a = 100; int b = 200; cout << "交换前,a 的值:" << a << endl; cout << "交换前,b 的值:" << b << endl; /* 调用函数来交换值 */ swap(a, b); cout << "交换后,a 的值:" << a << endl; cout << "交换前,b 的值:" << b << endl; return 0; } // 函数定义 void swap(int& x, int& y) { int temp; temp = x; /* 保存地址 x 的值 */ x = y; /* 把 y 赋值给 x */ y = temp; /* 把 x 赋值给 y */ return; } 当上面的代码被编译和执行时,它会产生下列结果: 交换前,a 的值: 100 交换前,b 的值: 200 交换后,a 的值: 200 交换后,b...

C++ 类成员函数-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 类成员函数

C++ 类成员函数 C++ 类 & 对象 类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样。类成员函数是类的一个成员,它可以操作类的任意对象,可以访问对象中的所有成员。 让我们看看之前定义的类 Box,现在我们要使用成员函数来访问类的成员,而不是直接访问这些类的成员: class Box { public: double length; // 长度 double breadth; // 宽度 double height; // 高度 double getVolume(void);// 返回体积 }; 成员函数可以定义在类定义内部,或者单独使用范围解析运算符 :: 来定义。在类定义中定义的成员函数把函数声明为内联的,即便没有使用 inline 标识符。所以您可以按照如下方式定义 Volume() 函数: class Box { public: double length; // 长度 double breadth; // 宽度 double height; // 高度 double getVolume(void) { return length * breadth * height; } }; 您也可以在类的外部使用范围解析运算符 :: 定义该函数,如下所示: double Box::getVolume(void) { return length * breadth * height; } 在这里,需要强调一点,在 :: 运算符之前必须使用类名。调用成员函数是在对象上使用点运算符(.),这样它就能操作与该对象相关的数据,如下所示: Box myBox; // 创建一个对象 myBox.getVolume(); // 调用该对象的成员函数 让我们使用上面提到的概念来设置和获取类中不同的成员的值: #include <iostream> using namespace std; class Box { public: double length; // 长度 double breadth; // 宽度 double height; // 高度 // 成员函数声明 double getVolume(void); void setLength( double len ); void setBreadth( double...