先上笔记内容吧:

这次上课的内容有关

  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. Python主要模块和常用方法简览

    原文地址:http://blog.csdn.net/hwhjava/article/details/22284399 PY核心模块方法1. os模块: os.remove() #删除文件 os.unl ...

  2. linux如何关闭selinux?

    首先我们可以用命令来查看selinux的状态getenforce 这个命令可以查看到selinux的状态,当前可以看到是关闭状态的.还有一个命令也可以查看出selinux的状态.sestatus -v ...

  3. MyEclipse 8.5整合Git,并在Github上发布项目(转)

    下载Eclipse的git插件——EGit.下载网址http://download.eclipse.org/egit/updates-1.3/org.eclipse.egit-updatesite-1 ...

  4. Linux的默认编码可以通过export LC_ALL=zh_CN.GBK来修改

    http://www.cnblogs.com/malecrab/p/5300486.html

  5. Qt之界面美化输入框(使用QSS确实非同凡响)

    我们先看下酷狗音乐的输入框效果图: 这里我们需要实现在输入框里面添加一个按钮(多个也一样),并且提供一个默认时候的文字..直接上代码好了. QHBoxLayout *lay = new QHBoxLa ...

  6. 在SQL2005中部署CLR 程序集

    原文 在SQL2005中部署CLR 程序集 有关于CLR函数的用途和用法,请了解 SQL Server CLR 极速入门,启用.设计.部署.运行 http://www.yongfa365.com/It ...

  7. Android开发中怎样调用系统Email发送邮件(多种调用方式)

    在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...

  8. swjtu 1962 A+B(模拟)

    题目链接:http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1962 问题思路:考察编程基础的问题,涉及到字符串转为数字的问题. 代 ...

  9. Flex 全屏显示方法

    1,修改html-template下的index.template.html文件…增加四行 1</html> 上述文件增加了四行…见我文中有提示 2,Mxml文件: 假如一个button按 ...

  10. 【Java线程】Lock、Condition

    http://www.infoq.com/cn/articles/java-memory-model-5  深入理解Java内存模型(五)——锁 http://www.ibm.com/develope ...