先上笔记内容吧:

这次上课的内容有关

  1. 构造函数
  2. 析构函数
  3. 运算符重载
  4. return * this

  内容很细,大家好好回顾笔记再照应程序复习吧 :)

#include <iostream>

using namespace std;

class Integer {
public:
int i;
int geti () const {return this->i;}
void seti (int i) {this->i = i;}
Integer(int j = );
Integer(Integer & c);
~Integer();
}; Integer::Integer (Integer & c) { //Constructer Function
i = ;
cout << "Integer (Integer & c)" << endl;
} Integer::Integer (int j) : i(j) { //Copy Constructer Function
cout << "Integer (int j)" << endl;
} Integer::~Integer () { //Destructer Function
cout << "~Integer () " << endl;
} const Integer & fun (const Integer & in) {
cout << in.geti () << endl;
return in;
} int main (){ Integer b();
fun(b).geti();
Integer a(b); return ;
}

下面这个程序是有关运算符重载

通过 log print 可以跟踪观察到析构的顺序

同时还有引用返回、友元函数的例子

#include <iostream>

using namespace std;

class Integer {
private:
int i;
public:
int geti () const {return this->i;}
void seti (int i) {this->i = i;}
Integer(int j = );
Integer(Integer & c);
~Integer(); const Integer & operator = (const Integer & in);
friend const Integer operator + (const Integer & lhs, const Integer & rhs);
}; const Integer & Integer::operator = (const Integer & in) { //Member Function
i = in.i + ;
//return in;
return *this;
} const Integer operator + (const Integer & lhs, const Integer & rhs) { //全局函数
Integer out;
out.i = lhs.i + rhs.i;
return out;
} Integer::Integer (Integer & c) { //Constructer Function
i = ;
cout << "Integer (Integer & c)" << endl;
} Integer::Integer (int j) : i(j) { //Copy Constructer Function
cout << "Integer (int j)" << ' ' << j << endl;
} Integer::~Integer () { //Destructer Function
cout << "~Integer () " << ' ' << i << endl;
} int main (){ Integer b(), a, c, d;
cout << "a.geti()" << a.geti() << endl;
d = c = a = b;
cout << "a.geti()" << a.geti() << endl;
cout << "c.geti()" << c.geti() << endl;
cout << "d.geti()" << d.geti() << endl; d = a + b;
cout << "d.geti()" << d.geti() << endl; return ;
}

接下来运算符重载作一些附加说明

  • =
  • &
  • ,

这三个运算符可以直接使用

运算符重载的时候,如果既有 成员函数,又有全局函数, 优先(?)  

eg.

++i (前加) Integer & operator ++ ()
i++ (后加) Integer & operator ++ (int)
括号中的 int 仅仅表示后加,无其他含义

下面的这个例子可以帮助大家更好的理解

#include <iostream>

using namespace std;

class Integer {
private:
int i;
public:
int geti () const {return this->i;}
void seti (int i) {this->i = i;}
Integer(int j = 0);
Integer(Integer & c);
~Integer(); Integer & operator ++ ();
Integer operator ++ (int);
const Integer & operator = (const Integer & in);
friend const Integer operator + (const Integer & lhs, const Integer & rhs);
}; Integer & Integer::operator ++ () {
++i;
return *this;
} Integer Integer::operator ++ (int) {
Integer temp (this->i);
++temp.i;
return temp;
} const Integer & Integer::operator = (const Integer & in) { //Member Function
i = in.i;
//return in;
return *this;
} const Integer operator + (const Integer & lhs, const Integer & rhs) { //全局函数
Integer out;
out.i = lhs.i + rhs.i;
return out;
} Integer::Integer (Integer & c) { //Constructer Function
i = 18;
cout << "Integer (Integer & c)" << endl;
} Integer::Integer (int j) : i(j) { //Copy Constructer Function
cout << "Integer (int j)" << ' ' << j << endl;
} Integer::~Integer () { //Destructer Function
cout << "~Integer () " << ' ' << i << endl;
} int main (){ Integer b(8), a;
cout << b.geti () << "," << a.geti () << endl;
a = ++b;
cout << b.geti () << "," << a.geti () << endl;
a = b++;
cout << b.geti () << "," << a.geti () << endl; return 0;
}

  

