回顾:
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;

}

C++第四天学习的更多相关文章

  1. apue第四章学习总结

    apue第四章学习总结 4.1.若以stat函数去替换lstat函数,会发生: 原来的目录路径: $:~/workspace/apue2/include$ ls -l apue.h abc lrwxr ...

  2. Factorization Machines 学习笔记(四)学习算法

      近期学习了一种叫做 Factorization Machines(简称 FM)的算法.它可对随意的实值向量进行预測.其主要长处包含: 1) 可用于高度稀疏数据场景:2) 具有线性的计算复杂度.本文 ...

  3. 《Linux内核设计与实现》第四章学习笔记

    <Linux内核设计与实现>第四章学习笔记           ——进程调度 姓名:王玮怡  学号:20135116 一.多任务 1.多任务操作系统的含义 多任务操作系统就是能同时并发地交 ...

  4. 《Linux内核设计与实现》第四章学习笔记——进程调度

                                                                        <Linux内核设计与实现>第四章学习笔记——进程调 ...

  5. Spring实战第四章学习笔记————面向切面的Spring

    Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...

  6. 孤荷凌寒自学python第六十四天学习mongoDB的基本操作并进行简单封装3

    孤荷凌寒自学python第六十四天学习mongoDB的基本操作并进行简单封装3 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第十天. 今天继续学习mongoDB的简单操作, ...

  7. 《码出高效:Java开发手册》第四章学习记录,内容想当的多,前后花了几天的时间才整理好。

    <码出高效:Java开发手册>第四章学习记录,内容想当的多,前后花了几天的时间才整理好. https://naotu.baidu.com/file/e667435a4638cbaa15eb ...

  8. 鸟哥的linux私房菜——第四章学习

    ******************第四章学习****************** [热键] 1.Tab键:命令补全:文件补全: 2.Ctrl+c:中断目前指令: 3.Ctrl+d:离开当前文本界面: ...

  9. Day4 《机器学习》第四章学习笔记

    决策树 前几天学习了<机器学习>的前三章,前三章介绍机器学习的基础知识,接下来,第四章到第十章介绍一些经典而常用的机器学习方法,这部分算是具体的应用篇,第四章介绍了一类机器学习方法——决策 ...

  10. 第三周学习java第四章学习总结及体会!

    第三周java 2第四章的学习总结: 一.主要内容(类与对象): 1.类: 2.构造方法与对象的创建: 3.类与程序的基本结构: 4.参数传值: 5.对象的组合: 6.实例成员与类成员: 7.方法重载 ...

随机推荐

  1. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...

  2. Linux字符编码转换 UTF8转GB3212

    在LINUX上进行编码转换时,既可以利用iconv函数族编程实现,也可以利用iconv命令来实现,只不过后者是针对文件的,即将指定文件从一种编码转换为另一种编码.    一.利用iconv函数族进行编 ...

  3. 谈谈java的BlockingQueue

    http://www.cnblogs.com/archy_yu/archive/2013/04/19/3018479.html 博客园 首页 新随笔 联系 管理 随笔- 92  文章- 0  评论- ...

  4. Laravel Auth验证

    laravel自带了auth类和User模型来帮助我们很方便的实现用户登陆.判断. 首先,先配置一下相关参数 app/config/auth.php: model 指定模型 table 指定用户表 p ...

  5. 深度学习框架Caffe的编译安装

    深度学习框架caffe特点,富有表达性.快速.模块化.下面介绍caffe如何在Ubuntu上编译安装. 1. 前提条件 安装依赖的软件包: CUDA 用来使用GPU模式计算. 建议使用 7.0 以上最 ...

  6. FTP服务器配置部分

    构建基于虚拟用户的vsftpd服务器1.建立虚拟FTP用户的帐号数据库文件 (1) 建立虚拟用户的账户名.密码列表->奇数行为帐号名,偶数行为上一行中帐号的密码 (2) 转化为Berkeley ...

  7. chart.js在html中画曲线图

    http://www.bootcss.com/p/chart.js/docs/ http://www.chartjs.org/docs/   中有详细讲解 一.简介 Chart.js是一个基于HTML ...

  8. Apache 重启时会有报 AH00558

    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0 ...

  9. MySQL 启动、关闭、选择数据库等命令

    一.MySQL服务的启动和停止 1.net 命令来启动或停止mysql服务 net stop mysql(mysql是指你真正装的服务,如果装的是 mysql5,必须写成 net stop mysql ...

  10. UVA 10518 How Many Calls?

    题意:一个递推式第n项%b是多少. 递推式: 构造矩阵: #include<cstdio> #include<cstring> #include<cmath> #i ...