摘要: /*//5 包含对系统的消耗//由于book类的数据成员中包含量了3个String类的对像,因此在创建一个book类对像时也就不可避免的创建3个String类的对像,本节我们通过在String类和book类的构造和析构函数中添车输出语句演示这种消耗#include "String.h"class Book{public: Book(); ~Book(){ cout<<"Book类的析构函数执行...."<<endl;} Book(char*, char*, char*, float); //不能修改返回值,在函数内也不能修改,也不 阅读全文
posted @ 2012-09-24 23:59 简单--生活 阅读(191) 评论(0) 推荐(0) 编辑
摘要: // 6按别名传递book对像//由于在创建一个book类对像时会创建3个String类对像,因此,假如我们按值传递一个book类对像给某个函数,那么对系统的消耗是相当可观的,接下来的例子将演示按值与按别名传递book类对像的不同#include "String.h"class Book{public: Book(); ~Book(){ cout<<"Book类的析构函数执行...."<<endl;} Book(char*, char*, char*, float); //不能修改返回值,在函数内也不能修改,也不想调用复制构造函数 阅读全文
posted @ 2012-09-24 23:59 简单--生活 阅读(169) 评论(0) 推荐(0) 编辑
摘要: // 4 为图书重载GetAuthor函数//本节我们再创建一本图书,这本书是由两个合写的,为了记录这两个人的姓名,我们需要定义一个字符串#include "String.h"class Book{public: Book(); Book(char*, char*, char*, float); //不能修改返回值,在函数内也不能修改,也不想调用复制构造函数,按地址传递 const String& GetTitle()const{ return title; } const String& GetAuthor()const{ return author; } 阅读全文
posted @ 2012-09-24 23:40 简单--生活 阅读(339) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;class String{public: String(); //默认的构造函数 ~String(){ delete []str; len=0; //cout<<"析构函数执行"<<endl; } String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 //const char *getstr()const{ return str;} //读取字符串 / 阅读全文
posted @ 2012-09-24 23:39 简单--生活 阅读(190) 评论(0) 推荐(0) 编辑
摘要: // 38总结#include <iostream>using namespace std;class String{public: String(); //默认的构造函数 ~String(){ delete []str; len=0; cout<<"析构函数执行"<<endl; } String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 //const char *getstr()const{ return str;} //读取 阅读全文
posted @ 2012-09-24 22:37 简单--生活 阅读(234) 评论(0) 推荐(0) 编辑
摘要: // 35用重载比较运算符实现字符串的比较/*#include <iostream>using namespace std;class String{public: String(); //默认的构造函数 String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 //const char *getstr()const{ return str;} //读取字符串 //重载输出函数 friend ostream&operator<<(ostream & 阅读全文
posted @ 2012-09-24 22:36 简单--生活 阅读(1560) 评论(0) 推荐(0) 编辑
摘要: // 36为string类添加字符串的相加功能/*#include <iostream>using namespace std;class String{public: String(); //默认的构造函数 ~String(){ delete []str; len=0; cout<<"析构函数执行"<<endl; } String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 //const char *getstr()const{ 阅读全文
posted @ 2012-09-24 22:36 简单--生活 阅读(1162) 评论(0) 推荐(0) 编辑
摘要: // 33用重载输出运算符函数实现字符串的输出/*#include <iostream>using namespace std;class String{public: String(); //默认的构造函数 String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 //const char *getstr()const{ return str;} //读取字符串 //重载输出函数 friend ostream&operator<<(ostream &am 阅读全文
posted @ 2012-09-24 22:35 简单--生活 阅读(684) 评论(0) 推荐(0) 编辑
摘要: // 34用重载输入运算符函数实现字符串的输入/*#include <iostream>using namespace std;class String{public: String(); //默认的构造函数 String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 //const char *getstr()const{ return str;} //读取字符串 //重载输出函数 friend ostream&operator<<(ostream &am 阅读全文
posted @ 2012-09-24 22:35 简单--生活 阅读(758) 评论(0) 推荐(0) 编辑
摘要: // 30限制数组越界// 该函数有一个参数即数组的下载length, 下标值length会传递到函数体中进行判断,假如下标超过数组的长度,那么返回数组长度-1,也就是最后一个可见字符,假如不超过数组长度,那么返回该下标的数组元素/*#include <iostream>using namespace std;class String{public: String(); //默认的构造函数 String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 const char *ge 阅读全文
posted @ 2012-09-24 22:32 简单--生活 阅读(233) 评论(0) 推荐(0) 编辑
摘要: //31用复制构造函数实现字符串的初始化/*#include <iostream>using namespace std;class String{public: String(); //默认的构造函数 String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 const char *getstr()const{ return str;} //读取字符串 //这里是可以修改的 char &operator[](unsigned short int length); c 阅读全文
posted @ 2012-09-24 22:32 简单--生活 阅读(1278) 评论(0) 推荐(0) 编辑
摘要: // 32用重载赋值运算符函数实现字符串赋值功能/*#include <iostream>using namespace std;class String{public: String(); //默认的构造函数 String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 const char *getstr()const{ return str;} //读取字符串 //这里是可以修改的 char &operator[](unsigned short int length 阅读全文
posted @ 2012-09-24 22:32 简单--生活 阅读(1019) 评论(0) 推荐(0) 编辑
摘要: // 27 编辑写精减版的string类/*实现功能如下: 1: 可计算字符串的长度,类似于string类的size或者length 2: 可对字符串进行赋值,如string str="hell word"; 3: 可将char型字符串直接赋值给string型字符串,如 string str="hello word"; char ch[10] = "not at all"; 4: 可象对像那样将字符串进行初始化,如:string str("Month"); 5: 可将两个字符相加,然后将合并后的字符串赋给另一个字符 阅读全文
posted @ 2012-09-24 22:31 简单--生活 阅读(162) 评论(0) 推荐(0) 编辑
摘要: // 28创建string类/*#include <iostream>using namespace std;class String{public: String(); int getlen(){ return len;}private: int len; char *str;};//创建一个空的str变量String::String(){ len = 0; str = new char[1]; str[0] = '\0';};int main(){ String str; cout<<str.getlen()<<endl; return 阅读全文
posted @ 2012-09-24 22:31 简单--生活 阅读(137) 评论(0) 推荐(0) 编辑
摘要: //29创建可自动调节大小的string类字符串对象//上一节实现了一个空字符串的创建,本节将在这个基础上创建一个有字符的字符串,并且该字符串可以根据字符的长度自动调节数组的大小/*#include <iostream>using namespace std;class String{public: String(); //默认的构造函数 String(const char*const ch); //构造带值的string int getlen()const { return len;} //读取长度 const char *getstr()const{ return str;} 阅读全文
posted @ 2012-09-24 22:31 简单--生活 阅读(204) 评论(0) 推荐(1) 编辑
摘要: //26 前置跟后置自加/*#include <iostream>using namespace std;class A{public: A(int n){ rx=n;} friend ostream&operator<<(ostream&s, const A&c){ s<<c.rx<<endl; return s; } friend istream&operator>>(istream&s, A&c) { s>>c.rx; return s; } //前置运算符 int o 阅读全文
posted @ 2012-09-24 22:30 简单--生活 阅读(180) 评论(0) 推荐(0) 编辑
摘要: // 25重载自加运算符的执行次序/*#include <iostream>using namespace std;class A{public: A(int n){ rx=n;} friend ostream&operator<<(ostream&s, const A&c){ s<<c.rx<<endl; return s; } //前置运算符 int operator++(){ cout<<"++i"<<endl; rx++; return rx; } //后置自加 int 阅读全文
posted @ 2012-09-24 22:29 简单--生活 阅读(251) 评论(0) 推荐(0) 编辑
摘要: // 23重载输出运算符/*#include <iostream>using namespace std;class A{public: A(int x, int y){ rx = x; ry = y; }//private: int rx; int ry;};ostream&operator<<(ostream&s, const A&c){ s<<c.rx<<" "; s<<c.ry<<endl; return s;};int main(){ A a(3,4), b(5,6); 阅读全文
posted @ 2012-09-24 22:27 简单--生活 阅读(185) 评论(0) 推荐(0) 编辑
摘要: // 24友无的方式重载输出运算符//本节的开头首先讲一下运算符的知道,<<是按位左移运算符,但是当该符号与流对象,比如说cout连用地就变成了输出运算符,或者叫提取运算符//注意它们的区别,接位左移动算符是用来执行位移操作的,比如说//int num=1;//num<<1;//按位左移运算符(<<)将运算符左边的运算对像num向左移动运算符右侧指定的位数1,然后在低部补0//了解了它们的区另以后,接下来有个问题,按位左移运算符和输出运算符这两个符号是一样的,编辑器是如何来区分它们的,为了解释这个问题,我们需要返回到上一节的代码中/*#include < 阅读全文
posted @ 2012-09-24 22:27 简单--生活 阅读(440) 评论(0) 推荐(0) 编辑
摘要: // 21string数组与函数/*#include <iostream>#include <string>//假如要传递多个字符串,那么可以声明一个string对像数组,然后将数组传递到函数中using namespace std;void show(const string str[], int l);int main(){ const int l = 5; string st[l]; for(int i=0; i<l; i++) { cout<<i<<"请输入:"<<endl; cin>>s 阅读全文
posted @ 2012-09-24 22:26 简单--生活 阅读(221) 评论(0) 推荐(0) 编辑
简单--生活(CSDN)