#include <iostream>
#include "pets.hpp"

void play(MachinePets &obj){
    std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
}

void test() {
    PetCats cat("miku");
    PetDogs dog("da huang");

    play(cat);
    play(dog);
}

int main() {
    test();
}
#pragma once
#include<iostream>
using namespace std;

class MachinePets {
public:
    MachinePets(const string s):nickname(s){}
    virtual string talk() { return voice; }
    virtual string get_nickname() { return nickname; }
protected:
    string nickname;
    string voice;
};

//宠物猫
class PetCats :public MachinePets {
public:
    PetCats(const string s) :MachinePets(s) {  voice="miao~"; }
    string talk() { return voice; };
    string get_nickname() { return nickname; }
    
};

//宠物狗
class PetDogs :public MachinePets {
public:
    PetDogs(const string s) :MachinePets(s) { voice = "ao wu~"; }
    string talk() { return voice; }
    string get_nickname() { return nickname; }
    
};

 

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class Person {
public:
    Person(){}
    Person(string name0,string telephone0,string email0) :name(name0), telephone(telephone0),email(email0) {}
    Person(const Person& p1);
    void update_telephone();
    void update_email();

    friend std::ostream& operator << (std::ostream& out, const Person& c);
    friend std::istream& operator >> (std::istream& in, Person& c);
    friend bool operator==(const Person& c1, const Person& c2);
private:
    string name;
    string telephone;
    string email;
};
Person::Person(const Person& pl) {
    name = pl.name;
    telephone = pl.telephone;
    email = pl.email;
}
void Person::update_telephone() {
    cout << "Enter the telephone number:";
    cin.clear();
    cin >> telephone;
    cout << "telephone number has been updated..." << endl;
}
void Person::update_email() {
    cout << "Enter the email address:";
    cin.clear();
    cin >> email;
    cout << "email address has been updated..." << endl;
}
bool operator==(const Person& c1,const Person &c2) {
    return(c1.name == c2.name && c1.telephone == c2.telephone && c1.email == c2.email);
}
std::ostream& operator << (std::ostream& out, const Person& c) {
    out << left << setw(15) << c.name << setw(15) << c.telephone << setw(15) << c.email;
    return out;
}
std::istream& operator >> (std::istream& in, Person& c) {
    getline(in, c.name);
    getline(in,c.telephone);
    getline(in, c.email);
    cout << endl;
    return in;
}
#include <iostream>
#include <fstream>
#include <vector>
#include "Person.hpp"

void test() {
    using namespace std;

    vector<Person> phone_book;
    Person p;

    cout << "Enter person's contact until press Ctrl + Z" << endl;
    while (cin >> p)
        phone_book.push_back(p);

    cout << "\nupdate someone's contact: \n";
    phone_book.at(0).update_telephone();
    phone_book.at(0).update_email();

    cout << "\ndisplay all contacts' info\n";
    for (auto& phone : phone_book)
        cout << phone << endl;

    cout << "\ntest whether the same contact\n";
    cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;
}

int main() {
    test();
}

 

//=======================
//        main.cpp
//=======================

// main function for the RPG style game

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

#include "swordsman.h"


int main()
{
    string tempName;
    bool success = 0;        //flag for storing whether operation is successful
    cout << "Please input player's name: ";
    cin >> tempName;        // get player's name from keyboard input
    player* human;        // use pointer of base class, convenience for polymorphism
    int tempJob;        // temp choice for job selection
    do
    {
        cout << "Please choose a job: 1 Swordsman, 2 Archer, 3 Mage" << endl;
        cin >> tempJob;
        system("cls");        // clear the screen
        switch (tempJob)
        {
        case 1:
            human = new swordsman(1, tempName);    // create the character with user inputted name and job
            success = 1;        // operation succeed
            break;
        default:
            break;                // In this case, success=0, character creation failed
        }
    } while (success != 1);        // so the loop will ask user to re-create a character

    int tempCom;            // temp command inputted by user
    int nOpp = 0;                // the Nth opponent
    for (int i = 1; nOpp < 5; i += 2)    // i is opponent's level
    {
        nOpp++;
        system("cls");
        cout << "STAGE" << nOpp << endl;
        cout << "Your opponent, a Level " << i << " Swordsman." << endl;
        system("pause");
        swordsman enemy(i, "Warrior");    // Initialise an opponent, level i, name "Junior"
        human->reFill();                // get HP/MP refill before start fight

        while (!human->death() && !enemy.death())    // no died
        {
            success = 0;
            while (success != 1)
            {
                showinfo(*human, enemy);                // show fighter's information
                cout << "Please give command: " << endl;
                cout << "1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game" << endl;
                cin >> tempCom;
                switch (tempCom)
                {
                case 0:
                    cout << "Are you sure to exit? Y/N" << endl;
                    char temp;
                    cin >> temp;
                    if (temp == 'Y' || temp == 'y')
                        return 0;
                    else
                        break;
                case 1:
                    success = human->attack(enemy);
                    human->isLevelUp();
                    enemy.isDead();
                    break;
                case 2:
                    success = human->specialatt(enemy);
                    human->isLevelUp();
                    enemy.isDead();
                    break;
                case 3:
                    success = human->useHeal();
                    break;
                case 4:
                    success = human->useMW();
                    break;
                default:
                    break;
                }
            }
            if (!enemy.death())        // If AI still alive
                enemy.AI(*human);
            else                            // AI died
            {
                cout << "YOU WIN" << endl;
                human->transfer(enemy);        // player got all AI's items
            }
            if (human->death())
            {
                system("cls");
                cout << endl << setw(50) << "GAME OVER" << endl;
                delete human;        // player is dead, program is getting to its end, what should we do here?
                    system("pause");
                return 0;
            }
        }
    }
    delete human;            // You win, program is getting to its end, what should we do here?
        system("cls");
    cout << "Congratulations! You defeated all opponents!!" << endl;
    system("pause");
    return 0;
}

 

 

 

posted on 2022-11-28 21:56  胡嘉瑞  阅读(17)  评论(0)    收藏  举报