天天看点

C++中类作用域、运算符重载、vector类详解类作用域运算符重载对象数组vector类

类作用域

  • 类的成员函数/成员变量具有类作用域
  • 可以在以下场合使用类的成员:

    类的成员函数中

    类对象通过.调用类成员函数/或访问成员变量

    类对象指针通过->调用类成员函数/或访问成员变量

    通过域作用符::使用

  • 例如:
class A{
private:
    int y;
public:
    int f(int v) {
        x += v;
        y *= v;
    }
public:
    int x;
    static int kappa;
};
int A::kappa;

int main(){
    A a;
    int r = a.f(2);
    cout<<a.x<<endl;
    A *pa = new A();
    pa->f(3);
    A::kappa = 7;
    delete pa;
    return 0;
}
           

运算符重载

  • 例:定义一个整数类Integer,可以实现如下运算

    Integer x1(1);//𝑥1=1

    Integer x2(2);//𝑥2=2

    Integer u1 = x1 + x2;//重载运算符operator+

    Integer u2 = 3 + x1;//重载运算符operator+

    Integer v = x1 – x2;//重载运算符operator-

    Integer w = -v; //重载运算符operator-

    bool b = x1>x2; //重载运算符operator>

    cout<<x1<<x2; //重载运算符operator<<

    Integer x;

    cin>>x; //重载运算符operator>>

Integer x(1),y(2);

赋值运算符operator=:

x = y;

x.operator=(y);

Integer u1 = x1 + x2;

x1.opeator+(x2)

定义为成员函数:

Integer Integer::operator+(const Integer &r) { 
    return Integer(value + r.value); 
}

Integer u2 = 3 + x1;->3.operator+(x1)?

Integer Integer::operator+(const Integer &r) { 
    return Integer(value + r.value); 
}
           

定义为友元函数:

friend Integer operator+(const Integer& x1, const Integer &x2);
Integer operator+(const Integer& x1, const Integer &x2) {
    return Integer(x1.value + x2.value);
}
Integer u2 = 3 + x1;->Integer u2 = operator+(3,x1)?
Integer w = -v;->w = v.operator-();
====>
Integer operator-() { return Integer(-value); } 

bool b = x1>x2;->bool b = operator>(x1,x2);
====>
friend bool operator>(const Integer &x1, const Integer &x2);
bool operator>(const Integer &x1, const Integer &x2) {
    return x1.value > x2.value;
}

cout<<x1<<x2;->cout.operator<<(x1).opeator<<(x2);
====>
friend ostream& operator<<(ostream &os, const Integer &x);
ostream& operator<<(ostream &os, const Integer &x) {
    os << x.value;
    return os;
}
cin>>x; ->cin.operator>>(x);
====>
friend istream& operator>>(istream &is, Integer &x);
istream& operator>>(istream &is, Integer &x) {
    is >> x.value;
    return is;
}

           
  • 练习:改写表示有理数的类Rational,重载合适的运算符,使得下面的代码可以运行:

    Rational x1(1,2),x2(2,3);//𝑥1=1/2,𝑥2=2/3

    Rational u = x1 + x2;

    Rational v = x1 – x2;

    Rational w = x1 + 1;

    Rational z = 1 – x2;

    Rational r = x1 * x2;

    Rational s = x1 / x2;

    Rational t = x1 ++;

    bool b = x1>x2;

    x2-=1;

    cin>>u;

    cout<<-v;

对象数组

class Point {
private:
    int x, y;
public:
    Point() :x(0), y(0) { 
        cout << "Point()" << endl; }
    Point(int _x, int _y) :x(_x), y(_y) { 
        cout << "Point(int,int)" << endl; }
    ~Point() { cout << "~Point()" << endl; }
    int getX() const { return x; }
    int getY() const { return y; }
    void print() const { 
        cout << '(' << x << ',' << y << ')' << endl; 
    }
};
int main(void) {
    Point pts0[3];
    for (int i = 0; i < 3; ++i) pts0[i].print();
    Point pts1[] = { Point(),Point(1,1),Point(1,2) };
    for (int i = 0; i < 3; ++i) pts1[i].print();

    return 0;
}
           
C++中类作用域、运算符重载、vector类详解类作用域运算符重载对象数组vector类

vector类

#include <vector>
using namespace std;

//构造vector对象:
vector<elementType> vecName;

vector<int> iVec;//构造了一个对象iVec,元素数目为0
vector<double> dVec(5);//构造了一个对象dVec,元素数目为5
vector<char> cVec(10,'a'); //构造了一个对象cVec,包含10个字符'a'

//获取vector中的元素数目:
iVec.size();//0
dVec.size();//5

//访问vector中的元素:
dVec[0] = 1.0;
for(int i = 0;i<dVec.size();++i) cout<<dVec[i]<<endl;
cout<<dVec.at(1)<<endl;//0

//向vector中添加元素:
iVec.push_back(1);//iVec=[1]
iVec.push_back(2);//iVec=[1,2]
iVec.push_front(3);//iVec=[3,1,2]
iVec.push_front(4);//iVec=[4,3,1,2]

//删除vector中最后一个元素:
iVec.pop_back();iVec=[4,3,1]

//删除vector中的所有元素:
iVec.clear();

//判断vector是否为空:
iVec.empty();//返回true

//vector赋值:
vector<int> v1(5,1),v2(10,2);
v2 = v1;//v2=[1,1,1,1,1]
v1.assign(3,-1);//v1=[-1,-1,-1]

           
  • 使用迭代器
//使用迭代器遍历vector
vector<int> iVec;
iVec.push_back(0);iVec.push_back(1);
iVec.push_back(2);iVec.push_back(3);

vector<int>::iterator it;
for (it = iVec.begin();it != iVec.end;++it) cout << *it;

//以vector为元素的vector:
//假设记录全班50位同学三门功课的成绩:
vector<vector<int> > AllScores;
for (int i = 0; i < 50; ++i) {
    vector<int> score(3,0);
    cin >> score[0] >> score[1] >> score[2];
    AllScores.push_back(score);
}
//也可以这样写:
AllScores.resize(50);
for (int i = 0; i < AllScores.size(); ++i) {
    AllScores[i].resize(3);
    cin >> AllScores[i][0] >> AllScores[i][1] >> AllScores[i][2];
}

           
  • 例子:使用一组Point对象,拟合一条直线
void LineFitting(vector<Point> pts, double &a, double &b) {
    double mx = 0;
    double my = 0;
    double mx2 = 0;
    double mxy = 0;
    for (auto a : pts) {
        double xi = a.getX();
        double yi = a.getY();
        mx += xi;
        my += yi;
        mx2 += xi*xi;
        mxy += xi*yi;
    }
    int numPoints = pts.size();
    mx /= numPoints;
    my /= numPoints;
    mx2 /= numPoints;
    mxy /= numPoints;

    a = (mxy - mx*my) / (mx2 - mx*mx);
    b = my - a*mx;
}
int main(void) {
    vector<Point> pts;
    pts.push_back(Point(0, 1.1));
    pts.push_back(Point(1, 2-0.1));
    pts.push_back(Point(3, 4 + 0.01));
    double a = 0, b = 0;
    LineFitting(pts, a, b);
    cout << "a=" << a << ",b=" << b << endl;
    return 0;
}