zoukankan      html  css  js  c++  java
  • c++作业-8

    8-7

    实现++ --的运算符,同时重载前后缀

    #include<bits/stdc++.h>
    using namespace std;
    class point{
    
        public:
        point(int a=0,int b=0):x(a),y(b){    
        }
        point operator++ (){
              x++;y++;
              return *this;
        }//前置 
        point operator++(int) {
            point z=*this;
            ++(*this);
            return z;
        }
        point operator-- (){
              x--;y--;
              return *this;
        }
        point operator--(int) {
            point z=*this;
            --(*this);
            return z;
        }
        void show(){
            cout<<x<<" "<<y<<endl;
        }
    
        int x,y;
    }; 
    int main(){
        point k(1,1);
        cout<<"k++ show:"<<endl;
        (k++).show();
        cout<<"++k show:"<<endl;
        (++k).show();
        cout<<"--k show:"<<endl;
        (--k).show();
        cout<<"k-- show:"<<endl;
        (k--).show();
    
    }
    

    8-8

    观察实现虚函数及其派生的条件

    #include<bits/stdc++.h>
    using namespace std;
    class BaseClass{
        public:
            virtual void fn1(){
                cout<<"Base Class fn1"<<endl;
            }
            void fn2(){
                cout<<"Base Class fn2"<<endl;
            }
    }; 
    class DerivedClass:public BaseClass{
        public:
            void fn1(){
                cout<<"Derived Class fn1"<<endl;
            }
            void fn2(){
                cout<<"Derived Class fn2"<<endl;
            }
    };
    int main(){
      DerivedClass    k;
      BaseClass *k1=&k;
      k1->fn1();
      k1->fn2();
      DerivedClass *k2=&k;
      k2->fn1();
      k2->fn2();
    }
    

    8-10

    在point的友元函数上重载’+'

    #include<bits/stdc++.h>
    using namespace std;
    class point{
        public :
        point(int a=0,int b=0):x(a),y(b){
        }
        friend point operator+( point k1, point k2);
        void show();
        private :
            int x,y;
    }; 
    void point::show(){
        cout<<x<<" "<<y<<endl;
    }
    point operator+( point k1, point k2){
        return point(k1.x+k2.x,k1.y+k2.y);
    }
    int main(){
        point k1(1,2),k2(2,3),k3;
        k3=k1+k2;
        k1.show();
        k2.show();
        k3.show();
    }
    
  • 相关阅读:
    centos 下查找软件安装在哪里的命令
    Ubuntu常用命令大全
    linux下vi命令大全
    查看linux系统版本命令
    Linux系统安装时分区的选择(推荐)
    Java subList的使用
    Java中unicode增补字符(辅助平面)相关用法简介
    Java编码方式再学
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 283. Move Zeroes
  • 原文地址:https://www.cnblogs.com/nowheretrix/p/8982880.html
Copyright © 2011-2022 走看看