-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructor
More file actions
57 lines (47 loc) · 1.08 KB
/
Constructor
File metadata and controls
57 lines (47 loc) · 1.08 KB
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
*
Constructors
Class constructors are special member functions of a class. They are executed whenever new objects are created within that class.
The constructor's name is identical to that of the class. It has no return type, not even void.
*/
class myClass {
public:
myClass() {
cout <<"Hey";
}
void setName(string x) {
name = x;
}
string getName() {
return name;
}
private:
string name;
};
int main() {
myClass myObj;
return 0;
}
//Outputs "Hey"
//Now, upon the creation of an object of type myClass, the constructor is automatically called.
////////////////////////////////
#include <iostream>
using namespace std;
class Rectangle
{
double mHeight, mWidth;
public:
Rectangle() :mHeight(1),mWidth(1){}
Rectangle(double height, double width) :mHeight (height), mWidth(width) {}
void setRectangle(double x, double y)
{
mHeight =x;
mWidth=y;
}
double getHeight() { return mHeight ;}
double getWidth() { return mWidth ;}
} ;
int main () {
Rectangle first(5,6);
first.setRectangle(5, 6);
return 0;
}