C++ is going to pass sooner or later, for the sake of JNI, or pick up a little bit.

I want to use the simplest things to tell the most complex truth, and use the most interesting way to guide the most boring you.


Preface:This story is pure fiction

Background In 1988, Long Shao took an interview with Xingyu International because he had printed “Hello, World! And shock everyone and get an offer right away.

Lin Xi,20 (Difficulty making choices, constantly changing demands) Long Shao,20 (junior engineer who can print out sentences) Jet,21 (Intermediate engineer with object-oriented thinking)


1. Print statements

Jet: how are you getting on with C++, longshao? Longshao: It feels good. Now I can print Hello,World. Jet: Good

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return0; } ---->[print]---- Hello, World!Copy the code

Lin xi: draw a face for me with C++ long shao: give you a pout face

int main() {
    std::cout << "-- -- -- -- -- -- -- -- -- --" << std::endl;
    std::cout << "| -- - |" << std::endl;
    std::cout << ".. | |" << std::endl;
    std::cout << "| | -}" << std::endl;
    std::cout << "-- -- -- -- -- -- -- -- -- --" << std::endl;
    return 0;
}
Copy the code

Lin Xi: not very good-looking, change – to ~, let me Chou Long shao: OK, this is simple, a character change is OK

int main() {
    std::cout << "~ ~ ~ ~ ~ ~ ~ ~ ~ ~" << std::endl;
    std::cout << "| | ~ ~" << std::endl;
    std::cout << ".. | |" << std::endl;
    std::cout << "| | -}" << std::endl;
    std::cout << "~ ~ ~ ~ ~ ~ ~ ~ ~ ~" << std::endl;
    return 0;
}
Copy the code

Lin Xi: forget it, change back, so good rub dragon little: you are not playing laozi? One character at a time… TM is trouble

int main() {
    std::cout << "-- -- -- -- -- -- -- -- -- --" << std::endl;
    std::cout << "| -- - |" << std::endl;
    std::cout << ".. | |" << std::endl;
    std::cout << "| | -}" << std::endl;
    std::cout << "-- -- -- -- -- -- -- -- -- --" << std::endl;
    return 0;
}
Copy the code

Lin Xi: I’ve thought about it. Aesthetically, it would be better to use a wave on the top, a straight line on the bottom and a wave on the eyebrows. Longshao: elder brother, what do you want to do? You are very tired. Lin Xi: Not bad. Let’s have a lunch break and talk about it in the afternoon.

int main() {
    std::cout << "~ ~ ~ ~ ~ ~ ~ ~ ~ ~" << std::endl;
    std::cout << "| | ~ ~" << std::endl;
    std::cout << ".. | |" << std::endl;
    std::cout << "| | -}" << std::endl;
    std::cout << "-- -- -- -- -- -- -- -- -- --" << std::endl;
    return 0;
}
Copy the code

At noon, Long Shao angrily told Jet Jet about this matter: if it is 100 lines up and down, can you change it one by one? Longshao: if so, I only write once, who let me change, I with who urgent. Jet: Well, young people are fickle. I’m going to teach you how to extract variables and for loops


2. The for loop

Under Jet’s guidance, Major General Dragon refactored the code with a for loop

#include <iostream>
using namespace std;

int main() {
    auto top="~";
    for(int i = 0; i < 10; ++i) { i! = 9? cout << top: cout << top<<endl; } std::cout <<"| | ~ ~" << std::endl;
    std::cout << ".. | |" << std::endl;
    std::cout << "| | -}" << std::endl;
    std::cout << "~ ~ ~ ~ ~ ~ ~ ~ ~ ~" << std::endl;
    return 0;
}
Copy the code

Longshao: it’s true, but it looks a bit messy. It feels much more complicated than my code. Jet: Simplicity has a simple cost, and complexity has a complex value. If Lin Xi asks you to change the top again, you only have to change one place.

