C++第四天学习

回顾:
1.初始化表
2.this指针
3.拷贝构造
Test(const Test& rt)
{
//1.分配新空间
//2.给新空间赋值
}
4.static成员
类::函数();
类型 类名::静态成员变量=0;

----------------------------------------
1.友元 friend
友元是对类的封装机制的一个补充。
一个类可赋予 某些函数/类 访问它的私有成员的权限

友元分为友元函数和友元类

1.友元函数
1.友元函数可以是普通的函数
在类中声明友元函数的格式:
friend 类型 函数名(参数表);
2.友元函数可以是类的成员函数
friend 类型 类名::函数名(参数表);

类的前置声明:
class 类名;

2.友元类
friend 类名;

注:
友元关系是单向的。
友元关系是不可传递的。

练习:求两点之间的距离。
ex.cpp


2.运算符重载
cout << "hello c++";
C++中函数可以重载,运算符也能够重载,运算符重载会赋予该运算符一种新的含义。

class student
{

}

(x,y)

1 + 1 = 2
1.1 + 2.2 = 3.3

科学计算
复数 :实部 虚部
class Complex
{
public:
Complex();

private:
double real;
double img;
};
Complex c1,c2;
c3 = c1 + c2;
(1,2i)

运算符重载是通过创建 运算符重载函数 来实现的。

运算符重载函数:
返回类型 operator运算符(参数表);
如:
int operator+(Complex ,Complex);

运算符重载函数: 可以是类外定义的普通函数
也可以是类的成员函数或友元函数
运算符--->运算符函数:
运算符函数代替了运算符,其中函数参数对应的是运算符的操作数,函数返回值对应运算符的运算结果。

(重载输入运算符,输入一个点。)

哑元:占位用的,只有类型,没有形参

 

 

#include<iostream>
using namespace std;


class student
{
public:
student(int n=0,string sn="zhangfei"):num(n),name(sn)
{

}
friend void func(student );
void show()
{
cout << num << endl;
cout << name << endl;
}
private:
int num;
string name;
};

void func(student s)
{

cout << s.num << endl;
cout << s.name << endl;
}

int main()
{
student s;
func(s);
return 0;
}

 

 

 

#include<iostream>
using namespace std;

class Girl;

class Boy
{
public:
Boy()
{
num = 123456;
name = "zhaoyun";
}
void show(Girl& x);

void show2(Girl& x);

private:
int num;
string name;
};


class Girl
{
public:
Girl()
{
num = 8888888;
name = "xiaowei";
}
friend Boy;
// friend void Boy::show(Girl&);
private:
int num;
string name;
};
void Boy::show(Girl& x)
{
cout << x.num << endl;
cout << x.name << endl;
}

void Boy::show2(Girl& x)
{
cout << x.num << endl;
cout << x.name << endl;
}

int main()
{
Boy boy;
Girl girl;
boy.show(girl);
boy.show2(girl);

}

 

 

#include<iostream>
using namespace std;

class Point
{
public:
Point()
{
x = 0;
y = 0;
}
void show()
{
cout << "(" << x << "," << y << ")" << endl;
}
private:
int x;
int y;
};

int main()
{
Point p;
// cout << p << endl;
// p.show();
}

 

 

#include<iostream>
using namespace std;

class Complex
{
public:
Complex(double r=0,double i=0);
void show();
// friend Complex operator+(Complex&,Complex&);
Complex operator+(Complex& c2);
friend ostream& operator<<(ostream &o,Complex &c3);
private:
double real;
double img;
};

Complex::Complex(double r,double i):real(r),img(i)
{
}

void Complex::show()
{
cout << "("<< real << "," << img << "i)" << endl;
}

Complex Complex::operator+(Complex& c2)//成员函数
{
Complex c;
c.real = c2.real + this->real;
c.img = c2.img + this->img;
return c;

}
/*
Complex operator+(Complex &c1,Complex &c2)//友元函数
{
Complex c
c.real = c1.real + c2.real;
c.img = c1.img + c2.img;
return c;
}
*/

ostream& operator<<(ostream &o,Complex &c3)
{

o << "(" << c3.real << "," << c3.img << "i)";
return o;
}

int main()
{
Complex c1(1,1),c2(2,2);
Complex c3;
c3 = c1 + c2;// c3 operator+(c1,c2)
// c1.operaor+(c2)
// c3 = c1.operator+(c2);

// c3.show();
cout << c3 << endl;// ostream& operator<<(cout,c3)

}

 

 

#include<iostream>
using namespace std;

class Time
{
public:
Time()
{
m = 0;
s = 0;
}
void show()
{
cout << m << ":" << s << endl;
}
friend Time& operator++(Time&);

friend Time operator++(Time&,int);
private:
int m;
int s;
};

Time& operator++(Time& t)//前++
{
if(++t.s == 60)
{
t.s = 0;
t.m++;
}
return t;
}

Time operator++(Time& t,int)//表示后++
{
Time temp = t;
if(++t.s == 60)
{
t.s = 0;
t.m++;
}
return temp;
}
int main()
{
Time t1;
// ++(++t1);// t1 operator++(t1)

// t1++;// t1 operator++(t1)
cout << "------------------" << endl;
// cout << t1++;
// cout << t1;
Time t2 = t1++;
t2.show();
t1.show();
}

 

 

 

#include<iostream>
#include<cmath>
using namespace std;

class Point
{
public:
Point()
{
x = 0;
y = 0;
}
Point(int x,int y)
{
this->x = x;
this->y = y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
void setX(int x)
{
this->x = x;
}
void setY(int y)
{
this->y = y;
}
friend double Dist(Point&,Point&);
private:
int x;
int y;

};
//1.写一个函数求两点之间的距离
//2.写一个类,实现求两个点之间的距离


double Dist(Point &p1,Point &p2)
{
double dist = sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
return dist;
}
int main()
{
Point p1(0,3),p2(4,0);
cout << "p1到p2之间的距离为:"<< Dist(p1,p2) << endl;

}

posted @ 2016-07-19 20:16  立刻行动  阅读(136)  评论(0编辑  收藏  举报