0621 第五讲操作符重载(1)算数、输出、下标运算符重载

0621 第五讲操作符重载(1)算数、输出、下标运算符重载
操作符重载:操作符操作自定义类型操作数
注意:
1)只能重载已经存在的操作符(eg:+ - * /)
2)重载后操作数 数量不变(4+5)
3)至少有一个操作数是用户自定义类型
object+object object+int int+int
4)重载后优先级不变
operator:要重载的操作符是…
操作符重载两种形式:friend 成员函数
如果函数实现在声明中完成,这个函数是一个内联函数(不要过于复杂if,代码不要过长,快,增加内存使用)
CPoint.hpp

friend CPoint operator+(const CPoint &p1,const CPoint &p2);
main.cpp
CPoint operator/(const CPoint &p1,const CPoint &p2)
{
    if (p2.x==0||p2.y==0) {
        exit(1);
    }
    return CPoint(p1.x/p2.x,p1.y/p2.y);
}

只能用类的成员函数实现重载的操作符:下标操作符、自增自减操作符
只能用类的友元函数实现重载的操作符:输出操作符
既可以用类的成员函数又可以用类的友元函数实现重载的操作符:赋值运算符、算数运算符以及关系运算符
成员函数运算符重载
当一个对象作为参数传递,在函数体内是可以访问其私有成员的
eg:

CPoint CPoint:: operator+(const CPoint &p1)
{
    return CPoint(p1.x+x,p1.y+y);
}

1、输出操作符重载:
cout:对象
ostream:输出流类型

cin:对象
isteam:输入流类型

ostream&:函数链
stream & out: 直接使用cout(out)l
const Point *p输出对象:
注意:输出输入操作符重载只能使用friend
CPoint.hpp
friend ostream& operator<<(ostream& out,const CPoint &p);
Circle.hpp
friend ostream& operator<<(ostream& out,const Circle& c);
main.cpp

ostream& operator<<(ostream& out,const CPoint &p)//标准适用格式
{
    out<<p.x<<endl;
    out<<p.y<<endl;
    return out;
}
ostream& operator<<(ostream& out,const Circle& c)
{
//    cout<<"x = "<<c.p.getX()<<endl;
//    cout<<"y = "<<c.p.getY()<<endl;
    out<<c.p<<endl;
    out<<"r = "<<c.r<<endl;
    out<<"area = "<<c.getArea()<<endl;
    out<<"girth = "<<c.getGirth()<<endl;
    return out;
}

2、输入操作符重载:
CPoint.hpp

friend istream& operator>>(istream& in,CPoint& p);

Circle.hpp

friend istream& operator>>(istream& in,Circle& c);

main.cpp

istream& operator>>(istream& in,Circle& c)
{
    in>>c.p;
    in>>c.r;
    return in;
}
istream& operator>>(istream& in,CPoint& p)
{
    in>>p.x;
    in>>p.y;
    return in;
}

1+2主函数:

int main(int argc, const char * argv[]) {
    CPoint p1(1,1),p2(2,2);
    Circle c1(p1,1);
    cout<<c1;
    cin>>p1;
    p1.Print();
    cin>>c1;
    c1.print();
    return 0;
}

3、下标运算符重载:

class Test1
{
public:
    //const对象进行调用
    const int& operator[](int index)const//[]自定义函数
    {
        return array[index];//[]系统提供下表符
    }
    //非const对象进行调用
    int& operator[](int index)
    {
        return array[index];
    }
    void print()
    {
        for (int i = 0; i < 2; ++i) {
            cout<<array[i];
        }
    }
private:
    int array[2];
};
int main(int argc, const char * argv[]) {
  Test1 t1;
    t1[0]=0;
    cout<<t1[0]<<endl;
}

大作业;
CPerson.hpp

#ifndef CPerson_hpp
#define CPerson_hpp
#include "CDate.hpp"
#include <stdio.h>
class CPerson
{
private:
    char *name;
    char *email;
    CDate birthday;
public:
    CPerson ();
    CPerson(char *_name,char *_email,CDate _birthday);
    CPerson(char *_name,char *_email,int _year,int _month,int _day);
    ~CPerson();
    void print();
    void set(char *_name,char *_email);
    char* getName();
    char* getEmail();
    CDate getBirthday();
};
#endif

CPerson.cpp

#include "CPerson.hpp"
#include <string>
#include <iostream>
#include "CDate.hpp"
using namespace std;
//CPerson(char *_name,char *_email,CDate _birthday);
//CPerson(char *_name,char *_email,int _year,int _month,int _day);
//~CPerson();
//void print();
//void set(char *_name,char *_email,int _year,int _month,int _day);
//char* getName();
//char* getEmail();
//int getAge();
//CDate* getBirthday();
CPerson:: CPerson ()
{
    cout<<"调用CPerson无參析构函数"<<endl;
}
CPerson::CPerson(char *_name,char *_email,CDate _birthday):birthday(_birthday)
{
    name=new char[strlen(_name)];
    strcpy(name, _name);
    email=new char[strlen(_email)];
    strcpy(email, _email);
    cout<<"调用CPerson参数有CDate对象的构造函数"<<endl;
}
CPerson::CPerson(char *_name,char *_email,int _year,int _month,int _day):birthday(_year,_month,_day)
{
    name=new char[strlen(_name)];
    strcpy(name, _name);
    email=new char[strlen(_email)];
    strcpy(email, _email);
    cout<<"调用CPerson输入年月日的构造函数"<<endl;

}

CPerson::~CPerson()
{
    if (name !=NULL) {
        delete name;
        name=NULL;
    }
    if (email !=NULL) {
        delete email;
        email=NULL;
    }
    cout<<"调用CPerson析构函数"<<endl;
}
void CPerson::print()
{
    cout<<"name :"<<name<<endl;
    cout<<"sex :"<<email<<endl;
    birthday.print();
}

