系统学习C++(4)
来源:互联网 发布:淘宝新开店铺 编辑:程序博客网 时间:2024/05/17 08:52
构造函数的访问顺序
(1) 子类创建对象时
当从父类派生一个子类并声明一个子类的对象时,它将先调用父类的构造函数,然后调用当前类的构造函数来创建对象;在释放子类对象时,先调用的是当前类的析构函数,然后是父类的析构函数。
下面简单用程序展示这一点:
Father.h
#include <iostream>using namespace std;class Father{public: Father() { cout << "Create the Father!" <<endl; } ~Father() { cout << "Release the Father!" <<endl; }};class Son:public Father{public: Son() { cout << "Create the Son!" <<endl; } ~Son() { cout << "Release the Son!" << endl; }};
main.cpp
#include "Father.h"int main(){ Son s; return 0;}
运行结果:
(2) 定义一个基类类型的指针,调用子类的构造函数为其构建对象,当释放对象时,如果析构函数是虚函数,则先调用子类的析构函数,然后再调用父类的析构函数;如果不是析构函数,则只调用父类的析构函数。
含有虚函数的情况:
#include <iostream>using namespace std;class Father{public: Father() { cout << "Create the Father!" <<endl; } virtual ~Father() { cout << "Release the Father!" <<endl; }};class Son:public Father{public: Son() { cout << "Create the Son!" <<endl; } ~Son() { cout << "Release the Son!" << endl; }};
#include "Father.h"int main(){ Father *f = new Son(); // 定义基类指针,调用子类的构造函数 f->~Father(); return 0;}
运行结果:
没有虚函数的情况:
#include <iostream>using namespace std;class Father{public: Father() { cout << "Create the Father!" <<endl; } ~Father() { cout << "Release the Father!" <<endl; }};class Son:public Father{public: Son() { cout << "Create the Son!" <<endl; } ~Son() { cout << "Release the Son!" << endl; }};
运行结果:
练习:开发一个程序,定义一个类,包含“商品”和“售价”两个成员变量,要求重载运算符"
#include <iostream>using namespace std;class Buy{public: int goods; float price; friend ostream& operator <<(ostream& out,const Buy& buy) { out << "good:" << buy.goods << " " <<"price: "<< buy.price; return out; } Buy(int m1,float m2) { goods = m1; price = m2; }};int main(){ Buy b1(2,20.7); cout << b1<<endl;;}
运行结果:
总结:
此处使用友元,因为重载输出运算符第一个形参必须是ostream类,而成员函数的第一个形参是默认的(this指针),而本类又需要用到ostream类中属性,则需要friend。重载输入运算符也是同理。
同时,一般对于重载输出运算符,第二个形参一般是const,因为输出的一个类不需要更改它;
exp: ostream & operator << (ostream& out, const classT& obj)
而重载输入运算符的第二个形参,则必须是非const,否则无法赋值。
exp: istream & operator >> (istream& in, classT& obj )
阅读全文
0 0
- 系统学习C++(4)
- 系统学习C++(1)
- 系统学习C++(2)
- 日志系统开发学习总结(C#)
- C++IO系统学习
- 系统学习C语言
- 深入系统学习C/C++
- 从零开始系统学习C/C++
- 系统编程(文件操作)---C语言学习(2)
- C语言学习系统的数据库设计(原创)
- 破解C语言学习系统 v3.0
- linux系统C语言学习总结
- Linux系统C语言学习总结
- linux系统C语言学习总结
- C学习笔记 --- 学生信息管理系统
- 今天开始系统学习linux、c
- c语言学生信息管理系统学习感想
- μc/osII 系统学习 - 任务
- 【洛谷】P1582倒水
- tEST 1 for NOIP 2017.9.9.
- 1032 链表
- leetcode 171. Excel Sheet Column Number(C语言)
- 数据库学习——聚合函数
- 系统学习C++(4)
- Fiddler Script 优化
- Cointainer With Most Water
- hdu5289ST表+二分
- shell流程控制
- A、B、C、D、E类IP
- Map.Entry使用详解
- [实践]自行车租赁预测
- py-faster-rcnn在Windows下的end2end训练