Skip to content

Commit f8128bc

Browse files
committed
[Chap10][object]:Practise the usage of object
1 parent 941e9f4 commit f8128bc

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

10_chap_class/move/.move.h.swp

12 KB
Binary file not shown.

10_chap_class/move/.test.cpp.swp

12 KB
Binary file not shown.

10_chap_class/move/move.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include"move.h"
2+
#include<iostream>
3+
#include<cstring>
4+
5+
using namespace std;
6+
7+
Move :: Move (double a, double b)
8+
{
9+
x = a;
10+
y = b;
11+
}
12+
13+
void Move :: showmove () const
14+
{
15+
cout << "x" << x << "y" << y << endl;
16+
}
17+
18+
Move Move:: add (const Move & m)
19+
{
20+
x = x + m.x;
21+
y = y + m.y;
22+
23+
return *this;
24+
}
25+
26+
void Move :: reset (double a, double b)
27+
{
28+
x = a;
29+
y = b;
30+
}
31+

10_chap_class/move/move.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef __MOVE_H__
2+
#define __MOVE_H_
3+
4+
class Move
5+
{
6+
private:
7+
double x;
8+
double y;
9+
public:
10+
Move (double a = 0, double b = 0);
11+
void showmove ()const;
12+
Move add (const Move & m);
13+
void reset (double a = 0, double b = 0);
14+
};
15+
16+
17+
#endif

10_chap_class/move/test.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "move.h"
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
Move m1;
9+
//Move m2 = Move(10,20);
10+
//Move m3 = Move(30,40);
11+
12+
Move* m2 = new Move(10,20);
13+
Move* m3 = new Move(30,40);
14+
15+
//m3.showmove();
16+
m3->showmove();
17+
cout << "-----------------" << endl;
18+
//m3.add(m2);
19+
m3->add(*m2);
20+
//m3.showmove();
21+
m3->showmove();
22+
cout << "-----------------" << endl;
23+
//m2.showmove();
24+
m2->showmove();
25+
26+
cout << "delete ----------------" << endl;
27+
delete(m2);
28+
delete(m3);
29+
30+
return 0;
31+
}

0 commit comments

Comments
 (0)