C++教程 第12页

C++ break和continue-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ break和continue

学习C++ – C++ break和continue break和continue语句使程序可以跳过部分代码。 您可以在switch语句和任何循环中使用break语句。 它导致程序执行到切换或循环后的下一条语句。 continue语句用于循环,并导致程序跳过其余部分循环,然后开始一个新的循环。 例子 以下代码显示了两个语句的工作原理。 #include <iostream> using namespace std; const int my_size = 80; int main(){ char line[my_size]; int spaces = 0; cout << "Enter a line of text:\n"; cin.get(line, my_size); cout << "Complete line:\n" << line << endl; for (int i = 0; line[i] != "\0"; i++) { cout << line[i]; // display character if (line[i] == ".") // quit if it"s a period break; if (line[i] != " ") // skip rest of loop continue; spaces++; } cout << "\n" << spaces << " spaces\n"; return 0; } 上面的代码生成以下结果。

C++ 引用变量-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 引用变量

学习C++ – C++引用变量 C++引用是作为先前定义的变量的替代名称的名称。 例如,如果您使Bob成为Robert变量的引用,则可以互换使用Bob和Robert。 引用变量的主要用途是作为函数的形式参数。 如果使用引用作为参数,则该函数与原始数据而不是副本一起使用。 引用提供了一个方便的替代方法,用于处理具有函数的大型结构的指针。 创建引用变量 C和C++使用&符号来表示变量的地址。 C++使用&符号来声明引用。 例如,要使罗伯特成为变量的替代名称,您可以执行以下操作: int bob; int & robert = bob; // makes robert an alias for bob 在这种情况下,&不是地址运算符。 相反,它作为类型标识符的一部分。 int& 表示引用到内部。 参考声明允许您互换使用bob和robert。 两者都是指相同的值和相同的内存位置。 #include <iostream> using namespace std; int main(){ int bob = 101; int & robert = bob; // robert is a reference cout << "bob = " << bob; cout << ", robert = " << robert << endl; robert++; cout << "bob = " << bob; cout << ", robert = " << robert << endl; cout << "bob address = " << &bob; cout << ", robert address = " << &robert << endl; return 0; } 上面的代码生成以下结果。 注意 & 在下面的代码中声明一个引用类型变量。 int & robert = bob; &运算符在下一个语句中是地址运算符:...

C++ 文件-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 文件

学习C++ – C++文件 写入文本文件 以下代码从用户收集信息,将输出发送到显示器,然后将相同的输出发送到文件。 #include <iostream> #include <fstream> // for file I/O using namespace std; int main() { char automobile[50]; int year; double a_price; ofstream outFile; // create object for output outFile.open("test.txt"); // associate with a file cout << "Enter the make and model: "; cin.getline(automobile, 50); cout << "Enter the model year: "; cin >> year; cout << "Enter the price: "; cin >> a_price; // display information cout << fixed; cout.precision(2); cout.setf(ios_base::showpoint); cout << "Model: " << automobile << endl; cout << "Year: " << year << endl; cout << "$" << a_price << endl; //File outFile << fixed; outFile.precision(2); outFile.setf(ios_base::showpoint); outFile << "Model: " << automobile << endl; outFile << "Year: " << year << endl;...

C++ 控制台-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 控制台

学习C++ – C++控制台 读取字符到文件结尾 #include <iostream> int main() { using namespace std; char ch; int count = 0; cin.get(ch); // attempt to read a char while (cin.fail() == false) // test for EOF { cout << ch; // echo character ++count; cin.get(ch); // attempt to read another char } cout << endl << count << " characters read\n"; return 0; } 上面的代码生成以下结果。 使用cin.get()读取字符 #include <iostream> int main(void) { using namespace std; int ch; // should be int, not char int count = 0; while ((ch = cin.get()) != EOF) // test for end-of-file { cout.put(char(ch)); ++count; } cout << endl << count << " characters read\n"; return 0; } 上面的代码生成以下结果。

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

C++ 类

