zoukankan      html  css  js  c++  java
  • Modern C++ Course [Lecture 5] {Move Semantics, Classes}

    here, we're just creating a class Square inherited from class Rect.

    compiler calls only Square.Print()

    now we would really like to see what is polymorphism, so we create a reference here.

    Result is still "square" here.

    let's remove the virtual

    First we defined a Square object "square", and square.Print() called Square's Print().

    Then we defined a reference Rect object of square, then compiler would regard rect_ref as a Rect object, and call Rect.Print().

     if we change a way to represent the Print() in using another function outside the class definition, in which way we don't need to write print function for every object.

     will receive errors here.

     but adding "virtual" and "override" can magically avoid the problem.

     if we dont add virtual here, the output will always be:

    rect 10 10

    rect 10 20

    rather than:

    square 10 10

    square 10 20

    I think the idea behind virtual is that designers of C++ didn't want to waste time writing functions for classes inherited from this parental class. A reference would be efficient.

     

    delete the "const" will have error, because it's overload rather than override 

     

     polymorphism only works for reference. dont remove the & !

     

    Let's summarize the two important points for polymorphism:

    1,  virtual function in parental class

    2, use reference to represent objects in the interface definition

     pure virtual func:

     struct is just class with only public

     

     

    just F.Y.I. 

     

     

    in operation will ignore the white place

    input is an object of ifstream

    getline is actually std::getline

    output: define ofstream object "out", and out << 

    input: define ifstream object "in", and in >>

     

     

  • 相关阅读:
    Android 编辑框(EditText)属性学习
    读书笔记 -《高效程序猿的45个习惯-敏捷开发修炼之道》
    Launcher3实现壁纸居中
    Trie树的常见应用大总结(面试+附代码实现)
    LeetCode Summary Ranges
    abap选择屏幕上的button
    SQLite Expert表分离和解决SQLite Expert删除表后大小不变的问题
    git push 失败
    动态限制EdiText仅仅能输入特定字符
    DateTime类常用技巧摘录
  • 原文地址:https://www.cnblogs.com/ecoflex/p/10195189.html
Copyright © 2011-2022 走看看