zoukankan      html  css  js  c++  java
  • 运算符重载_继承_多态_模版

    1:运算符重载

     

     

     

      1 RMB.h
      2 #ifndef RMB_H
      3 #define RMB_H
      4 
      5 #include <bits/stdc++.h>
      6 using namespace std;
      7 class RMB
      8 {
      9     public:
     10         RMB();
     11         RMB(int, int);
     12         virtual ~RMB();
     13         void display()
     14         {
     15             cout << "RMB:" << yuan << ".";
     16             if(jf < 10)
     17             {
     18                 cout << "0";
     19             }
     20             cout << jf << endl;
     21         }
     22         friend RMB operator+(const RMB&, const RMB&);//作为友元函数的元素符重载
     23         friend bool  operator>(const RMB&, const RMB&);
     24         int Getjf() { return jf; }
     25         void Setjf(int val) { jf = val; }
     26         int Getyuan() { return yuan; }
     27         void Setyuan(int val) { yuan = val; }
     28         RMB operator+(const RMB& s)//作为成员函数的运算符重载
     29         {
     30             int c = this->jf + s.jf;
     31             int d = this->yuan + s.yuan;
     32             return RMB(c,d);
     33         }
     34     protected:
     35 
     36     private:
     37         int jf;
     38         int yuan;
     39 };
     40 
     41 #endif // RMB_H
     42 RMB.cpp
     43 /*
     44  * 文件名:
     45  * 描  述:
     46  * 作  者:
     47  * 时  间:
     48  * 版  权:
     49  */#include "RMB.h"
     50 #include <bits/stdc++.h>
     51 using namespace std;
     52 RMB::RMB()
     53 {
     54     cout << "RMB construction" << endl;
     55 }
     56 RMB::RMB(int _yuan, int _jf):yuan(_yuan),jf(_jf)
     57 {
     58     cout << "RMB(int, int) construction" << endl;
     59 }
     60 RMB::~RMB()
     61 {
     62     cout << "RMB destruction"  << endl;
     63 }
     64 
     65 RMB operator+(const RMB& val1,const RMB& val2)//重载加运算符
     66 {
     67     int jf = val1.jf + val2.jf;
     68     int yuan = val1.yuan + val2.yuan;
     69     if(jf > 99){
     70         yuan++;
     71         jf-=100;
     72     }
     73     return RMB(yuan, jf);//局部对象不可以返回引用
     74 }
     75 
     76 bool operator>(const RMB& val1, const RMB& val2)//判断大小
     77 {
     78     bool ret =false;
     79     if(val1.yuan > val2.yuan)
     80     {
     81         ret = true;
     82     }else if(val1.yuan == val2.yuan){
     83         if(val1.jf > val2.jf)
     84             {
     85                 ret  = true;
     86             }
     87     }
     88     return ret;
     89 }
     90 
     91 main.cpp
     92 #include <iostream>
     93 #include "RMB.h"
     94 using namespace std;
     95 
     96 int main()
     97 {
     98 
     99     RMB rmb1(12,13);
    100     RMB rmb2(9,9);
    101     cout << (rmb1 > rmb2) << endl;
    102     RMB rmb3 = rmb1 + rmb2;
    103     cout << rmb3.Getyuan() << "  " << rmb3.Getjf() << endl;
    104     rmb3.display();
    105     return 0;
    106 }
    View Code

     

    可以进行运算符重载的运算符

    不可以进行运算符重载的运算符 

     

     更详细的的运算符重载的内容可以点击这篇博客

    2:继承

     

     

     

     

     关于继承的详细知识可以看这篇博客

     关于继承的基础知识可以看这篇博客

     1 Animal.h
     2 #ifndef ANIMAL_H
     3 #define ANIMAL_H
     4 
     5 #include<bits/stdc++.h>
     6 using namespace std;
     7 class Animal
     8 {
     9     public:
    10         Animal();
    11         virtual ~Animal();
    12         virtual void bark()
    13         {
    14             cout << "动物发出叫声" << endl;
    15         }
    16         unsigned int Getheight()const { return height; }
    17         void Setheight(unsigned int val) { height = val; }
    18 
    19     protected:
    20 
    21     private:
    22         unsigned int height;
    23 };
    24 class dog:public Animal//单继承
    25 {
    26 public:
    27     dog(){};
    28     dog(unsigned int height,unsigned int weight):weight(weight)
    29     {
    30         Setheight(height);
    31        // height = height;
    32         //weight = weight;
    33     }
    34     unsigned int getWeight()const
    35     {
    36         return weight;
    37     }
    38     void setWeight(int w){
    39       weight = w;
    40     }
    41     virtual void bark()
    42     {
    43         cout << "狗儿汪汪叫" << endl;
    44     }
    45 private:
    46     unsigned int weight;
    47 };
    48 class littleDog : public Animal,public dog//多继承
    49 {
    50 public:
    51     littleDog(unsigned int height,unsigned int weight)
    52     {
    53         setWeight(weight);
    54     }
    55 private:
    56 
    57 };
    58 #endif // ANIMAL_H
    59 Animal.cpp
    60 /*
    61  * 文件名:
    62  * 描  述:
    63  * 作  者:
    64  * 时  间:
    65  * 版  权:
    66  */#include "Animal.h"
    67 
    68 Animal::Animal()
    69 {
    70     //ctor
    71 }
    72 
    73 Animal::~Animal()
    74 {
    75     //dtor
    76 }
    77 main.cpp
    78 #include <iostream>
    79 
    80 using namespace std;
    81 #include <Animal.h>
    82 int main()
    83 {
    84     dog d(70,70);
    85     d.bark();//虚函数,重写父类版本
    86     d.Animal::bark();//强制调用father中定义的版本
    87     cout << d.Getheight() << d.getWeight() << endl;
    88     //d.dogbark();
    89     littleDog  ld(12,12);
    90     cout << ld.getWeight() << endl;
    91     return 0;
    92 }
    View Code

    3:多态

     

    这篇博客讲了多态的基础

    这一篇做了很好的总结 

    4:类模版

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 template <class T>
     4 class Stack
     5 {
     6 public:
     7     Stack():top(0)
     8     {
     9         array[top] = 0;
    10     }
    11     void push(const T&i);
    12     T pop();
    13 private:
    14     enum{SIZE = 100};
    15     T array[SIZE];
    16     int top;
    17 };
    18 template<typename T>
    19 void Stack<T>::push(const T& val)
    20 {
    21     if(top < SIZE){
    22         array[top++] = val;
    23     }
    24 }
    25 template<typename T>
    26 T Stack<T>::pop()
    27 {
    28     if(top > 0)
    29     return array[--top];
    30     else array[0];
    31 }
    32 int main()
    33 {
    34     Stack<int> a;
    35     a.push(1);
    36     a.push(3);
    37     a.push(2);
    38     cout << a.pop() << endl;
    39     cout << a.pop() << endl;
    40     cout << a.pop() << endl;
    41 }

    提高了代码的复用性

  • 相关阅读:
    leetcode 48. Rotate Image
    leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点) 、26/80. Remove Duplicates from Sorted ArrayI、II
    leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
    leetcode 58. Length of Last Word
    安卓操作的一些问题解决
    leetcode 378. Kth Smallest Element in a Sorted Matrix
    android studio Gradle Build速度加快方法
    禁用gridview,listview回弹或下拉悬停
    Android Studio找不到FragmentActivity类
    安卓获取ListView、GridView等滚动的距离(高度)
  • 原文地址:https://www.cnblogs.com/henuliulei/p/11716199.html
Copyright © 2011-2022 走看看