-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclassesAndObjects.cpp
71 lines (55 loc) · 2 KB
/
classesAndObjects.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<bits/stdc++.h>
using namespace std;
class Point {
private:
int x, y; // Called as instance member variables
public:
// instance member functions
void setData(int x, int y) {
this->x = x;
this->y = y;
}
void getData();
int getX() {
return x;
}
int getY() {
return y;
}
// Manhattan distance between 2 points
int manhattanDistance(Point p) {
return abs(x - p.x) + abs(y - p.y);
}
};
/*
Point:: --> This thing is called membership label (Class name and then scope resolution operator)
*/
void Point::getData() {
cout << "The coordinates of point are: (" << x << ", " << y << ")" << endl;
}
int main() {
/*
Class is description of object (Basically blueprint)
It is used to describe properties and behaviour of an object
Object is an instance of a class
Differences between classes and structures:
1. Access specifiers are public for structs by default and private for classes
2. Inheritance defaults to public for structs by default and private for classes
Except these, you can do everything in struct and class in similar fashion
Difference between defining a function inside the class and outside the class:
--> Functions defined inside the class are inline by default but when they are
defined outside the class, inline keyword has to be used explicitly to reuqest for
inline functionality
IMP: State of an object should only be changed by instance member functions. State here means values of member variables
Different names for Instance member variables: Attributes, data members, fields, properties, etc
Different names for Instance member functions: Methods, procedures, actions, operations, services, etc
*/
Point p1;
p1.setData(2, 3);
p1.getData();
Point p2;
p2.setData(4, 5);
p2.getData();
int distance = p1.manhattanDistance(p2);
cout << "Manhattan distance between (" << p1.getX() << " ," << p1.getY() << ") and (" << p2.getX() << ", " << p2.getY() << ") is " << distance << endl;
}