0713-----C++Primer听课笔记----------类和对象

1.要求:将一篇文章录入,然后实现查询:

a)查询可以输入字符或者字符串

b)将包含它们的单词输出

c)允许重复

d)如果输入查询有空格,当多个处理

Ex: 输入ro,应该打印出“microsift”

输入”he wr”,则查询两次

1.1  不用类实现

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>

using namespace std;
/*
 *第一个版本  录入一篇文章 查找字符串
 *
 */
ifstream &open_file(ifstream &is, const string &filename){
    is.close();
    is.clear();
    is.open(filename.c_str());
    return is;
}

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, const string &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, const char *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);
    }
    return 0;
}

1.2用结构体实现

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>

using namespace std;
/*
 *第二个版本  用结构体 录入一篇文章 查找字符串
 */

ifstream &open_file(ifstream &is, const string &filename){
    is.close();
    is.clear();
    is.open(filename.c_str());
    return is;
}

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(const string &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, const char *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);
    }
    return 0;
}

1.3用类现实

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>

using namespace std;

class Findstr{
    public:
        void read_file(const string &filename);
        vector<string> find_string(const string &str);

    private:
        vector<string> words_;
};

void Findstr::read_file(const string &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(const string &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, const char *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;
        }
    }
    return 0;
}

2.类中的public和private属性

2.1private意味着该成员只能在类的内部被访问,而public可以再任意位置被访问。

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Student{
    public:
        void set_name(const string &name){
            name_ = name;
        }
        const string &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, const char *argv[])
{
    Student s1;
    s1.set_name("zhangsan");
    s1.set_age(12);
    s1.set_score(90);
    s1.print(cout);

    return 0;
}

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>

using namespace std;

class Test{
    public:
        void set(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, const char *argv[])
{
    Test t;
    t.set(10, 9);
    t.print();
    return 0;
}

3.2 class的成员函数含有一个隐含的参数。例如:s.print(), print()函数有一个隐式参数,就是s的指针。

原文地址:https://www.cnblogs.com/monicalee/p/3841664.html