回顾:
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. MyEclipse9,MyEclipse10 安装ADT

    Eclipse安装ADT 时步骤是开 Eclipse IDE,进入菜单中的 "Help" -> "Install New Software" ,点击Add ...

  2. JS读RSS

    <html>  <head>   <title>javascript读取RSS数据</title>   <META content="t ...

  3. 64位win7系统下面安装pyOpenSSL

    原文地址:http://blog.csdn.net/xinsuixiaofeiyu/article/details/40350067 最近想装一个scrapy玩下爬虫,虚拟机Linux下面倒是很好安装 ...

  4. Using StructureMap DI and Generic Repository

    In this post, i will show how to use generic repository and dependency injection using structuremap. ...

  5. how to enable #ifdef macro in the command line of make?

    Compilers normally use the -D flags eg Code: test.o: test.cpp $(CC) $(CFLAGS) -DTEST1 test.cpp -o $@

  6. treeview 与tabControl组合使用

    1.左边一个treeview,右边一个tabcontrol: 2.调整控件让tree在tabcontrol上,并让treeview压住tab页 3.将tab页的name设置成treeview的node ...

  7. 操作IFRAME及元素

    内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe id="mainiframe..& ...

  8. JS基础知识——定时器

    1.定时器——setInterval("执行函数名",间隔时间); setInterval(fun,1000);每隔1秒钟执行一次fun函数 setInterval("f ...

  9. PHP安装插件方式

    PHP安装插件方法主要有两种: 1.先安装相关的库,zlib.curl.xml等,然后在安装 php 时的 ./configure 中设置 --with-xxx(你需要的插件),三部曲安装即可. 2. ...

  10. (简单) POJ 2406 Power Strings,扩展KMP。

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...