实验5

实验3
pets.hpp

点击查看代码
#pragma once

#include<string>
#include<iostream>
using namespace std;

class MachinePets {
private:
    string nickname;
public:
    MachinePets(const string& s)nickname(s){
    }
    string get_nickname() {
        return nickname;
    }
    virtual string talk() {
    }
};
class PetCats :public MachinePets {
public:
    PetCats(const string &s):MachinePets(s) {}
    string talk() {
        return "miao miao~";
    }
};
class PetDogs :public MachinePets {
public:
    PetDogs(const string& s) :MachinePets(s) {}
    string talk() {
        return "ao wo~";
    }
};

task4
film.hpp

点击查看代码
#pragma once

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class Film {
public:
    Film() {
    }
    Film(const string& flimName, const string& filmDirector, const string& filmMadeIn, const int& filmYear);
    friend istream& operator>>(istream& in, Film& film);
    friend ostream& operator<<(ostream& out,  Film& film);
    
private:
    string filmName;
    string filmDirector;
    string filmMadeIn;
    int filmYear;
};

Film::Film(const string& flimName, const string& filmDirector, const string& filmMadeIn, const int& filmYear) {
    this->filmName = filmName;
    this->filmDirector = filmDirector;
    this->filmMadeIn = filmMadeIn;
    this->filmYear = filmYear;
}

istream& operator>>(istream& in, Film& film) {
    cout << "录入片名:";
    in >> film.filmName;
    cout << "录入导演:";
    in >> film.filmDirector;
    cout << "录入制片国家:";
    in >> film.filmMadeIn;
    cout << "录入上映年份:";
    in >> film.filmYear;
    return in;

}
ostream& operator<<(ostream& out, Film& film) {
    out << left;
    out << setw(15) << film.filmName << setw(15) << film.filmDirector << setw(15) << film.filmMadeIn << setw(15) << film.filmYear;
    return out;
}
bool compare_by_year(Film& f1, Film& f2) {
    return f1.filmYear < f2.filmYear;
}

task5
Complex.hpp

点击查看代码
#pragma once
#include<iostream>
#include<string>
using namespace std;
template<typename T>
class Complex{
    private:
        T real,imag;
    public:
        Complex(){}
        Complex(T x,T y):real(x),imag(y){}
        Complex(const Complex& a){
            real=a.real;
            imag=a.imag;
        }
        Complex operator+=(const Complex& a){
            real+=a.real;
            imag+=a.imag;
            return *this;
        }
        friend Complex operator+(const Complex& a,const Complex& b){
            return Complex(a.real+b.real,a.imag+b.imag);
        }
        friend istream& operator>>(istream& in,Complex& a){
            in>>a.real>>a.imag;
            return in;
        }
        friend ostream& operator<<(ostream& out,const Complex& a){
            if(a.imag>=0)
            out<<a.real<<"+"<<a.imag<<"i";
            else
            out<<a.real<<"-"<<-a.imag<<"i";
        }
        T get_real(){
            return real;
        }
        T get_imag(){
            return imag;
        }
        friend bool operator==(const Complex& a,const Complex& b){
            return a.real==b.real&&a.imag==b.imag;
        }
         
};

posted @ 2024-12-08 22:16  灬灬zhang  阅读(2)  评论(0编辑  收藏  举报