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;
}
C++第四天学习的更多相关文章
- apue第四章学习总结
apue第四章学习总结 4.1.若以stat函数去替换lstat函数,会发生: 原来的目录路径: $:~/workspace/apue2/include$ ls -l apue.h abc lrwxr ...
- Factorization Machines 学习笔记(四)学习算法
近期学习了一种叫做 Factorization Machines(简称 FM)的算法.它可对随意的实值向量进行预測.其主要长处包含: 1) 可用于高度稀疏数据场景:2) 具有线性的计算复杂度.本文 ...
- 《Linux内核设计与实现》第四章学习笔记
<Linux内核设计与实现>第四章学习笔记 ——进程调度 姓名:王玮怡 学号:20135116 一.多任务 1.多任务操作系统的含义 多任务操作系统就是能同时并发地交 ...
- 《Linux内核设计与实现》第四章学习笔记——进程调度
<Linux内核设计与实现>第四章学习笔记——进程调 ...
- Spring实战第四章学习笔记————面向切面的Spring
Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...
- 孤荷凌寒自学python第六十四天学习mongoDB的基本操作并进行简单封装3
孤荷凌寒自学python第六十四天学习mongoDB的基本操作并进行简单封装3 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第十天. 今天继续学习mongoDB的简单操作, ...
- 《码出高效:Java开发手册》第四章学习记录,内容想当的多,前后花了几天的时间才整理好。
<码出高效:Java开发手册>第四章学习记录,内容想当的多,前后花了几天的时间才整理好. https://naotu.baidu.com/file/e667435a4638cbaa15eb ...
- 鸟哥的linux私房菜——第四章学习
******************第四章学习****************** [热键] 1.Tab键:命令补全:文件补全: 2.Ctrl+c:中断目前指令: 3.Ctrl+d:离开当前文本界面: ...
- Day4 《机器学习》第四章学习笔记
决策树 前几天学习了<机器学习>的前三章,前三章介绍机器学习的基础知识,接下来,第四章到第十章介绍一些经典而常用的机器学习方法,这部分算是具体的应用篇,第四章介绍了一类机器学习方法——决策 ...
- 第三周学习java第四章学习总结及体会!
第三周java 2第四章的学习总结: 一.主要内容(类与对象): 1.类: 2.构造方法与对象的创建: 3.类与程序的基本结构: 4.参数传值: 5.对象的组合: 6.实例成员与类成员: 7.方法重载 ...
随机推荐
- CentOS 6.5搭建Samba服务器
目标需求:在Windows7下访问CentOS 6.5 root用户桌面/ZS文件夹 0.准备工作 关闭防火墙并开启不起动 service iptables stop chkconfig iptabl ...
- Hadoop: failed on connection exception: java.net.ConnectException: Connection refuse
ssh那些都已经搞了,跑一个书上的例子出现了Connection Refused异常,如下: 12/04/09 01:00:54 INFO ipc.Client: Retrying connect t ...
- 转:WebTest的常见问题与解决
WebTest的常见问题与解决录制好一个WebTest,加上各种规则,编辑后运行并不会像我们想象的那么顺利成功,往往会碰到很多问题,运行不成功的情况比较多,这样我们就遇到了如何解决这些问题的情形.1. ...
- Maven项目中提示:Eclipse “cannot be resolved to a type” error
我的解决办法是这个: 临时解决方法是:Clean项目 ********百度到的其他解决办法,统一归纳此处****************** (1)jdk不匹配(或不存在) 项目指定的jdk为“jdk ...
- Codeforces Round #369 (Div. 2) D. Directed Roads (DFS)
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- oracle 主键自动增长
oracle 主键自动增长 2009-12-11 16:07:00| 分类: 数据库资料|字号 订阅 这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下: create tabl ...
- ural1521 War Games 2
War Games 2 Time limit: 1.0 secondMemory limit: 64 MB Background During the latest war games (this s ...
- Linux的iptables常用配置范例(2)
iptables -F #清除所有规则 iptables -X #清除所有自定义规则 iptables -Z #各项计数归零 iptables -P INPUT DROP #将input链 ...
- PDF 补丁丁 0.5 正式版发布
经过了两年的测试,新版本的 PDF 补丁丁已经比较稳定了.在农历新年前发布这个 0.5 版,作为正式稳定版吧. 新的 PDF 补丁丁比旧的 0.3 版增加了许多功能: PDF 可视化编辑文档书签,可从 ...
- tableView滑动到底部
- (void)scrollToBottom { NSInteger sectionCount = [self.dataSource numberOfSectionsInTableView:self] ...