-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29constructors.cpp
More file actions
38 lines (34 loc) · 849 Bytes
/
Copy path29constructors.cpp
File metadata and controls
38 lines (34 loc) · 849 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
#include <iostream>
using namespace std;
class complex
{
private:
int a, b;
public:
complex(); // a constructor
void printnum()
{
cout << "z = " << a << " + " << b << "i" << endl;
}
};
// This is a Default Constructor : this doesn't take any parameters so it's a default constructor
complex ::complex()
{
a = 10;
b = 8;
}
int main()
{
// constructors are functions of class with the same name as the class they belong to, they are used to initialize objects
complex c;
c.printnum();
return 0;
}
/*
Characteristics / Properties of constructors
1) They should be created inside public section of the class
2) They are automatically invoked whenever an object is created
3) They do not have return type or return values
4) They can have default arguments
5) We can not refer to their addresses
*/