読者です 読者をやめる 読者になる 読者になる

個人的自由ノート

ふと自分が気になった事を書いてます

C++で問題を解く

#include <string>
#include <iostream>

using namespace std;

class Human {
public:
    string name;
    int height;
    int weight;
};

int main(void){
    Human nakano;
    Human morita;

    nakano.name = "中野太郎";
    nakano.height = 173;
    nakano.weight = 62;

    morita.name = "森田孝司";
    morita.height = 168;
    morita.weight = 78;

    cout << "nakano = " << nakano.name << " " << nakano.height << "cm" << " " << nakano.weight << "kg" << endl;
    cout << "morita = " << morita.name << " " << morita.height << "cm" << " " << morita.weight << "kg" << endl;

    return 0;
}


#include <string>
#include <iostream>

using namespace std;

class Human {
private:
    string full_name;
    int height;
    int weight;
public:
    Human(string name, int h, int w){
        this->full_name = name;
        this->height = h;
        this->weight = w;
    }

    string name(){
        return this->full_name;
    }

    int get_height(){
        return this->height;
    }

    int get_weight(){
        return this->weight;
    }

    void grow_fat(int dw){
        this->weight += dw;
    }

    void slim_off(int dw){
        this->weight -= dw;
    }
};

int main(void){
    Human nakano("中野太郎", 173, 65);
    Human morita("森田孝司", 168, 71);

    nakano.slim_off(3);
    morita.grow_fat(7);

    cout << "nakano = " << nakano.name() << " " << nakano.get_height() << "cm " << nakano.get_weight() << "kg" << endl;
    cout << "morita = " << morita.name() << " " << morita.get_height() << "cm " << morita.get_weight() << "kg" << endl; 
    return 0;
}


// q10-2.h
#include <string>
#include <iostream>

class Human {
private:
    std::string full_name;
    int height;
    int weight;
public:
    Human(std::string name, int h, int w);

    std::string get_name() { return this->full_name; }
    int get_height() { return this->height; }
    int get_weight() { return this->weight; }

    void grow_fat(int dw);
    void slim_off(int dw);
};


#include "q10-2.h"

using namespace std;

Human::Human(std::string name, int h, int w){
    this->full_name = name;
    this->height = h;
    this->weight = w;
}

void Human::grow_fat(int dw){
    this->weight += dw;
}

void Human::slim_off(int dw){
    this->weight -= dw;
}

int main(void){
    Human nakano("中野太郎", 173, 65);
    Human morita("森田孝司", 168, 71);

    nakano.slim_off(3);
    morita.grow_fat(7);

    cout << "nakano = " << nakano.get_name() << " " << nakano.get_height() << "cm " << nakano.get_weight() << "kg" << endl;
    cout << "morita = " << morita.get_name() << " " << morita.get_height() << "cm " << morita.get_weight() << "kg" << endl; 

    return 0;
}



#include "q10-2.h"

using namespace std;

Human::Human(std::string name, int h, int w){
    this->full_name = name;
    this->height = h;
    this->weight = w;
}

void Human::grow_fat(int dw){
    this->weight += dw;
}

void Human::slim_off(int dw){
    this->weight -= dw;
}

void print_human(string title, Human* p){
    cout << title << p->get_name() << " " << p->get_height() << "cm " << p->get_weight() << "kg" << endl;
}

int main(void){
    Human nakano("中野太郎", 173, 65);
    Human morita("森田孝司", 168, 71);

    nakano.slim_off(3);
    morita.grow_fat(7);

    print_human("nakano = ", &nakano);
    print_human("morita = ", &morita);

    return 0;
}