C++ programming-class comprehensive exercises

Just like Lu Ban also went through constant practice before becoming a carpenter's ancestor.

… The
master raised the third question: “The two apprentices learned the craft and went down the mountain, and the master gave each of them an axe. The big apprentice used the axe to earn a golden mountain, and the two apprentices used the axe to carve in people’s hearts A name. Which apprentice would you like to learn from?” Lu Ban immediately replied, “I would like to learn from the second one.” The master laughed.
The master said: "Well, you got all the answers right, and I have to accept you. But to learn art from me, you have to use my guy. But this guy, I haven't called you for 500 years, you take it Fix it.”
Lu Ban took out the guy in the wooden box and took a look. The axe broke, the plane was overgrown, the chisel was bent and bald, and it was time to clean up. He rolled up his sleeves and sharpened it on the whetstone. He grinds during the day and at night, so that his arms are sore, and his hands are blistered, and the high and thick whetstone is ground like a crescent. It has been sharpening for seven days and nights, the axe is sharpened, the plane is polished, and the chisel is sharpened, each piece is shiny. He showed it to the master one by one, who couldn't help but nodded.
The master said: "Try the axe you sharpened. Go and cut down the big tree in front of the door. That big tree has grown for five hundred years."
Luban raised the axe and walked under the tree. This big tree is so thick that a few people can't hold it; when I looked up, it was about to reach the sky. He picked up an axe and kept cutting down, cutting down the big tree twelve days and twelve nights.
Luban went into the house with an axe to see the master. The master said: "Try the plane you sharpened. First, use an ax to cut this big tree into a big tree, and then use the plane to plan it out; it should be so smooth that it does not leave a burr. It's like the moon of fifteen."
Lu Ban turned around, took an axe and a plane to the door. He chopped off the branches of the big tree with axe after axe, and planed the knots on the trunk one after another. After twelve days and twelve nights, he planed the big tree. It has to be round and light.
Luban took the axe and plane into the house to see the master. The master said: "Try the chisel you sharpened. You chisel two thousand and four hundred eyes on the big jade: six hundred square, six hundred round, six hundred stupid, six hundred. flat."
Lu Ban picked up the chisel and axe, came to the side of Dazhu and chiseled them. He chiseled one eye and another, and saw the sawdust flying around. A full twelve days and twelve nights were cut, and two thousand four hundred eyes were cut: six hundred square, six hundred round, six hundred staggered, and six hundred flat.
Lu Ban brought chisel and axe to see the master. The teacher laughed, and he praised Lu Ban and said: "Good boy, I will teach you all the craftsmanship!" He took Lu Ban to Westinghouse after he finished speaking. It turned out that there were many models in the Westinghouse. There were towers, pavilions, bridges and towers, tables, chairs, boxes and cabinets, all of which were so exquisite. The teacher said with a smile: "You take these models apart and then install them again. Each model must be disassembled and set up again. You will learn the craftsmanship by your own concentration." The
teacher went out after speaking. Lu Ban picked up this one and looked at the other one, but he was reluctant to put it down. He held the model piece by piece in his hand, turned it over and looked at it, carefully dismantling each piece three times and installing it three times. I don’t care about eating and sleeping every day. When the master came to see him in the morning, he was thinking; when he came to see him at night, he was still thinking. The master urged him to sleep, but he casually agreed, but didn't put down the model in his hand.
Lu Ban studied hard for three years and learned all the crafts. The master will also try him, destroy all the models and let him recreate. Based on his memory, he made each piece exactly the same as the original. The master also proposed many new models for him to build. He did it while pondering, and the result was all made in the way the master said. The master is very satisfied.
...

Many features and grammars in C++ are not used in my work, which leads to stumbling when taking over other people's projects. So here I want to write a project that includes most of the features to improve my C++ level.

This is a very complete summary... The
C++ study outline is
also a project UML.

This is the code on the teacher's open-book test paper in a course of my university, and then it was secretly photographed and typed out. The knowledge of the related categories inside is more sharp and let go.

#include <iostream>
using namespace std;

//Y类开始=================================================
class Y {
    
    
public:
	Y();
	Y(int j);
	Y(const Y&);
	Y& operator = (const Y&);
	virtual ~Y();
	virtual int GetValue() = 0;
	static unsigned NY() {
    
     return nY; }
private:
	int j;
	static unsigned nY;
};

unsigned Y::nY = 0;
Y::Y() {
    
    
	cout << "default constructor of Y " << endl;
	j = 0;
	++nY;
}

Y::Y(int j) {
    
    
	cout << "regular constructor of Y" << endl;
	this->j = j;
	++nY;
}

Y::Y(const Y& y) {
    
    
	cout << "copy constructor of Y " << endl;
	j = y.j;
	++nY;
}

Y& Y::operator=(const Y& y) {
    
    
	cout << "assignment operator of Y" << endl;
	j = y.j;
	return *this;
}

Y::~Y() {
    
     cout << "destructon of Y" << endl; }

//X类,继承Y类=============================================
class X : public Y {
    
    
public:
	X();
	X(int i);
	X(const X&);
	X& operator=(const X&);
	~X();
	virtual int GetValue() {
    
     return k; }
	static unsigned NX() {
    
     return nX; }
private:
	int k;
	static unsigned nX;
};

unsigned X::nX = 0;
X::X() {
    
    
	cout << "default constructor of X" << endl;
	k = 0;
	++nX;
}

X::X(int i) :Y(i) {
    
    
	cout << "regular constructor of X" << endl;
	k = i;
	++nX;
}

X::X(const X& x) {
    
    
	cout << "copy constructor of X" << endl;
	k = x.k;
	++nX;
}

X& X::operator=(const X& x) {
    
    
	cout << "assignment operaton of X" << endl;
	k = x.k;
	return *this;
}

X::~X() {
    
     cout << "destructor of x" << endl; }

X userCode(X b)
{
    
    
	X c = b;
	return c;
}

//main函数开始==================================================
int main() {
    
    
	cout << "Beginning: Y::NY()=" << Y::NY() << endl;
	cout << "Beginning: X::NX()=" << X::NX() << endl;
	X a(3);
	cout << "calling userCode()" << endl;
	X d;
	d = userCode(a);
	cout << "back to main()" << endl;
	cout << "d.GetValue()" << d.GetValue() << endl;

	Y*e = new X(7);
	cout << "e->GetValue()=" << e->GetValue() << endl;
	cout << "before deleting e Y::NY() = " << Y::NY() << endl;
	cout << "before deleting e X::NX() = " << X::NX() << endl;
	delete e;
	cout << "before ending: Y::NY()" << Y::NY() << endl;
	cout << "before ending: X::NX()" << X::NX() << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/sinat_27382047/article/details/102641855