之运算符重载和虚表指针
分类:web前端

#include<iostream>
using namespace std;
class user{
public:
    user(){
    name=new char[20];
    }
    void getuse();
    void setuse(char *n,int p);
    ~user(){
    delete[]name;
    }
private:
    char *name;
    int password;
};
void user::setuse(char *n,int p){
    name=n;
    password=p;

1 -> *运算符重载

#include<iostream>
#include<string>
using namespace std;
class Teacher
{public:

}
void user::getuse(){
cout<<"帐号:"<<name<<" 密码:"<<password<<endl;
}
int main()
{
    cout<<"The name and the password"<<endl;
user ip;
ip.setuse("my",123);
ip.getuse();
return 0;
}

//autoptr.cpp

Teacher(int n,string nam,char s)
{num=n;
 name=nam;
 sex=s;
}
void display();
protected:
 int num;
 string name;
 char sex;
};
void Teacher::display()
{cout<<"num="<<num<<endl;
cout<<"name="<<name<<endl;
cout<<"sex="<<sex<<endl;}

#include<iostream>
using namespace std;
class user{
public:
    user(){
    name=new char[20];
    }
    void getuse();
    void setuse();
    ~user(){
    delete[]name;
    }
private:
    char *name;
    int password;
};
void user::setuse(){
    cin>>name>>password;     

 

class BirthDate
{ public:
BirthDate(int y,int m,int d)
{year=y;month=m;day=d;}
void display();
protected:
 int year;
 int month;
 int day;

}
void user::getuse(){
cout<<"帐号:"<<name<<" 密码:"<<password<<endl;
}
int main()
{
    cout<<"The name and the password"<<endl;
user ip;
ip.setuse();
ip.getuse();
return 0;
}

 

};

#include<iostream>

class Professor:public Teacher,public BirthDate
{public:
Professor(int n,string nam,char s,int y,int m,int d):
Teacher(n,nam,s),BirthDate(y,m,d){}
void display();

#include<string>

private:

using namespace std;

};

 

void Professor::display()
{cout<<"num="<<num<<endl;
cout<<"name="<<name<<endl;
cout<<"sex="<<sex<<endl;
cout<<"year="<<year<<endl;
cout<<"month="<<month<<endl;
cout<<"day="<<day<<endl;
}

struct date{

int main()
{   Teacher t(123,"asd",'k');
 Professor prof1(072211101,"zhoudongdong",'man',2009,5,18);
Teacher *pt=&t;
pt->display();
pt=&prof1;
pt->display();
return 0;
}

    int year;

    int month;

    int day;

};

 

struct Person{

    string name;

    int age;

    bool gender;

    double salary;

    date birthday;

   

    Person() {cout<<"创建Person对象在"<<this<<endl;}

   

    ~Person(){cout<<"释放Person对象在"<<this<<endl;}

};

 

class autoptr{

    Person *p;

    static int cnt;

public:

    autoptr(Person *p):p(p){}

    autoptr(const autoptr& a):p(a.p){++cnt;}

 

   

    ~autoptr(){

        cout<<"cnt="<<autoptr::cnt<<endl;

        if(--cnt==0)

        delete p;

    }

 

    Person* operator->() {return p;}  //将对象模拟成指针

   

    Person& operator*() {return *p;}

};

 

int autoptr::cnt=0;

 

int main()

{

    //autoptr a(new Person());

    autoptr a=new Person();

    autoptr b=a;

    autoptr c=a;

 

    cout<<"=============================="<<endl;

 

    a->name="zhangming";

    cout<<"name:"<<(*a).name<<endl;

 

    a->birthday.year=1993;

    a->birthday.month=10;

    a->birthday.day=9;

    cout<<"birthday:"<<(*a).birthday.year<<"/"<<(*a).birthday.month

        <<"/"<<(*a).birthday.day<<endl;

   

    cout<<"==============================="<<endl;

 

    return 0;

}

 

 

2.赋值运算符=重载,实现堆栈类

//stack.cpp

 

 

#include<iostream>

#include<string>

using namespace std;

 

typedef unsigned int uint;

 

class Stack

{

public:

    Stack(uint n):mem(new string[n]),max(n),len(0){}

    Stack(const Stack &s):mem(new string[s.max]),

                  max(s.max),len(s.len){}

 

