回顾:
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. 单词接龙(dragon)

    单词接龙(dragon) 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次) ...

  2. tset

    test fsdfs jsjf sdfsdfsdfsdf sfs fsfsfs test112 sdfs sdfsdf sdfs sf af s sdf sadfasdfsa asfasf sdfsa ...

  3. 用free -m查看的结果:

    用free -m查看的结果: # free -m          total    used    free     shared buffers     cached Mem:           ...

  4. FMDB 查询是否存在某个表,或某个数据

    //监测数据库中我要需要的表是否已经存在 NSString *existsSql = [NSString stringWithFormat:@"select count(name) as c ...

  5. posix第二篇-----linux 锁机制

    1 简介 锁机制(lock) 是多线程编程中最常用的同步机制,用来对多线程间共享的临界区(Critical Section) 进行保护. Pthreads提供了多种锁机制,常见的有: 1) Mutex ...

  6. http://www.linux-commands-examples.com/xmllint

    http://www.linux-commands-examples.com/xmllint hen hao!

  7. angularJS 系列(一)

    angularJS 中 $scope 的原型 和 原型继承之间的细微差别是什么? 参考:http://stackoverflow.com/questions/14049480/what-are-the ...

  8. CDOJ 1273 God Qing's circuital law

    暴力枚举+idea.做的时候mod写错了,写成了1000000009,找了两个多小时才发现...... a[1],a[2],a[3]....a[N] b[1],b[2],b[3]....b[N] 首先 ...

  9. Apache2 三种MPM对比分析

    就最新版本的Web服务器Apache(版本是Apache 2.4.10,发布于2014年7月21日)来说,一共有三种稳定的MPM(Multi-Processing Module,多进程处理模块)模式. ...

  10. MAC平台下mysql7.5的安装

    1.下载mysql(DMG格式64位的版本) http://dev.mysql.com/downloads/mysql/ 2.安装mysql 待下载*.dmg文件后双击,运行该安装文件 3.无限下一步 ...