目录
004_complex.cc Complex pt3=operator+(pt1,pt2)
005_complec.cc Complex pt3=pt1.operator+(pt2)
04 如果类A被声明成类B的友元,则( )。(多选题) BDE
07 运算符重载的原则是什么?有哪些规则?不能重载的运算符有哪几个?
08 用运算符重载方式实现Complex类对象的+、-、+=、-=、自增、自减操作
09 编写Base类使下列代码输出为1,提示:本题考查的其实就是运算符重载的知识点。
001_friend.cc
#include <iostream>
#include <math.h>
using std::cout;
using std::endl;
class Point{
public:
Point(int x,int y)
:_ix(x),_iy(y)
{}
friend double distance(const Point & p1,const Point & p2);
private:
int _ix;int _iy;
};
double distance(const Point & p1,const Point & p2){
return sqrt(pow(p1._ix-p2._ix,2)+
pow(p1._iy-p2._iy,2));
}
void test(){
Point pt1(0,0);
Point pt2(3,4);
cout<<distance(pt1,pt2)<<endl;
}
int main(void)
{
test();
return 0;
}
002_friend.cc
#include <iostream>
#include <math.h>
using std::cout;
using std::endl;
class Point;//前向声明
//可以利用POint引用或者point指针
class Line{
public:
double distance(const Point & p1,const Point & p2);
void setPoint(Point & p1);
/* double distance(const Point & p1,const Point & p2){ */
/* return sqrt(pow(p1._ix-p2._ix,2)+ */
/* pow(p1._iy-p2._iy,2)); */
/* }//坏,报错,不知道具体实现喵 */
};
class Point{
public:
Point(int x,int y)
:_ix(x),_iy(y)
{}
friend double Line::distance(const Point & p1,const Point & p2);
friend void Line::setPoint(Point & p1);
private:
int _ix;int _iy;
};
double Line::distance(const Point & p1,const Point & p2){
return sqrt(pow(p1._ix-p2._ix,2)+
pow(p1._iy-p2._iy,2));
}
void Line::setPoint(Point & p1){
p1._ix=100;
}
void test(){
Point pt1(0,0);
Point pt2(3,4);
Line ll;
cout<<ll.distance(pt1,pt2)<<endl;
ll.setPoint(pt1);
cout<<ll.distance(pt1,pt2)<<endl;
}
int main(void)
{
test();
return 0;
}
003_friend.cc
#include <iostream>
#include <math.h>
using std::cout;
using std::endl;
class Point;//前向声明
class Line{
public:
double distance(const Point & p1,const Point & p2);
void setPoint(Point & p1);
};
class Point{
public:
Point(int x,int y)
:_ix(x),_iy(y)
{}
friend class Line;//友元类
private:
int _ix;int _iy;
//坏 将Point 用C语言的方式强转指针,理解为两个int类型的数组
};
double Line::distance(const Point & p1,const Point & p2){
return sqrt(pow(p1._ix-p2._ix,2)+
pow(p1._iy-p2._iy,2));
}
void Line::setPoint(Point & p1){
p1._ix=100;
p1._iy=100;
}
void test(){
Point pt1(0,0);
Point pt2(3,4);
Line ll;
cout<<ll.distance(pt1,pt2)<<endl;
ll.setPoint(pt1);
cout<<ll.distance(pt1,pt2)<<endl;
}
int main(void)
{
test();
return 0;
}
004_complex.cc Complex pt3=operator+(pt1,pt2)
#include <iostream>
using std::cout;
using std::endl;
class Complex{
public:
Complex(int real,int image)
:_real(real),_image(image)
{}
void print(){
cout<<_real<<"+"<<_image<<"i"<<endl;
}
/* int getImage()const{return _image;} */
/* int getReal()const{return _real;} */
friend Complex operator+(const Complex & c1,const Complex & c2);
private:
int _real;int _image;
};
//友元函数形式实现
//ababa 别用 抽风小辉 aaaaaaaa可以用
Complex operator+(const Complex & c1,const Complex & c2){
return Complex(c1._real+c2._real,c1._image+c2._image);
}
//普通函数实现
/* Complex operator+(const Complex & c1,const Complex & c2){ */
/* return Complex(c1.getReal()+c2.getReal(),c1.getImage()+c2.getImage()); */
/* }//破坏性更强喵! */
void test(){
Complex c1(3,4);
Complex c2(6,1);
c1.print();c2.print();
Complex c3=c1+c2;
c3.print();
}
int main(void)
{
test();
return 0;
}
005_complec.cc Complex pt3=pt1.operator+(pt2)
#include <iostream>
using std::cout;
using std::endl;
class Complex{
public:
Complex(int real,int image)
:_real(real),_image(image)
{}
void print(){
cout<<_real<<"+"<<_image<<"i"<<endl;
}
//成员函数形式实现
//成员函数 一个参数, 其他多个
Complex operator+(const Complex & c1){
return Complex(c1._real+_real,c1._image+_image);
}
private:
int _real;int _image;
};
void test(){
Complex c1(3,4);
Complex c2(6,9);
c1.print();c2.print();
Complex c3=c1+c2;
c3.print();
}
int main(void)
{
test();
return 0;
}
006_operator_=.cc
#include <iostream>
using std::cout;
using std::endl;
class Complex{
public:
Complex(int real,int image)
:_real(real),_image(image)
{}
void print(){
cout<<_real<<"+"<<_image<<"i"<<endl;
}
Complex operator+(const Complex & c1){
return Complex(c1._real+_real,c1._image+_image);
}
//不修改操作数的--》友元 修改操作数的--》成员
//希望自定义类型在操作时和内置类型一样
Complex & operator +=(const Complex & c1){
this->_real+=c1._real;
this->_image+=c1._image;
return *this;
}
private:
int _real;int _image;
};
void test(){
Complex c1(3,4);
Complex c2(6,9);
c1.print();c2.print();
Complex c3=c1+c2;
c3.print();
c1+=c3;
c1.print();
}
int main(void)
{
test();
return 0;
}
007_operator=.cc
#include <iostream>
using std::cout;
using std::endl;
class Complex{
public:
Complex(int real,int image)
:_real(real),_image(image)
{}
void print(){
cout<<_real<<"+"<<_image<<"i"<<endl;
}
Complex operator+(const Complex & c1){
return Complex(c1._real+_real,c1._image+_image);
}
Complex & operator +=(const Complex & c1){
this->_real+=c1._real;
this->_image+=c1._image;
return *this;
}
Complex & operator =(const Complex & c1){
this->_real=c1._real;
this->_image=c1._image;
return *this;
}
//重载
Complex & operator =(int c1){
this->_real=c1;
this->_image=c1;
return *this;
}
private:
int _real;int _image;
};
void test(){
Complex c1(3,4);
Complex c2(6,9);
c1.print();c2.print();
Complex c3=c1+c2;
c3.print();
c1+=c3;
c1.print();
c3=c1;
c3.print();
c3=250;
c3.print();
}
int main(void)
{
test();
return 0;
}
008_operator++.cc
#include <iostream>
using std::cout;
using std::endl;
class B
{
int y;
public:
friend class A;
};
class A
{
int x;
public:
A(int a,B &rrrr, int b)
{
x=a;
rrrr.y=b;
}
void Display( B & );
};
void A::Display(B &rrrr)
{
cout<<x<<" "<<rrrr.y<<endl;
}
int main( )
{
B Obj2;
A Obj1(33, Obj2, 88);
//x=33,y=88
Obj1.Display(Obj2);
//33 88
return 0;
}
作业:
01 关于友元的描述中,( )是错误A
-
A友元函数是成员函数,它被说明在类体内
-
B友元函数可直接访问类中的私有成员
-
C友元函数破坏封装性,使用时尽量少用
-
D友元类中的所有成员函数都是友元函数
02 下面对于友元函数描述正确的是( ) C
-
A友元函数的实现必须在类的内部定义 ×
-
B友元函数是类的成员 ×
-
C友元函数破坏了类的封装性和隐藏性
-
D友元函数不能访问类的私有成员 ×
03 友元的作用是( ) A
-
A提高程序的运行效率
-
B加强类的封装性
-
C实现数据的隐蔽
-
D加成员函数的种类
04 如果类A被声明成类B的友元,则( )。(多选题) BDE(坏,DE
-
A类A的成员即类B的成员 (不对,B不能访问A,坏!,给我看晕了
-
B类B的成员即类A的成员 (对
-
C类A的成员函数不能访问类B的成员 (能
-
D类A的成员函数可以访问类B的成员 (是哒
-
E类B不一定是类A的友元 (对!
05 写出下列程序的结果 33 88
class B
{
int y;
public:
friend class A;
};
class A
{
int x;
public:
A(int a,B &r, int b)
{
x=a;
r.y=b;
}
void Display( B & );
};
void A::Display(B &r)
{
cout<<x<<" "<<r.y<<endl;
}
int main( )
{
B Obj2;
A Obj1(33, Obj2, 88);
Obj1.Display(Obj2);
return 0;
}
06 什么是友元?友元的存在形式有?友元有何特点?
1,将其他类/函数这是为一个类的友元,那么友元类/函数就可以在前一个类的类定义之外访问其私有成员了
2,普通函数形式,成员函数形式,友元类
3,特点
——友元不受类中访问权限的限制(可以访问私有
——破坏了类的封装性
——不能滥用
——单向的(A有B的钥匙,B没有A的钥匙
——不传递
——不继承
07 运算符重载的原则是什么?有哪些规则?不能重载的运算符有哪几个?
1,原则
希望自定义类型在操作时和内置类型保持一致
2,规则
01,操作数类型,必须要有自定义类型或枚举类型,不能全是内置类型
02,优先级结合性,不变
03,操作数个数,不变
04,参数,不能设置默认参数(设置默认值,就是改变了操作数个数
05,逻辑与&& 逻辑或|| 不再具备短路求值特性,进入函数体之前必须完成所有函数参数得计算,不推荐重载
06,不能重载内置不存在的运算符
3,带点的+sizeof
.成员访问运算符
.*成员指针访问运算符
?:三目运算符
::作用域限定符
sizeof长度运算符
08 用运算符重载方式实现Complex类对象的+、-、+=、-=、自增、自减操作
#include <iostream>
using std::cout;
using std::endl;
class Complex{
public:
Complex(int real,int image)
:_real(real),_image(image)
{}
void print(){
cout<<_real<<"+"<<_image<<"i"<<endl;
}
Complex operator+(const Complex & c1){
return Complex(c1._real+_real,c1._image+_image);
}
Complex operator-(const Complex & c1){
return Complex(_real-c1._real,_image-c1._image);
}
Complex & operator +=(const Complex & c1){
this->_real+=c1._real;
this->_image+=c1._image;
return *this;
}
Complex & operator -=(const Complex & c1){
this->_real-=c1._real;
this->_image-=c1._image;
return *this;
}
Complex & operator =(const Complex & c1){
this->_real=c1._real;
this->_image=c1._image;
return *this;
}
Complex & operator =(int c1){
this->_real=c1;
this->_image=c1;
return *this;
}
Complex & operator++(){
cout<<"<<<<<<<<++complex"<<endl;
this->_real++;
this->_image++;
return *this;
}
Complex operator++(int){
cout<<"<<<<<<complex++"<<endl;
Complex cx(_real,_image);
this->_real++;
this->_image++;
return cx;
}
Complex & operator--(){
cout<<"<<<<<<<< --complex"<<endl;
this->_real--;
this->_image--;
return *this;
}
Complex operator--(int){
cout<<"<<<<<< complex--"<<endl;
Complex cx(_real,_image);
this->_real--;
this->_image--;
return cx;
}
private:
int _real;int _image;
};
void test(){
Complex c1(3,4);
Complex c2(6,9);
c1.print();c2.print();
Complex c3=c1+c2;
c3.print();
//c2.operator-(c1)
c3=c2-c1;
c3.print();
cout<<endl;
c1+=c3;
c1.print();
c1-=c3;
c1.print();
cout<<endl;
c3=c1;
c3.print();
c3=250;
c3.print();
cout<<endl;
(++c3).print();
(c3++).print();
c3.print();
cout<<endl;
(--c3).print();
(c3--).print();
c3.print();
}
int main(void)
{
test();
return 0;
}
09 编写Base类使下列代码输出为1,提示:本题考查的其实就是运算符重载的知识点。
int i=2;
int j=7;
Base x(i);
Base y(j);
cout << (x+y == j - i) << endl;
#include <iostream>
using std::cout;
using std::endl;
/* 编写Base类使下列代码输出为1 */
class Base{
public:
Base(int b)
:_b(b)
{}
Base operator+(const Base & b){
return Base(b._b-_b);
}
bool operator==(int num){
return _b==num;
}
private:
int _b;
};
void test(){
int i=2;
int j=7;
Base x(i);
Base y(j);
//x+y = i+j?? -x+y
cout << (x+y == j - i) << endl;
}
int main(void)
{
test();
return 0;
}