Essential c++第四章笔记

①在class中定义成员函数会被自动视为inline函数

<练习>
4.1
main.cpp

#include "Stack.h"
int main(void)
{
    Stack cc;
    cc.push("changshi");
    cc.size();
    cc.pop();
    cc.peek();
    return 0;
}

Stack.h

#ifndef STACK_H
#define STACK_H
#include <vector>
#include <iostream>
#include <string>
using namespace std;
class Stack
{
public:
    bool push(const string &);
    bool pop(string &);
    bool peek(string &);
    bool empty(void);
    bool full(void);
    int size(void) {return _stack.size();}
private:
    vector<string> _stack;
};
#endif

Stack.cpp

#include "Stack.h"
bool Stack::push(const string & str)
{
    if(full())
        return false;
    _stack.push_back(str);
    return true;
}
bool Stack::pop(string & elem)
{
    if(empty())
        return false;
    elem = _stack.back();
    _stack.pop_back();
    return true;
}
bool Stack::peek(string & elem)
{
    if(empty())
        return false;
    elem = _stack.back();
    return true;
}
bool Stack::empty(void)
{
    return _stack.empty();
}
bool Stack::full(void)
{
    return _stack.size() == _stack.max_size();
}

4.2

bool Stack::find(const string & str)
{
    if(empty())
        return false;
    for(int i = 0; i < _stack.size(); i++)
        if(str == _stack[i])
            return true;
    return false;
}
int Stack::count(const string & str)
{
    int count = 0;
    if(empty())
        return -1;
    for(int i = 0; i < _stack.size(); i++)
        if(str == _stack[i])
            count++
    return count;
}

②当我们以某个对象作为另一个对象的初值会默认成员逐一初始化操作

③成员函数可以根据const与否而重载

④如果想让类对象保持常量性,又想修改某个成员的数据,那么可以在此成员前加上关键字mutable,表示该数据可变但又不影响常量性

⑤静态数据成员实际上是类域中的全局变量,所以该成员定义不能放在头文件中(会引起重复定义,即使用上预处理指令也不行),静态数据成员被该类与该类中所有对象共享,包括派生类对象,静态数据成员可以成为成员函数的可选参数,普通成员不行,静态数据成员可以是所属的类的类型,而普通数据成员只能是所属类的指针或引用,可在const函数中合法被改变

⑥静态成员函数地址可用普通函数指针储存,普通成员函数地址需要用类成员函数指针储存.静态成员函数不可调用非静态成员,因为类中的静态函数不含this指针.静态成员函数不可同时声明为virtual,const,volatile函数

⑦在class主体外部进行静态成员函数定义时,不需要加上static(规则同样适用于静态成员数据)

⑧任何运算符如果和另一个运算符性质相反,通常以后者实现前者

⑨运算符重载规则
不可引入新的运算符,除了’.’, '.’, ‘::’, '?:'其他运算符皆可被重载
*运算符操作数目不可更改
*运算符优先级不可更改
*运算符函数参数中至少有一个为class类型

⑩可以让A类与B类建立友元关系,A类所有成员函数都成为B类的友元

①①静态成员函数可以以"类名::函数名"方式直接调用

①②指向类成员函数的指针的声明typedef 返回值(类名::*指针类型名)(参)

①③vector<vector >必须在后2个’>'加上空白

<练习>
4.3

#include <string>
class data
{
private:
	static string program_name;
	static string version_stamp;
	static int version_number;
	static int texts_run;
	static int tests_passed;
};

4.4
main.c

#include "Stack.h"
int main(void)
{
    UserProfile c;
    cout << c;
    cin >> c;
    cout << c;
    return 0;
}
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class UserProfile
{
public:
    UserProfile(string Ne = "guest", int Lt = 1, int Gt = 0, int Rt = 0, int Ll = 0, float Rr = 0.0f) : Name(Ne), LoginCount(Lt), GuessCount(Gt), RightCount(Rt), Level(Ll), RightPer(Rr){}
    friend istream & operator>>(istream &, UserProfile &);
    friend ostream & operator<<(ostream &, const UserProfile &);
    bool operator==(const UserProfile &);
    bool operator!=(const UserProfile &);
private:
    string Name;
    int LoginCount;
    int GuessCount;
    int RightCount;
    int Level;
    float RightPer;
};
#endif
#include "Stack.h"
istream & operator>>(istream & in, UserProfile & data)
{
    cout << "Name:";
    in >> data.Name;
    return in;
}
ostream & operator<<(ostream & out, const UserProfile & data)
{
    out << "Name:" << data.Name << endl
        << "LoginCount:" << data.LoginCount << endl
        << "GuessCount:" << data.GuessCount << endl
        << "RightCount:" << data.RightCount << endl
        << "Level:" << data.Level << endl
        << "RightPer" << data.RightPer << endl;
    return out;
}
bool UserProfile::operator==(const UserProfile & objec)
{
    return this == &objec;
}
bool UserProfile::operator!=(const UserProfile & objec)
{
    return !(this == &objec);
}

4.5

方法一样,偷个懒
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值