-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstructor.cpp
61 lines (52 loc) · 1.19 KB
/
constructor.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
/*costructor is a particular data type which is used to group a couple of objectes for various time.
if the valaueis not given here then by default it takes garbage value.
and inside a constructor the passing value will be printed for that iteration.*/
#include<iostream>
#include<string>
using namespace std;
//FIRST STEP:
/*
class laptop
{
public:
string name;
float price;
laptop()//constructor have to have the name of class.
//the value can also be passed as laptop(objname= " Mac", objprice = 100.00)
{
// strcpy(name," Mac");//string copy
name=" Mac";
price=100.00;
cout<<"Name : "<<name<<endl<<" Price : "<<price<<endl;
}
};
int main()
{
laptop obj;// here the (.) operator is not given
return 0;
}
*/
//SECOND STEP:
class laptop
{
public:
string name;
float price;
laptop()
{
//here the garbage val will print.
cout<<" Name : "<<name<<endl<<"Price : "<<price<<endl;
}
laptop(string objname, float objprice)
{
name=objname;
price=objprice;
cout<<" Name : "<<name<<endl<<"Price : "<<price<<endl;
}
};
int main()
{
laptop obj;// here the (.) operator is not given
laptop m(" Mac",100.00);
return 0;
}