8000 add C++PrimerTest 15/4/26 · tthhee/leetcode@96817c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96817c8

Browse files
committed
add C++PrimerTest 15/4/26
1 parent 1a672ae commit 96817c8

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

C++PrimerTest/C++PrimerTest.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <memory>
4+
#include <vector>
5+
using namespace std;
6+
7+
struct CModel
8+
{
9+
private:
10+
int a;
11+
string str;
12+
public:
13+
CModel()
14+
{
15+
cout << "CModel()" << endl;
16+
a = 0;
17+
str = " ";
18+
}
19+
CModel(int i, string s):a(i), str(s)
20+
{
21+
cout << "CModel(i, s)" << endl;
22+
}
23+
CModel(const CModel& m)
24+
{
25+
cout << "CModel(const CModel&)" << endl;
26+
a = m.a;
27+
str = m.str;
28+
}
29+
void print()
30+
{
31+
cout << "a: " << a << endl;
32+
cout << "str: " << str << endl;
33+
}
34+
CModel& operator=(CModel& s)
35+
{
36+
a = s.a;
37+
str = s.str;
38+
cout << "using operator=" << endl;
39+
}
40+
41+
~CModel()
42+
{
43+
cout << "using ~CModel()" << endl;
44+
}
45+
};
46+
47+
void showCModel(CModel& obj)
48+
{
49+
obj.print();
50+
}
51+
int main()
52+
{
53+
CModel object(2, "xiangyu");
54+
CModel object2(3, "liuchang");
55+
// shared_ptr<CModel> cp = make_shared<CModel>(object);
56+
vector<CModel> Cvec;
57+
Cvec.push_back(object);
58+
Cvec.push_back(object2);
59+
Cvec.push_back(object2);
60+
Cvec.push_back(object);
61+
cout << "size: " << Cvec.size() << "maxsize: " << Cvec.max_size() << endl;
62+
for(auto it : Cvec)
63+
{
64+
it.print();
65+
}
66+
67+
// showCModel(*cp);
68+
return 0;
69+
}
70+

C++PrimerTest/C++PrimerTest.exe

96.6 KB
Binary file not shown.

C++PrimerTest/exercise13-14.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
3+
class Numbered
4+
{
5+
public:
6+
int mysn;
7+
int a;
8+
int b;
9+
static int num;
10+
Numbered()
11+
{
12+
num++;
13+
mysn = num;
14+
a = 0;
15+
b = 0;
16+
}
17+
Numbered(const Numbered & s)
18+
{
19+
num++;
20+
mysn = num;
21+
a = s.a;
22+
b = s.b;
23+
24+
}
25+
26+
27+
};
28+
void f(const Numbered& s)
29+
{
30+
std::cout << s.mysn << std::endl;
31+
}
32+
int Numbered::num = 0;
33+
int main()
34+
{
35+
Numbered a, b = a, c = b;
36+
f(a);
37+
f(b);
38+
f(c);
39+
40+
return 0;
41+
}

C++PrimerTest/exercise13-14.exe

70 KB
Binary file not shown.

C++PrimerTest/exercise13.18.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
static int num = 0;
7+
class Employee{
8+
string name;
9+
int sn;
10+
Employee()
11+
{
12+
name = " ";
13+
sn = ++num;
14+
}
15+
Employee(string name)
16+
{
17+
sn = ++num;
18+
this.name = name;
19+
}
20+
21+
Employee(const Employee& s)
22+
{
23+
sn = ++num;
24+
name = s.name;
25+
}
26+
};

0 commit comments

Comments
 (0)
0