我们想 像使用基本数据类型一样使用类,自然,类自然也有指针,我们通过下面的代码来领教一下对象指针存在的意义:
1 # include "iostream" 2 # include "string" 3 # include "ctime" 4 # include "cstdlib" 5 const int Arsize = 10; 6 const int MaxLen = 81; 7 int main() 8 { 9 using namespace std; 10 string name; 11 cout << "Hi,what's your name? >>"; 12 cin >> name; 13 14 cout << name << ",please enter up to " << Arsize 15 << "short saying<empty line to quit>: "; 16 string sayings[Arsize];//说明这里出现了什么问题 17 char temp[MaxLen]; 18 int i; 19 for (i = 0; i < Arsize; i++) 20 { 21 cout << i + 1 << ":"; 22 cin.get(temp, MaxLen);//cin.get表示的是什么意思 23 while (cin&& cin.get() != ' ') //注意这里的判别用法 24 continue; 25 if (!cin || temp[0] == ' ') //说明直接输入“enter”按键则程序会退出 26 break; 27 else 28 sayings[i] = temp; 29 } 30 int total = i; 31 32 if (total > 0) 33 { 34 cout << "Here are your saying: "; 35 for (i = 0; i < total; i++) 36 cout << sayings[i] << " "; 37 38 string * shortest = &sayings[0];//定义了指针并初始化 39 string * first = &sayings[0];//定义了指针并初始化 40 for (i = 1; i < total; i++) 41 { 42 if (sayings[i].length() < shortest->length()) 43 shortest = &sayings[i]; 44 if (sayings[i] < *first) 45 first = &sayings[i]; 46 } 47 cout << "shortest saying : " << *shortest << endl; 48 cout << "first alphabetically : " << *shortest << endl; 49 srand(time(0)); 50 int choice = rand() % total; 51 string * favorite = new string(sayings[choice]);//使用new定义了指针 52 cout << "my favorite saying: " << *favorite << endl; 53 delete favorite; 54 } 55 else 56 cout << "Not much to say,eh? "; 57 cout << "Bye. "; 58 system("pause"); 59 return 0; 60 }
首先注意第16行的代码:使用string类定义了字符串数组,注意我们使用string 定义字符串而不是用c风格的char p[]来定义字符串有诸多的好处:因为string类中既包含了科学的内存管理,同重载了+ ,==,!=,>...这些运算符,来方便我们像使用一般变量一样使用类对象。我们需要形成这样一种意识:即定义的类应该具有重载基本运算符的功能,同时我们即使不知道string能够重载这些,也要站在类的角度,认识到运算符重载对于一般类属性的意义。
cin.get(字符数組名,接收字符数目)用來接收一行字符串,可以接收空格,如下图:
1 #include <iostream> 2 using namespace std; 3 main () 4 { 5 char a[20]; 6 cin.get(a,20); 7 cout<<a<<endl; 8 } 9 10 输入:jkl jkl jkl 11 输出:jkl jkl jkl 12 13 输入:abcdeabcdeabcdeabcdeabcde (输入25个字符) 14 输出:abcdeabcdeabcdeabcd (接收19个字符+1个'