c++学习笔记十四

32、运算符重载为友元函数
  #include<iostream.h>
class complex 
{
public:
complex(double i=0,double j=0);
friend complex operator -(const complex & c1,const complex & c2);
friend complex operator +(const complex & c1,const complex & c2);
friend complex operator *(const complex & c1,const complex & c2);
void Print();
private:
double real;
double imag;
};
complex::complex(double i,double j)
{
real = i;
imag = j;
}
complex operator -(const complex & c1,const complex & c2)
{
double r = c1.real - c2.real;
double i = c1.imag - c2.imag;
return complex(r,i);
}
complex operator +(const complex & c1,const complex & c2)
{
double r = c1.real + c2.real;
double i = c1.imag + c2.imag;
return complex(r,i);
}
complex operator *(const complex & c1,const complex & c2)
{
double r = c1.real * c2.real;
double i = c1.imag * c2.imag;
return complex(r,i);
}
void complex::Print()
{
cout<<"("<<real<<","<<imag<<")"<<endl;
}
void main()
{
complex c1(10.5,14.3),c2(0.5,4.3);
complex c;
// c = operator -(c1,c2);
c= c1-c2;
c.Print();
    
complex c3(2.2,3.3),c4(7.8,6.7);
c = operator +(c3,c4);
c.Print();


complex c5(1,2),c6(10,5);
c = operator *(c5,c6);
c.Print();
}
总结:a.运算符重载为友元函数,它并不是类申明中的成员函数,但它可以使用类的私有成员。
        在类外部定义时,不能将它申明为complex complex::operator -(),而是complex operator -().
        原因在于它不是类的成员函数。
      b.运算符作为友元函数时,可以使用两种方式使用:1、c = operator -(c1,c2);
   2、c= c1-c2;


33、运算符重载为友元函数与成员函数的不同
#include<iostream.h>
class integer
{
public:
integer(int i=0) {value=i;}
integer operator +(integer i)
{
return integer(value+=i.value);
}
void Print();
private:
int value;
};
void integer::Print()
{
cout<<value<<endl;
}
void main()
{
integer a(5),b(6);
    integer c;
//c = a+b;  正确
//c=a.operator +b; 正确
//c = 3 + b;   错误 
    c = b + 3;     //正确
c.Print();
}


/*
class integer
{
public:
integer(int i=0) {value=i;}
friend integer operator +(integer i1,integer i2);//修改为友元函数
void Print();
private:
int value;
};
integer operator +(integer i1,integer i2)
{
    integer temp=i1.value+i2.value;
    return temp;
}
*/
总结:a.在单目运算符函数作为成员函数,在实际运用中可能会因为操作数据而不同。
      
34、自增重载
#include<iostream.h>
class counter
{
public:
counter()  { value = 0; }
counter operator ++();
counter operator ++(int);
void Print();
private:
int value;
};
counter counter::operator ++()
{
value++;
return *this;
}
counter counter::operator ++(int i)
{
counter t;
t.value = value++;
return t;
}
void counter::Print()
{
cout<<value<<endl;
}
void main()
{
counter c,d;
//c++;
for(int i=0;i<10;i++)
d = c.operator ++(10);   //处理结果表达式和变量都加1
d.Print();
c.Print();
for(i=0;i<10;i++)
d = c.operator ++();     //处理结果表达式不变,但变量加1
c.Print();
d.Print();                
//d.Print();
}
总结:a.该程序主要的知识点在于对重载的++运算符函数中的返回值操作,
        前者返回了*this指针,后者返回了counter变量(无关变量)。


35、下标重载
#include<iostream.h>
class vector
{
public:
vector(int size) { v = new int[size]; }
~vector()     { delete[] v; }
int & operator [](int i) { return v[i]; }   //v[i]为int &型,而不是int *型。
private:
int *v;
};
void main()
{
vector p(4);
for(int i=0;i<4;i++)
p[i]=i;
p[2]=12;
for(i=0;i<4;i++)
cout<<p[i]<<endl;
}
总结:a.v[i]为int &型,而不是int *型。
     b.程序中出现[]符号时,使用operator [](int i) 函数进行处理。


36、用()运算符实现f()的函数操作
//函数运算符实现函数f(x,y)=x*y+5
#include<iostream.h>
class F
{
public:
double operator ()(double i,double j);
};


double F::operator ()(double i,double j)

double k = i*j+5; 
return k;
}


void main()
{
double out;
F f;
        out = f(5,2);
cout<<out<<endl;
}
总结:a.本程序的主要思维是将()作为F类的成员函数,新建一个F类f,然后通过()和参数实现
        对传入参数的值传递。


37、类型适应
    子类型的作用:一个函数可被重用于处理T类型的对象和T的各个子类型的对象,而不必为
  处理这些子类型的对象去重载该函数,可以减轻程序人员编写程序代码的负担。
#include<iostream.h>
#include<iostream.h>
class A
{
public:
A() { a = 0; }
A(int i) { a = i; }
void Print() { cout<<a<<endl; }
int Geta() { return a; }
private:
int a;
};
class B:public A
{
public:
B() { b = 0; }
B(int i,int j):A(i),b(j) { }
void Print() { A::Print();cout<<b<<endl; }
private:
int b;
};
void fun(A &d)
{
cout<<d.Geta()*10<<endl;
}
void main()
{
B bb(9,5);
A aa(5);
aa = bb;
aa.Print();
bb.Print();


cout<<endl;


B *pb = new B(9,5);
A *pa = new A(5);
pa = pb;
//pb->Print();
pa->Print();    //输出的只有a的私有成员,b的私有成员不会变
fun(bb);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值