面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】的更多相关文章

  1. 面向对象程序设计-C++ Finial exam review NOTES【第十六次上课笔记】

    写在前面: 我记得也不全,如果有记录的更全的同学可以留言,我会添加哒 :) 常量 内敛函数 为什么需要内敛函数 内敛函数适用于什么场合 内敛函数本身,最大优点是,避免了真正函数调用的开销 因为普通函数 ...

  2. default constructor,copy constructor,copy assignment

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  3. c++ constructor, copy constructor, operator =

    // list::push_back #include <iostream> #include <list> class element{ private: int numbe ...

  4. C++ explicit constructor/copy constructor note

    C++:explict 作用显示声明构造函数只能被显示调用从而阻止编译器的隐式转换,类似只能用()显示调用,而不能=或者隐式调用 #include <iostream> #include ...

  5. 面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】

    大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看 以下代码均已折叠,点击“+“即可打开 一开始老师用C语言大作业的例子,写了个 Student 的结构以及相关操作 #includ ...

  6. C++-copy constructor、copy-assignment operator、destructor

    本文由@呆代待殆原创,转载请注明出处. 对于一个类来说,我们把copy constructor.copy-assignment operator.move constructor.move-assig ...

  7. C++ 类 复制构造函数 The Copy Constructor

    一.复制构造函数的定义 复制构造函数是一种特殊的构造函数,具有一般构造函数的所有特性.复制构造函数创建一个新的对象,作为另一个对象的拷贝.复制构造函数只含有一个形参,而且其形参为本类对象的引用.复制构 ...

  8. 构造函数语义学之Copy Constructor构建操作(2)

    二.详述条件 3 和 4 那么好,我又要问大家了,条件1 和 2比较容易理解.因为member object或 base class 含有copy constructor.那么member objec ...

  9. 构造函数语义学之Copy Constructor构建操作(1)

    一.Copy Constructor的构建操作 就像 default constructor 一样,如果class没有申明一个 copy constructor,就会隐含的声明或隐含的定义一个.生成的 ...

随机推荐

  1. Git远程仓库的使用(三)

    1)git remote add : 添加远程仓库 git remote add origin git@github.com:用户名.仓库名.git 2) git push –u origin mas ...

  2. [LeetCode]题解(python):097-Interleaving String

    题目来源: https://leetcode.com/problems/interleaving-string/ 题意分析: 给定字符串s1,s2,s3,判断s3是否由s1和s2穿插组成.如“abc” ...

  3. Codecademy学习打卡1

    ————————————————————————— 想学习编程,并且打算“闭门造车”式的开启我的自学生涯. 前段时间买了一门厚重的“Java从入门到精通”.或许是对代码,电脑编程环境的陌生,再加上纯小 ...

  4. python自学笔记(十一)关于函数及书写格式

    1.函数是抽象的第一步       1.1 有关高压锅     1.2 函数是抽象出来的结构,是总结,是方法     1.3 多用函数     2.如何定义函数        2.1 def是关键词, ...

  5. w3wp.exe CPU过百问题

    w3wp.exe CPU过百问题 最近发布在windows  server2012  IIS8.0上的一个WebAPI项目,才几十个人在线,CPU就会出现过百情况,并且CPU一旦过百应用程序池就自动暂 ...

  6. Teclast/台电 P98HD四核测评9.7寸台电P98HD 评测体验 (转载)

    自从苹果新iPad上市推出后,拥有Retina高清屏幕分辨率的平板让我们的视线一下子变得“清晰”起来,超高2048x1536分辨率也成为厂商们追捧的对象,在经历了双核时代配备高清分辨率对于硬件性能承载 ...

  7. Nginx 的 Echo 模块 —— echo-nginx-module(转)

    Nginx 有个 echo 模块可以用来输出一些简单的信息,例如: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...

  8. sed(查找替换) 与awk(提取字段)

    通常: sed 处理列  awk处理行 比较方便 Sed是一个基本的查找替换程序     sed -i   "s/^@//g"     文件  #原地操作原文件,进行替换 cat ...

  9. ThinkPHP 3.1.2 查询方式的一般使用2

    //select id1> and id2< 默认是and $data['id']=array(array('gt',$id1),array('lt',$id2)); // $data[' ...

  10. 常用 API

    运行 Java 程序的参数.使用 Scanner 获取键盘输入.使用 BufferedReader 获取键盘输入.System类.Runtime类.Object类.Java 7新增的 Objects ...