-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleclass.cpp
76 lines (57 loc) · 1.71 KB
/
simpleclass.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
72
73
74
75
76
/*******************************************
Second example: second.cpp
A simple example for classes and methodes.
Chensong Zhang Feb/08/2011.
********************************************/
#include <iostream>
#include <string>
using namespace std;
// class definition ...
class Student {
string name;
unsigned short int age;
short sex; // 1 for male, -1 for female
public: // public methods
// constructor with default values
Student(string myname="Name", int myage=20, short mysex=1) {
name = myname;
age = myage;
sex = mysex;
}
void set_name(string givename);
void set_age(unsigned short i);
void set_sex(int givensex);
void output();
private: // private methods
string output_name();
int output_age();
string output_sex();
};
// definitions of the class members ...
void Student::set_name (string givename) { name.assign(givename); }
string Student::output_name () { return name; }
void Student::set_age (unsigned short i) { age = i; }
int Student::output_age () { return age; }
void Student::set_sex (int givensex) { sex = givensex; }
string Student::output_sex () { if (sex) return "male"; else return "female"; }
void Student::output() {
cout << output_name() << " is a "
<< output_sex() << " of age "
<< output_age() << "."
<< endl;
}
// main program here ...
int main()
{
Student Chensong, Yan;
cout << "Before setting values ..." << endl;
Chensong.output();
Yan.output();
// set values
Chensong.set_name("Chensong"); Chensong.set_age(33); Chensong.set_sex(1);
Yan.set_name("Yan"); Yan.set_age(28); Yan.set_sex(0);
cout << "After setting values ..." << endl;
Chensong.output();
Yan.output();
return 0;
}