    uint max_size()const {return max;} 

    uint size()const {return len;}

 

    Stack& push(const string &s)

    {

        if(len>=max) throw 1;

        mem[len++]=s;

        return *this;

    }

   

    string pop()

    {

        if(len==0) throw 0;

        return mem[--len];

    }

 

    ~Stack(){ delete[]mem; }

 

    void print()const{

        for(int i=0;i<len;i++)

        {

            cout<<mem[i]<<" ";

        }

        cout<<endl;

    }

 

    //重载赋值运算符

    Stack& operator=(const Stack &s)

    {

        if(*this==s)  return *this;  //考虑到自己给自己赋值

 

        delete[]mem; //释放原来的空间

 

        this->mem=new string[s.max];

        this->len=s.len;

        this->max=s.max;

 

        for(int i=0;i<len;i++)

        {

            mem[i]=s.mem[i];

        }

 

        return *this;

    }

 

private:

    string* mem;   

    uint max;

    uint len;

};

 

 

int main()

{

    Stack s1(5);

    Stack s2(s1); //错误,s1与s2同时指向同一块内存,

              //致使delete重复释放,

              //可以使用拷贝构造函数解决

    Stack s3(8);

 

    s1.push("1").push("2").push("3").push("4").push("5");

    s1.print();

 

    s1.pop();

    s1.pop();

   

    s1.print();

 

    s2.push("zhangming").push("wangwu");

    s2.print();

   

    s3=s1;   //s3.operator=(s1)

    s3.print();  //1 2 3

 

    s1=s2;

    s1.print();  //zhangming wangwu

 

    s3=s3;

    s3.print();  //1 2 3

 

    return 0;

}

 

 

 

3.new delete 运算符重载

//ND.cpp

 

#include<iostream>

using namespace std;

 

const int max_size=1000;

int mem[max_size];

 

class A

{

public:

    A(){cout<<"A()"<<endl;}

   

    ~A(){cout<<"~A()"<<endl;}

 

    static void* operator new(size_t bytes)  //bytes=sizeof(A)

    {

        cout<<"new"<<endl;

 

        alloc=bytes;  //即alloc=sizeof(A)

        if(alloc>max_size) throw 0;

 

        return (mem+alloc);

    }

 

    static void operator delete(void *p)

    {

        cout<<"delete"<<endl;

        alloc=0;   

    }

 

    void init(int n){

        memset(mem,max_size,sizeof(int));

        for(int i=0;i<n;i++)

        {

            mem[i]=i;  

        }

    }

 

    void show(){

        for(int i=0;i<alloc;i++)

        {

            cout<<mem[i]<<" "; 

        }

        cout<<endl;

    }

 

private:

    static int alloc;

    int num;

    char name[10];

};

 

int A::alloc=0;

 

int main()

{

    A *a=new A;  //实参实际上为sizeof(A)

 

    a->init(5);  //给分配的前五个元素赋初值,剩余元素赋0

    a->show();

 

    delete a;

 

    return 0;

}

 

 

 

 

4.虚函数与虚表指针

//virtual.cpp

 

 

#include<iostream>

using namespace std;

 

class A{

    int d;

public:

    virtual void f(){cout<<"A类的虚函数"<<endl;}

    virtual void g(){cout<<this<<","<<&d<<endl;}

    int* get_d(){return &d;}

};

 

class B:public A{

    int d;

public:

    void f(){cout<<"B类的虚函数"<<endl;}

    void g(){cout<<this<<","<<get_d()<<endl;}

    void k(){}

    void m(){}

    void n(){} 

};

 

int main()

{

    A *p=new A;

    A *q=new B;

 

    p->f();  //输出:A类的虚函数

    q->f();  //输出:B类的虚函数

   

    p->g();

    q->g();

 

    memcpy(q,p,4);//让q所指对象的虚表指针指向A类

    q->f();  //输出:A类的虚函数

   

    delete p;

    delete q;

 

    cout<<"================="<<endl;

    cout<<sizeof(A)<<endl;

    cout<<sizeof(B)<<endl;

    cout<<"================="<<endl;

 

    return 0;

}

本文由10bet手机官网发布于web前端,转载请注明出处:之运算符重载和虚表指针

上一篇:没有了 下一篇:为什么需要auto_ptr_ref
猜你喜欢
热门排行
精彩图文