-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathclass.cpp
More file actions
54 lines (43 loc) · 961 Bytes
/
Copy pathclass.cpp
File metadata and controls
54 lines (43 loc) · 961 Bytes
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
// Example C++ Class
#include<iostream>
#include<string>
using namespace std;
class MyClass {
private:
string name;
int num;
public:
// Constructor with default values
MyClass(const string& n="", int x=10) {
name = n;
num = x;
}
// Method declarations
void print();
void change(const string& n, int x);
};
// Display the name & num
void MyClass::print() {
cout << "Name = " << name << " ";
cout << "Num = " << num << endl;
}
// Mutator
void MyClass::change(const string& n, int x) {
name = n;
num = x;
}
// Non class member function
int timesTwo(int x) {
return 2 * x;
}
int main() {
MyClass c1("Bob Smith", 1);
MyClass c2("Alice Jones", 2);
c1.print();
c2.print();
cout << endl;
cout << "timeTwo(5)=" << timesTwo(5) << endl << endl;
c2.change("Alice Smith", timesTwo(4));
c2.print();
return 0;
}