Getters and Setters

4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€87.69
List Price:  €122.77
You save:  €35.08
£75.17
List Price:  £105.24
You save:  £30.07
CA$138.15
List Price:  CA$193.41
You save:  CA$55.26
A$156.01
List Price:  A$218.42
You save:  A$62.41
S$131.13
List Price:  S$183.59
You save:  S$52.46
HK$775.83
List Price:  HK$1,086.19
You save:  HK$310.36
CHF 82.37
List Price:  CHF 115.32
You save:  CHF 32.95
NOK kr1,040.74
List Price:  NOK kr1,457.09
You save:  NOK kr416.34
DKK kr654.68
List Price:  DKK kr916.58
You save:  DKK kr261.90
NZ$167
List Price:  NZ$233.81
You save:  NZ$66.80
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳12,244.69
List Price:  ৳17,143.06
You save:  ৳4,898.36
₹8,541.58
List Price:  ₹11,958.56
You save:  ₹3,416.97
RM439.21
List Price:  RM614.91
You save:  RM175.70
₦161,317.86
List Price:  ₦225,851.46
You save:  ₦64,533.60
₨28,073.90
List Price:  ₨39,304.58
You save:  ₨11,230.68
฿3,339.06
List Price:  ฿4,674.82
You save:  ฿1,335.76
₺3,828.72
List Price:  ₺5,360.36
You save:  ₺1,531.64
B$572.08
List Price:  B$800.93
You save:  B$228.85
R1,857.55
List Price:  R2,600.64
You save:  R743.09
Лв171.49
List Price:  Лв240.10
You save:  Лв68.60
₩142,204.87
List Price:  ₩199,092.51
You save:  ₩56,887.63
₪366.59
List Price:  ₪513.24
You save:  ₪146.65
₱5,655.18
List Price:  ₱7,917.48
You save:  ₱2,262.29
¥14,198.32
List Price:  ¥19,878.22
You save:  ¥5,679.89
MX$1,953.70
List Price:  MX$2,735.25
You save:  MX$781.55
QR364.07
List Price:  QR509.71
You save:  QR145.64
P1,376.72
List Price:  P1,927.47
You save:  P550.74
KSh12,943.70
List Price:  KSh18,121.70
You save:  KSh5,178
E£5,094.13
List Price:  E£7,131.98
You save:  E£2,037.85
ብር13,446.70
List Price:  ብር18,825.91
You save:  ብር5,379.21
Kz91,690.83
List Price:  Kz128,370.83
You save:  Kz36,680
CLP$95,242.47
List Price:  CLP$133,343.27
You save:  CLP$38,100.80
CN¥731.15
List Price:  CN¥1,023.64
You save:  CN¥292.49
RD$6,020.55
List Price:  RD$8,429.01
You save:  RD$2,408.46
DA13,234.77
List Price:  DA18,529.21
You save:  DA5,294.44
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q776.33
List Price:  Q1,086.89
You save:  Q310.56
GY$21,142.77
List Price:  GY$29,600.73
You save:  GY$8,457.95
ISK kr12,708.72
List Price:  ISK kr17,792.72
You save:  ISK kr5,084
DH928.92
List Price:  DH1,300.53
You save:  DH371.60
L1,718.78
List Price:  L2,406.37
You save:  L687.58
ден5,396.62
List Price:  ден7,555.49
You save:  ден2,158.86
MOP$805.16
List Price:  MOP$1,127.26
You save:  MOP$322.09
N$1,878.49
List Price:  N$2,629.97
You save:  N$751.47
C$3,709.18
List Price:  C$5,193
You save:  C$1,483.82
रु13,729.94
List Price:  रु19,222.47
You save:  रु5,492.52
S/373.49
List Price:  S/522.91
You save:  S/149.41
K416.97
List Price:  K583.78
You save:  K166.80
SAR375.05
List Price:  SAR525.09
You save:  SAR150.03
ZK2,869.58
List Price:  ZK4,017.53
You save:  ZK1,147.95
L436.50
List Price:  L611.12
You save:  L174.62
Kč2,193.75
List Price:  Kč3,071.34
You save:  Kč877.59
Ft35,836.25
List Price:  Ft50,172.18
You save:  Ft14,335.93
SEK kr958.55
List Price:  SEK kr1,342.01
You save:  SEK kr383.45
ARS$110,318.96
List Price:  ARS$154,450.96
You save:  ARS$44,132
Bs696.42
List Price:  Bs975.02
You save:  Bs278.59
COP$431,407.96
List Price:  COP$603,988.40
You save:  COP$172,580.44
₡50,669.87
List Price:  ₡70,939.85
You save:  ₡20,269.97
L2,580.74
List Price:  L3,613.14
You save:  L1,032.40
₲806,732.12
List Price:  ₲1,129,457.25
You save:  ₲322,725.12
$U4,215.14
List Price:  $U5,901.37
You save:  $U1,686.22
zł376.49
List Price:  zł527.11
You save:  zł150.61
Already have an account? Log In

Transcript

Hello, and welcome to the 53rd tutorial on c++ programming series. And in this part we're gonna be looking at getters and setters get a lay to retrieve a value, usually when the value isn't public, but it could be public. But then sort of the use of getters is questionable. And it is common practice not to have any variable public, in general, for a lot of application, and this is to prevent any misuse accidentally from outside the class. And setter will be used to specifically set the data. getters and setters are just regular method, really a getter has a return type.

And that's the same that says that the return type is the same as the value that you are returning obviously, and takes no arguments. Obviously, you could take arguments you could change if you wanted to. This is just a general convention of getters and setters has no turn time, but takes an argument or maybe multiple arguments and does something with it. Which is what the variable will be set. And you could maybe put a return type. But this is just a general convention of getters and setters.

So let's go ahead and open up our project. And what we're going to do first is implement a really simple Class, Class, vehicle. Public new vehicle service critical structure, we're going to create some clover more than a week into speed. In a and at the moment, we can't get it from outside the class, the speed and engine. So what we're going to do, we're only going to be dealing with the speed but you could modify this at the end and try and do the age of other little tasks. I didn't do didn't get paid the commission Call whatever you want, but obviously general naming convention and general logic is or naming what it's doing.

So get speakers get in the speed variable, then we're going to call it set speed. You guessed it, this sets the speed in new speed. I don't know if you can hear that there seems to be some sort of somebody working outside from the office cleaning up something, but if you can, very sorry about that. Let's just get rid of this piece of code. And what we're gonna do. Before we do that, we need to actually implement the function so int vehicle, get speed.

And here is going to be return speed. Then we'll do void Boyd vehicle sets the lead on it. When the speed equals new speed. The next thing that we're going to do is just simply create our objects in the vehicle, B and w. And if we do STD C, BMW gets the SDP. And why run it, and we get a error. And that is I believe, because we haven't set the variable so we need to do BMW dot set feed and this will provide a value of 500.

We run the macro, implement the constructor, vehicle, even if we don't do anything code, we declare that we need to implement it ourselves. But now if we run this, we get bill succeeded and it's got a value of 500 point if we comment this out a second. You're a believer. Because the speed hasn't been set yet, because it's private content explicitly, we use these settings and get a 30. This is the use of setting getters, you have to set variable and be mtbf to get variable as well. It's just to prevent any misuse outside the object.

That's it for this part of this series. In the next part, we'll look at static variables. If you have any questions, feel free to message us at support at sonar systems Koto, UK, the email will be in this in the description you can comment on this video or directing messages via YouTube, or the requirements of source code will also be in the description. And as usual, thanks for watching. I hope you have a nice day.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.