第八周 程序阅读-运算符重载

#include <iostream>
using namespace std;
class Sample
{
private:
    int x;
public:
    Sample () { }
    Sample(int a){x=a;}
    void disp(){cout<<"x="<<x<<endl;}
    friend Sample operator+( Sample &s1, Sample &s2);
};
Sample operator+( Sample &s1, Sample &s2)
{
    return Sample(s1.x+s2.x);
}

int main()
{
    Sample obj1(10);
    Sample obj2(20);
    Sample obj3;
    obj3=obj1+obj2;
    obj3.disp();
    return 0;
}

#include <iostream>
using namespace std;
class Sample
{
private:
    int x;
public:
    Sample() {}
    Sample (int a){x=a;}
    void disp(){cout<<"x="<<x<<endl;}
    Sample operator+(Sample &s);
};
Sample Sample:: operator+( Sample &s)
{
    return Sample(x+s.x);
}
int main()
{
    Sample obj1(20);
    Sample obj2(20);
    Sample obj3;
    obj3=obj1+obj2;
    obj3.disp();
    return 0;
}

#include<iostream>
using namespace std;
class Wages//“工资”类
{
    double base;//基本工资
    double bonus;//奖金
    double tax;//税金
public:
    Wages(double CBase, double CBonus,double CTax):
  base(CBase), bonus(CBonus),tax(CTax) {}
    double getPay()const;//返回应付工资额
    Wages operator+(Wages w)const;//重载加法
};
double Wages::getPay()const
{
    return base+bonus-tax;
}
Wages Wages::operator+(Wages w)const
{
    return Wages(base+w.base,
        bonus+w.bonus,tax+w.tax);
}
int main()
{
    Wages wl(2000,500,100),w2(5000,1000,300);
    cout<<(wl+w2).getPay()<<endl;
    return 0;
}

#include<iostream>
using namespace std;
class Pair
{
    int m;
    int n;
public:
    Pair(int i, int j):m(i),n(j) {}
    bool operator >(Pair p) const;
};
bool Pair::operator>(Pair p)const
{
    if (m!=p.m)
        return m>p.m;
    return n>p.n;
}
int main()
{
    Pair p1(3,4),p2(4,3), p3(4,5);
    cout<<(p1>p2)<<(p2>p1)<<(p2>p3)<<(p3>p2);
    return 0;
}

知识点总结:

类内函数和友元函数对运算符进行重载

学习心得:

好好学习 天天向上 加油

时间: 2024-07-30 05:09:51

第八周 程序阅读-运算符重载的相关文章

第九周 程序阅读-学生信息管理系统

阅读程序"简单C++学生信息管理系统",找出其中出现构造函数.友元函数.运算符重载.静态数成员语法现象出现的位置,仔细体会其用法,在以后的设计中能够灵活应用有关方法和技巧. #include <iostream> #include <cstring> using namespace std; #define MAX 100 class CDate // 定义日期类 { private: unsigned short int year; // 年 unsigned

2015级C++第12周实践项目 运算符重载(一)

[项目1-实现复数类中的运算符重载] 参考解答 (1)请用类的成员函数,定义复数类重载运算符+.-.*./,使之能用于复数的加减乘除 class Complex { public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r; imag=i;} Complex operator+(const Complex &c2); Complex operator-(const Complex &c2); Complex op

第九周 程序阅读-字符串类的设计

阅读下面的程序,领会其中用到的设计方案.技术手段与算法. /* 对于要定义的字符串类CMyString, 数据成员包括: - 字符串的长度: - 指向字符串第一个字符的指针 成员函数包括: - 不带参数的构造函数: - 带一个类型为const char *类型的参数(用于对字符串初始化)的构造函数: - 带一个const CMyString&类型的复制构造参数: - 析构函数: - Strlen函数 (用于求字符串的长度): - int Find(char c) (找出字符c在本字符串中第一次出

第15周 程序阅读-二进制文件及文件的读取1

1.阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法).查看其内容,并理解文件存储的原理. (1) #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a; ofstream outfile("f1.dat",io

第15周 程序阅读-二进制及二进制文件的读取3

3.阅读下面的程序,指出其功能,体会seekg().tellg()等函数的功能及其用法 (1) #include<iostream> #include <fstream> using namespace std; const char * filename = "a.txt"; int main () { long l,m; ifstream file (filename, ios::in|ios::binary); l = file.tellg(); file

第二周 程序阅读

#include <iostream> #include <cstring> using namespace std; class Student { private: int num; char name[20]; char sex; public: void set_data(int n, char *p,char s) { num=n; strcpy(name,p); sex=s; } void display( ) { cout<<"num: &quo

第九周-程序阅读理解

/*Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:my.cpp *作 者:张瀚文 *完成日期:2016年5月6日 * *问题描述: 阅读程序,写出程序的运行结果并理解其运行机制. */ #include <iostream> #include<cstring> using namespace std; class A { char *a; public: A(char *aa) { a=new char[s

第九周-程序阅读

/*Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:my.cpp *作 者:张瀚文 *完成日期:2016年5月6日 * *问题描述: 阅读程序,写出程序的运行结果并理解其运行机制. */ #include <iostream> #include<cstring> using namespace std; class A { char *a; public: A(A &t); A(char *aa) {

第十二周程序阅读5:基类和派生类的转换

问题及代码: #include <iostream> using namespace std; class A { protected: int a,b; public: A(int aa, int bb):a(aa), b(bb) {} void printA() { cout<<"a: "<<a<<"\tb: "<<b<<endl; } }; class B: public A { int