学习C++ – C++类 C++关键字类将代码标识为定义类的设计。 语法将Product标识为此类的类型名称。 设计类的第一步是提供类声明。 语法 类声明在声明之后被编码,并且可以包括数据成员和函数成员。 声明有一个私有部分,在该部分中声明的成员只能通过成员函数访问。 声明还有一个公共部分,声明的成员可以使用类对象直接由程序访问。 通常,数据成员进入私有部分,成员函数进入公共部分。 典型的类声明有这种形式。 class className { private: data member declarations public: member function prototypes }; 公共部分的内容构成了设计的抽象部分,公共接口。 在私有部分中封装数据保护数据的完整性,并称为数据隐藏。 类设计的第二步是实现类成员函数。 以下代码显示了如何使用成员函数定义类。 #include <iostream> using namespace std; class Printer { public: // function that displays a welcome message to the Printer user void displayMessage() { cout << "Welcome to the Grade Book!" << endl; } }; int main() { Printer myPrinter; // create a Printer object named myPrinter myPrinter.displayMessage(); // call object"s displayMessage function } 上面的代码生成以下结果。 带参数的成员函数 以下代码显示了如何使用具有参数的成员函数定义类打印机,创建一个Printer对象并调用其displayMessage函数。 #include <iostream> #include <string> using namespace std; class Printer { public: void displayMessage( string courseName ) { cout << "Welcome to the grade book for\n" << courseName << "!" << endl; } }; // function main...

C++ 自定义类型

