0713-----C++Primer听课笔记----------类和对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了0713-----C++Primer听课笔记----------类和对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5368字,纯文字阅读大概需要8分钟。
内容图文
![0713-----C++Primer听课笔记----------类和对象](/upload/InfoBanner/zyjiaocheng/1323/d511c00470ae4e97801d3ed4eeef34fc.jpg)
1.要求:将一篇文章录入,然后实现查询:
a)查询可以输入字符或者字符串
b)将包含它们的单词输出
c)允许重复
d)如果输入查询有空格,当多个处理
Ex: 输入ro,应该打印出“microsift”
输入”he wr”,则查询两次
1.1 不用类实现
#include <iostream> #include <string> #include <vector> #include <fstream> #include <stdexcept> usingnamespace std; /* *第一个版本 录入一篇文章 查找字符串 * */ ifstream &open_file(ifstream &is, conststring &filename){ is.close(); is.clear(); is.open(filename.c_str()); returnis; } void read_file(vector<string> &vec, string &filename){ ifstream is; string word; if(!open_file(is, filename)){ throw std::runtime_error("open_file_error"); } while(is >> word){ vec.push_back(word); } is.close(); is.clear(); } vector<string> find_str(vector<string>&vec, conststring &str){ vector<string> res; for(vector<string>::iterator it = vec.begin(); it != vec.end(); ++it){ string::size_type loc = it->find(str); if(loc != string::npos) res.push_back(*it); } return res; } void print(vector<string> &res){ for(vector<string>::iterator it = res.begin(); it != res.end(); ++it){ cout << *it << endl; } } int main(int argc, constchar *argv[]) { string filename = "a.txt"; vector<string> vec, res; string str; read_file(vec, filename); while(cin >> str){ res = find_str(vec, str); print(res); } return0; }
1.2用结构体实现
#include <iostream> #include <string> #include <vector> #include <fstream> #include <stdexcept> usingnamespace std; /* *第二个版本 用结构体 录入一篇文章 查找字符串 */ ifstream &open_file(ifstream &is, conststring &filename){ is.close(); is.clear(); is.open(filename.c_str()); returnis; } struct Find{ vector<string> vec; void read_file(string &filename){ ifstream is; string word; if(!open_file(is, filename)){ throw std::runtime_error("open_file_error"); } while(is >> word){ vec.push_back(word); } is.close(); is.clear(); } vector<string> find_str(conststring &str){ vector<string> res; for(vector<string>::iterator it = vec.begin(); it != vec.end(); ++it){ string::size_type loc = it->find(str); if(loc != string::npos) res.push_back(*it); } return res; } void print(vector<string> &res){ for(vector<string>::iterator it = res.begin(); it != res.end(); ++it){ cout << *it << endl; } } }; int main(int argc, constchar *argv[]) { string filename = "a.txt"; Find s; s.read_file(filename); string str; vector<string> res; while(cin >> str){ res = s.find_str(str); s.print(res); } return0; }
1.3用类现实
#include <iostream> #include <string> #include <vector> #include <fstream> #include <stdexcept> usingnamespace std; class Findstr{ public: void read_file(conststring &filename); vector<string> find_string(conststring &str); private: vector<string> words_; }; void Findstr::read_file(conststring &filename){ ifstream is; is.close(); is.clear(); is.open(filename.c_str()); if(!is){ throw std::runtime_error("open file failed"); } string str; while( is >> str){ words_.push_back(str); } is.close(); } vector<string> Findstr::find_string(conststring &str){ vector<string> result; for(vector<string>::iterator it = words_.begin(); it != words_.end(); ++it){ string::size_type loc = it->find(str); if(loc != string::npos){ result.push_back(*it); } } return result; } int main(int argc, constchar *argv[]) { Findstr s; string filename = "a.txt"; s.read_file(filename); string str; while(cin >> str){ vector<string> res = s.find_string(str); for(vector<string>::iterator it = res.begin(); it != res.end(); ++it){ cout << *it << endl; } } return0; }
2.类中的public和private属性
2.1private意味着该成员只能在类的内部被访问,而public可以再任意位置被访问。
#include <iostream> #include <string> #include <vector> usingnamespace std; class Student{ public: void set_name(conststring &name){ name_ = name; } conststring &get_name()const{ return name_; } void set_age(int age){ age_ = age; } int get_age() const{ return age_; } void set_score(int score){ score_ = score; } int get_socre() const{ return score_; } void print(ostream &os){ os << name_<< "" << score_<< "" << age_<< endl; } private: string name_; int age_; int score_; }; int main(int argc, constchar *argv[]) { Student s1; s1.set_name("zhangsan"); s1.set_age(12); s1.set_score(90); s1.print(cout); return0; }
2.2 const三种用法:
a)修饰形参:使得形参只读,不可被更改;
b)修饰返回值,禁止外界修改返回值;
c)修饰类的成员函数, 禁止在函数内修改本对象。
2.3 const是一种常量语义,或者保护语义, 而非const是一种修改语义,因此在写程序时要注意从语义角度分析。
2.4函数签名的构成要素:
a)类名
b)函数名
c)形参表
d)类的成员函数是否为const函数
2.5 查看目标文件的办法:nm –A *.o|grep ****
3.this指针
3.1this 指针是本对象的指针。例如,t.set_name(“lisi”)那么这里的本对象指的是t,this就是t的地址。如下例所示。
#include <iostream> #include <string> #include <vector> usingnamespace std; class Test{ public: voidset(int a, int b){ /* *这里若用cout << a; 那么根据就近原则, 这个a是参数列表中的a 而不是 *类成员中的a */this->a = a; this->b = b; } void print(){ cout << a << "" << b << endl; } private: int a; int b; }; int main(int argc, constchar *argv[]) { Test t; t.set(10, 9); t.print(); return0; }
3.2 class的成员函数含有一个隐含的参数。例如:s.print(), print()函数有一个隐式参数,就是s的指针。
原文:http://www.cnblogs.com/monicalee/p/3841664.html
内容总结
以上是互联网集市为您收集整理的0713-----C++Primer听课笔记----------类和对象全部内容,希望文章能够帮你解决0713-----C++Primer听课笔记----------类和对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。