-
Notifications
You must be signed in to change notification settings - Fork 0
/
passing struct to class.cpp
67 lines (57 loc) · 1.32 KB
/
passing struct to class.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
#include<bits/stdc++.h>
using namespace std;
struct exam
{
float first;
float second;
float final;
void f()
{
cout << "The program is starting...." << endl;
}
};
class subject
{
char name[10];
exam Exam;
public:
subject() // empty constructor; initial values for name and exam
{
strcpy_s(name, "no name");
/*
Exam.first = 0;
Exam.second = 0;
Exam.final = 0;
*/ // ORRR
Exam = {0, 0, 0}; // Struct initial value
}
subject(char n[], float fst, float sec, float fin) // parameterized constructor
{
Exam.f();
strcpy_s(name, n);
Exam = {fst, sec, fin};
// or
/*
Exam.first = fst;
Exam.second = sec;
Exam.final = fin;
*/
}
float total()
{
return Exam.first + Exam.second + Exam.final;
}
void print()
{
cout << "The subject name is " << name << endl
<< ", its first exam mark is " << Exam.first
<< ", and the second exam mark is " << Exam.second
<< ", and the third exam mark is " << Exam.final << endl
<< "So, your total mark is " << total() << endl;
}
};
int main()
{
subject e("OOP", 25, 24, 49); // call parameterized constructor
e.print();
}