学习C++ – C++自定义类型 定义您自己的数据类型 您可以通过定义一个类来定义新的数据类型。 类类型可以是其他类型的基本类型或其他类类型的变量的组合。 类也可以具有作为其定义的组成部分的功能。 您可以定义一个类型为Box,它包含存储长度,宽度和高度来表示框的变量。 然后,您可以定义Box类型的变量,就像定义基本类型的变量一样。 每个Box对象将包含自己的长度,宽度和高度尺寸,您可以根据需要在程序中创建和操作尽可能多的Box对象。 类是用户定义的数据类型。 在类中定义的变量和函数是类的成员。 变量是数据成员和函数是函数成员。 类的函数成员是有时称为方法。 类型的变量存储对象。对象有时被称为类的实例。 定义类的实例被称为实例化。 面向对象 面向对象编程包含了一些其他重要的想法(着名的封装和数据隐藏,继承和多态)。 继承是根据另一种定义一种类型的能力。 多态性是指在不同时间采取不同形式的能力。 C++中的多态性总是涉及使用指针或引用来调用对象的函数成员。 定义类 类是用户定义的类型。 类型的定义使用class关键字。类定义的基本组织如下所示: class ClassName { // Code that defines the members of the class... }; 此类类型的名称为ClassName。 使用用户定义类的大写名称来区分类型和变量名是一个常见的约定。 类的成员都在大括号之间指定。 函数成员的定义可以在类定义的内部或外部。 class Box { private: double length {1.0}; double width {1.0}; double height {1.0}; public: // Function to calculate the volume of a box double volume() { return length*width*height; } }; length,width和height是Box类的数据成员,都是double类型。 每个Box对象都有自己的数据成员集。 这是很明显的 – 如果他们没有自己的数据成员,所有的对象都是一样的。 您可以像这样创建一个类型为Box的变量: Box myBox; // A Box object with all dimensions 1 myBox变量指的是具有默认数据成员值的Box对象。您可以调用该对象的volume()成员来计算卷: std::cout << "Volume of myBox is" << myBox.volume() << std::endl; // Volume is 1.0 您可以将数据成员指定为public,在这种情况下,您可以从类外部显式设置它们,如下所示: myBox.length = 1.5; myBox.width = 2.0; myBox.height = 4.0; std::cout << "Volume of myBox is" << myBox.volume()...

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

C++ 构造函数

学习C++ – C++构造函数 类构造函数是类中的一种特殊类型的函数。 当定义类的新实例时调用构造函数。 它在创建新对象时初始化,并确保数据成员包含有效值。 类构造函数与类具有相同的名称。 Box(),例如是Box类的构造函数。 构造函数不返回值,因此没有返回类型。 如果您没有为类定义构造函数,编译器将提供默认构造函数。 用new对象初始化 一般来说,如果Class_name是一个类,如果value的类型为Type_name,则该语句 Class_name * pclass = new Class_name (value); 调用此构造函数: Class_name (Type_name); 可能有微小的转换,例如: Class_name(const Type_name &); 例子 实例化MyBook类的多个对象,并使用MyBook构造函数指定每个MyBook对象创建时的课程名称。 #include <iostream> #include <string> using namespace std; class MyBook { public: // constructor initializes courseName with string supplied as argument MyBook( string name ) { setCourseName( name ); // call set function to initialize courseName } // function to set the course name void setCourseName( string name ) { courseName = name; // store the course name in the object } // function to get the course name string getCourseName() { return courseName; // return object"s courseName } // display a welcome message to the MyBook user void displayMessage() { // call...

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

C++ 运算符重载

学习C++ – C++运算符重载 要使运算符过载,您使用一个称为运算符函数的特殊函数。 运算符函数具有以下形式,其中op是运算符被重载的符号: operator op (argument-list) 例如,运算符+()重载+运算符,运算符*()重载*运算符。 op必须是有效的C++运算符。 下表列出了可以重载的运算符。 + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- , ->* -> () [] new delete new [] delete [] 添加加法运算符 以下代码将Time类转换为使用重载的加法运算符。 #include <iostream> class Time { private: int hours; int minutes; public: Time(); Time(int h, int m = 0); void AddMin(int m); void AddHr(int h); void Reset(int h = 0, int m = 0); Time operator+(const Time & t) const; void Show() const; }; Time::Time() { hours = minutes = 0; } Time::Time(int h, int m ) { hours = h; minutes = m;...

C++ 逻辑运算符-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

C++ 逻辑运算符

学习C++ – C++逻辑运算符 运算符是逻辑或,写成||;逻辑与,写&&和逻辑非,写成!。 逻辑或运算符:|| OR可以指示两种条件之一或两者满足要求。 逻辑或运算符,写入||,将两个表达式组合成一个。 如果一个或两个原件表达式为true或非零,则生成的表达式的值为true。 否则,表达式的值为false。 这里有些例子: 5 == 5 || 5 == 9 // true because first expression is true 5 > 3 || 5 > 10 // true because first expression is true 5 > 8 || 5 < 10 // true because second expression is true 5 < 8 || 5 > 2 // true because both expressions are true 5 > 8 || 5 < 2 // false because both expressions are false 因为||具有比关系运算符低的优先级,您不需要在这些表达式中使用括号。 下表总结了||运算符的工作原理。 expr1的值|| expr2 expr1 == true expr1 == false expr2 == true true true expr2 == false true false C ++||运算符支持快捷方式评估。 例如,考虑以下表达式: i++ < 6 || i == j 假设 i 最初的值为10。 如果表达式i ++<6是真的,C++将不会麻烦评估表达式 i== j,因为它只需要一个真实的表达式来使整个逻辑表达式为真。 例子 以下代码在if语句中使用||运算符来检查字符的大写和小写版本。 #include...

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

C++ 关系运算符

学习C++ – C++关系运算符 C++提供了六个关系运算符来比较数字。 运算符 含义 < 小于 <= 小于或等于 == 等于 > 大于 >= 大于或等于 != 不等于 例子 字符串类字符串的比较 #include <iostream> #include <string> // string class int main() { using namespace std; string word = "?ate"; for (char ch = "a"; word != "mate"; ch++) { cout << word << endl; word[0] = ch; } cout << "After loop ends, word is " << word << endl; // cin.get(); return 0; } 上面的代码生成以下结果。 例2 使用if语句,关系运算符和等式运算符比较整数。 #include <iostream> // allows program to perform input and output using std::cout; // program uses cout using std::cin; // program uses cin using std::endl; // program uses endl int main() { int number1; // first integer to compare int number2; // second integer to...