//void CPerson::set(char *_name,char *_email){
//    name=new char(strlen(_name)+1);
//    strcpy(name, _name);
//    email=new char(strlen(_email)+1);
//    strcpy(email, _email);
//}

char* CPerson::getName(){
    return name;
}

char* CPerson::getEmail(){
    return email;
}

CDate CPerson::getBirthday()
{
    return birthday;
}

Set.hpp

#ifndef Set_hpp
#define Set_hpp

#include <stdio.h>
#include "CPerson.hpp"
class Set
{
private:
    CPerson* *elements;
    int size;
    int capcity;
    int index;
public:
    Set (int size = 20);
    ~Set();
    void addPerson(CPerson &p);
    CPerson& removePerson();
    CPerson& removePerson(int index);
    CPerson& nextPerson();
    void print()const;
    int getSize()const;
    void Reset(int _size=0,int _capcity=0,int _index=0);
};
#endif

Set.cpp

#include "Set.hpp"
#include <iostream>
#include "CPerson.hpp"
using namespace std;
Set::Set (int _size)
{
    elements = new CPerson* [_size];
    size=0;
    capcity=_size;
    index=0;
}
Set::~Set()
{
//    if (*array !=NULL) {
//        delete []*array;
//        *array=NULL;
//    }
//    delete [] *array;
    if (elements != NULL) {
        delete [] elements;
    }

    cout<<"调用Set析构函数"<<endl;
}
void Set::addPerson(CPerson &p)
{
//    array[size]=new CPerson();
    if (size == capcity) {
        CPerson* *temp=elements;
        elements=new CPerson* [capcity+=10];
        for (int i=0; i<size; ++i) {
            elements[i]=temp[i];
        }
        delete []temp;
    }
    elements[size++]=&p;
//    elements = new CPerson *[1];
//    elements[size]=&p;
//    size++;
//    print();
}
void Set::print()const
{
    cout<<"size:"<<size<<endl;
    for (int i=0; i<size; ++i)
    {
        elements[i]->print();
    }
}
CPerson& Set::removePerson()
{
//     CPerson *p=elements[size];
//    if (elements[size]!=NULL) {
        --size;
        CPerson *p=elements[size];
        if (size<capcity/2) {
            cout<<"shringing\n"<<endl;
            CPerson* *temp=elements;
            elements=new CPerson* [capcity/=2];
            for (int i = 0; i < size; ++i) {
                elements[i]=temp[i];
            }
            delete []temp;
        }
//    }
//    print();
//    else
//    {
//        cout<<"没有空间释放了!"<<endl;
//    }
    return *p;
}
CPerson& Set::removePerson(int index)
{
    cout<<"shringing\n"<<endl;
    CPerson *p=elements[index];
    for (int i = index; i < size; ++i) {
        elements[i]=elements[i+1];
    }
    if (size<capcity/2) {
        cout<<"shringing\n"<<endl;
        CPerson* *temp=elements;
        elements=new CPerson* [capcity/=2];
        for (int i = 0; i < size; ++i) {
            elements[i]=temp[i];
        }
        delete []temp;
    }
    return *p;
}
CPerson& Set:: nextPerson()
{
    if (index == size) {
        index = 0;
    }
    return *elements[index++];
}
int Set::getSize()const
{
    return size;
}
void Set:: Reset(int _size,int _capcity,int _index ){
    size=_size;
    capcity=_capcity;
    index=_index;
}

main.cpp

#include <iostream>
#include "CDate.hpp"
#include "CPerson.hpp"
#include "Set.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
    CPerson *p1 = new CPerson((char*)"Lou",(char*) "lou@chat.ca", 20, 6, 1960);
    CPerson *p2 = new CPerson((char*)"Frank",(char*)"f123@chat.ca", 20, 3, 1967);
    CPerson *p3 = new CPerson((char*)"Ann", (char*)"ann@chat.ca", 20, 8, 1960);

    Set boys, girls;
    boys.addPerson(*p1);

    //test to see if the same object is retrieved from the set.
    if (p1 != &boys.removePerson())
    {
        cout << "ERROR: the objects are different \n";
    }
    else
    {
        cout << "Good, the objects are the same \n";
    }

    boys.addPerson(*p1);
    boys.addPerson(*p2);
    girls.addPerson(*p3);
    boys.addPerson(*(new CPerson((char*)"John", (char*)"f123@chat.ca", 20, 3, 1967)));
    girls.addPerson(*(new CPerson((char*)"Sue", (char*)"f123@chat.ca", 20, 3, 1967)));
    boys.addPerson(*(new CPerson((char*)"Frank", (char*)"frank@chat.ca", 25, 4, 1958)));
    girls.addPerson(*(new CPerson((char*)"Mary",(char*) "mary@chat.ca", 25, 4, 1955)));
    boys.addPerson(*(new CPerson((char*)"John", (char*)"johnchat.ca", 12, 12, 1970)));
    //print all the boys using the removeSomeElement() method and delete //them
    int numberOfBoys = boys.getSize();
    cout << "number of boys = " << numberOfBoys << "\n";

    for(int i = 0; i<numberOfBoys; i++)
    {
        CPerson & boy = boys.removePerson();
        boy.print();
        delete &boy;
    }

    //print the girls using the << operator of the SetOfPersons class
    cout << "number of girls = " << girls.getSize() << "\n";
    girls.print();

    //print of the girls birthdays and using the someElement() method
    int numberOfGirls = girls.getSize();
    girls.Reset();
    for(int i = 0; i<numberOfGirls; i++)
        girls.nextPerson().getBirthday().print();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值