-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassAndObjects.cpp
More file actions
40 lines (30 loc) · 974 Bytes
/
classAndObjects.cpp
File metadata and controls
40 lines (30 loc) · 974 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
#include<iostream>
using namespace std;
class Complex{
int a , b;
public:
// creating a constructor
// Constructor is a special member function with the same name as of the class
// it is used to initialize the objects of its class
// it is automatically invoked whenever an object is created
Complex(); // constructor declaration
void printNumber(){
cout<<"Your number is "<<a<<" + "<<b<<"i"<<endl;
}
};
Complex :: Complex(){ // this is a default constructor as it takes no parameters
a = 10;
b = 0;
}
int main(){
Complex c1,c2,c3;
c1.printNumber();
c2.printNumber();
c3.printNumber();
}
// Properties of constructors
// 1. It should be declared in the public section of the class
// 2. They are automatically invoked whenever the object is invoked
// 3. They cannot return values and do not have return types
// 4. It can have default arguments
// 5. We cannot refer to their address ***