int main() {
    auto top="#"; // The hero sees... }Copy the code

Long shao: oh, sure enough, so less change 9 times, great. Jet: You can change it the same way down here.

Although Longshao is a cute new, but the understanding is very good. Seconds to write:

int main() {
    auto top="#";
    auto bottom="-";
    for(int i = 0; i < 10; ++i) { i! = 9? cout << top: cout << top<<endl; } std::cout <<"| | ~ ~" << std::endl;
    std::cout << ".. | |" << std::endl;
    std::cout << "| | -}" << std::endl;
    for(int i = 0; i < 10; ++i) { i! = 9? cout << bottom: cout << bottom<<endl; }return 0;
}
Copy the code

And even this guy can relate:

int main() {
    auto top="#";
    auto bottom="-";
    auto brow="~";
    auto eyes="X";
    for(int i = 0; i < 10; ++i) { i! = 9? cout << top: cout << top<<endl; } cout <<"|"<< brow <<""<< brow <<"|" << endl;
    cout << "|"<< eyes <<""<< eyes <<"|" << endl;
    cout << "| | -}" << endl;
    for(int i = 0; i < 10; ++i) { i! = 9? cout << bottom: cout << bottom<<endl; }return 0;
}
Copy the code

Jet: excellent as you are. It looks like programming material, but I’ll give you some advice. Long shao: good, elder brother say add, I add. Although already unrecognizable, but really quite good to use ah, I can not be afraid of Lin Xi afternoon. Jet: Hehe, you are underestimating the demand for the product.


3. Function encapsulation

Jet: What if I print two faces? Long Shao: For CV engineers, it’s a piece of cake.

int main() {
    auto top="#"; // Top auto bottom="-"; // bottom auto brow="~"; // auto eyes="X"; // Eyes // Print the first facefor(int i = 0; i < 10; ++i) { i! = 9? cout << top: cout << top<<endl; } cout <<"|"<< brow <<""<< brow <<"|" << endl;
    cout << "|"<< eyes <<""<< eyes <<"|" << endl;
    cout << "| | -}" << endl;
    for(int i = 0; i < 10; ++i) { i! = 9? cout << bottom: cout << bottom<<endl; } // Print the second facefor(int i = 0; i < 10; ++i) { i! = 9? cout << top: cout << top<<endl; } cout <<"|"<< brow <<""<< brow <<"|" << endl;
    cout << "|"<< eyes <<""<< eyes <<"|" << endl;
    cout << "| | -}" << endl;
    for(int i = 0; i < 10; ++i) { i! = 9? cout << bottom: cout << bottom<<endl; }return 0;
}
Copy the code


Jet: We should have the self-esteem of world designers and never call ourselves CV engineers. Longshao: it is. We just copy and change other things. Jet shook his head helplessly: Ah, young people now. So, what if I want to print two different faces? Longshao: You mean define another set of variables? Jet: Then print 100 faces, your code can still see? Longshao :100… I choose death…. Jet: It’s time for a function to help you. It can encapsulate several statements and control them with arguments.

#include <iostream>// Declare the function voidprintFace(const char *top, const char *bottom, const char *brow, const char *eyes);

using namespace std;

int main() {
    printFace("#"."-"."~"."X");
    printFace("O"."N"."."."$");
    return0; } /** * Print a face * @param top * @param bottom * @param brow * @param eyes */ voidprintFace(const char *top, const char *bottom, const char *brow, const char *eyes) {
    for(int i = 0; i < 10; ++i) { i! = 9? cout << top: cout << top<<endl; } cout <<"|"<< brow <<""<< brow <<"|" << endl;
    cout << "|"<< eyes <<""<< eyes <<"|" << endl;
    cout << "| | -}" << endl;
    for(int i = 0; i < 10; ++i) { i! = 9? cout << bottom: cout << bottom<<endl; }}Copy the code

Jet: do you have a deeper understanding of C++ now? Long Shao nodded repeatedly. Afternoon in the face of Lin Xi in every way, dragon less easily. Linxi wonder: elder brother also can’t cure you, print too small, give me magnify one times. Longshao: Why don’t you make the mouse bigger to catch the cat? They had a small argument and broke up after work.


4. Class encapsulation

Jet and Longshao are neighbors and playmates from childhood. The two of them take the bus home together. Longshao: you give comment reason, whether he is unruly. Jet: Yes, but it’s also a need. Forget that, let me tell you about object-oriented programming. Long shao: Ah, a single dog, how to have the opportunity to object-oriented programming. Jet: Not that guy. Look, we’re taking the bus, right? Why don’t we just walk? Longshao: more than ten miles. You’re silly. It’s too much trouble if you break your leg. Jet: Why don’t you build your own bus and drive it yourself? Long little: cost, sit a bus three MAO three, build a bus say hundreds of thousands of little, enough sit a lifetime. Jet: So as long as we pass bus.go(0.33), we can very simple home, in fact, programming also has this idea, is object-oriented. If you have a Facer friend, you just need to print a face through its printFace(). Do you want to know him? Something like this:

 #include "Facer.h"

using namespace std;

int main() {
    Facer face("#"."-"."~"."X");
    face.printFace();
    face.bottom="V";
    face.printFace();
    return 0;
}
Copy the code

Longshao: It feels great. I want it. Tell me how to do that.


Start by defining a header file, facer.h, that describes the attributes (fields) and capabilities (methods) of the Facer

#include <iostream>
using namespace std;

#ifndef TOLYC_FACER_H
#define TOLYC_FACER_H

class Facer {
public:
    Facer(const string &top="#", const string &bottom="#", const string &brow="~", const string &eyes=".");
    ~Facer();
public:
    string top;
    string bottom;
    string brow;
    string eyes;
public:
    void printFace() ;
};

#endif //TOLYC_FACER_H
Copy the code

In Facer. CPP, construct, destruct and function methods are implemented.

#include "Facer.h"

Facer::Facer(
        const string &top,
        const string &bottom,
        const string &brow,
        const string &eyes) : top(top),
                              bottom(bottom),
                              brow(brow),
                              eyes(eyes) {}

void Facer::printFace() {
    for (int i = 0; i < 10; ++i) {
        i != 9 ? cout << top : cout << top << endl;
    }
    cout << "|" << brow << "" << brow << "|" << endl;
    cout << "|" << eyes << "" << eyes << "|" << endl;
    cout << "| | -}" << endl;
    for (int i = 0; i < 10; ++i) {
        i != 9 ? cout << bottom : cout << bottom << endl;
    }
}

Facer::~Facer() {}Copy the code

Longshao: I see, it’s very simple, object oriented only so much. Jet: Too yong too simple. This is just the tip of the object oriented iceberg. I’ll tell you more about it sometime. Cout is not very flexible here, so we can concatenate it with strings and print it again.

// // Created by Zhang Fengjietele on 2019/10/3. //#include <iostream>

using namespace std;

#ifndef TOLYC_FACER_H
#define TOLYC_FACER_H

class Facer {
public:
    Facer(const string &top="#", const string &bottom="#", const string &brow="~", const string &eyes=".");
    ~Facer();
public:
    string top;
    string bottom;
    string brow;
    string eyes;
public:
    void printFace() ;
    string getFace();
};

#endif //TOLYC_FACER_H
Copy the code

// // Created by Zhang Fengjietele on 2019/10/3. //#include "Facer.h"

Facer::Facer(
        const string &top,
        const string &bottom,
        const string &brow,
        const string &eyes) : top(top),
                              bottom(bottom),
                              brow(brow),
                              eyes(eyes) {}

void Facer::printFace() {
    cout<< getFace() << endl;
}

Facer::~Facer() {

}

string Facer::getFace() {
    string result;
    for (int i = 0; i < 10; ++i) {
        i != 9 ? result+=top : result+=top+"\n";
    }
    result+= "|" +brow + "" + brow + "|" +"\n";
    result+= "|" +eyes + "" + eyes + "|" +"\n";
    result+= "| -} |\n";
    for (int i = 0; i < 10; ++i) {
        i != 9 ? result+=bottom : result+=bottom+"\n";
    }
    return result;
}

Copy the code