Constant Objects

3 minutes
Share the link to this page
Copied
  Completed
C++ Constants/Literals. Constants refer to fixed values that the program may not alter and they are called literals. Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

Transcript

Hello, welcome to the 52nd tutorial in the c++ programming series and in this part we'll be looking at constant objects. Constant objects variables are only read only outside the class. So basically when they are created, essentially the read only, but they still can modify, the properties can still be modified within the class. So what we're going to do is open up our project and it's going to create a really simple class as we have been doing for the past few tutorials. Only code and you've guessed a vehicle if you've been watching our previous videos. Create some public stir wanna do a overloaded constructor with the paint speed in a dense eat can do in age.

Want to implement this constructor so vehicle colon colon vehicle And then here in the a Salesforce assign speed to well speed this a pools age. Now what would an SMP do is create a object in the normal fashion as we have done before, but we just use the keyword const before which basically stands for constant. Now per object type aka vehicle for BMW, that's the name and then we're gonna pass in some values as can be assigned initially 657 for the age. Stephanie a very old car for 657 years old. They name it I've called them so yeah, okay, so if we just do STD c out In the age FTD line. Now if we run this, we're about six or seven.

It works as it did before, if we try and do, because notice these two are public, so we can access them directly outside the class, but we do BMW ag equals on puffery cause semicolon running. And the error says read only variable is not assignable. Basically this object is read only, as I mentioned before, so you can't modify its properties or variables or anything like that. But you can access the function you can access the variables. So yeah, this is a way of creating a constant object, which is read only. That's it for this part of this series.

In the next tutorial, we'll be looking at getters and setters. If you have any questions, feel free to message us at support at sonar systems calm okay. The email will be in the description. You can comment on this video or historically messages via YouTube or the recordings source code will also be in the description as usual. Thanks for watching and 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.