declare

Buckys C++ Programming Tutorials - 44 - const Objects



Sharing buttons:

what's going on guys welcome back to

your 44 C++ tutorial and in this

tutorial I'm going to be teaching you

guys about a couple things specifically

about the keyword Const which stands for

a constant now whenever you use the

keyword Const it means that whatever you

use it on you typically use on variables

and stuff it means that that variable

remains constant throughout your program

aka it can't be changed or modified in

any way so let's go ahead and take a

quick look at that say you have an

integer variable X and you set equal to

3 whenever you first make it and then

later on you want to change it to

something like 5 while your program is

just going to run fine and whenever you

try to print out X or use it in any

other way it's going to run perfectly

fine as expected so what the constant

keyword does in that constant keyword Co

n St whenever you're creating something

you put the constant keyword in there

and it's pretty much telling your

compiler all right now whenever I create

this variable I'm going to set equal to

value and I don't want you to change it

for the rest of the program so now

whenever you change try to change to

something else like 5 in try to run this

program is going to get error because

it's saying hey Bucky you called this

variable constant

you promised me we're going to change it

and now you lied me right there so

that's why I'm giving you an error so

that's basically what the constant

keyword does and make sure that whatever

is constant is unable to be modified so

that being said later on in your

programming you're going to learn that

some objects you create you don't want

them to be able to be modified some

objects are just regular objects and

other objects you want constant you

don't want them to be able to change so

you use this constant keyword whenever

you create an object so therefore you

have an object that can't be modified

and you might be thinking RA so I can

just use it with normal functions and

not exactly whenever you have a constant

object

you can't call regular functions with

them you can only call functions that

they themselves are constant so constant

objects need constant functions and of

course whenever you create a constant

function you need to make sure it's

constant both in the prototype in the

header file and in

the definition aka the C++ source file

so let's go ahead and just create a

regular function first and then I'm

going to be running it aside a constant

object I'm going to be doing a couple

things so let's go ahead and make um a

sally function and we'll just name it

like print shiz and this is just a

regular function I'm not doing anything

weird yet so see out I'm just like I am

a regular function and then end that

line and then of course since we created

a function we need to prototype it so

then it is void I think I named it print

shiz just like that so now we can see

that we can just go ahead into our main

program create a Sally object and then

we can use that cell object to call our

regular functions such as print shows

just like that and whenever we run it

everything is going to go smoothly it's

going to print out I'm a regular

function so I did that and we already

know that so let's go ahead and jump

into constant objects now whenever you

create a constant object you do so doing

this throw your keyword Co n st before

you create your object and everything

else is basically exactly the same you

type class you want it to be from and

also what your name your object like

Const object looks like a pretty good

name search and IRA all I need to do is

Const

object and then I can go ahead and call

it my other methods like print shiz well

whenever we try and do this we get an

error message because it's saying

alright you created an object that it

was constant and you try to use a

function that wasn't constant see this

function isn't constant is just a

regular function so we need to build

special constant functions whenever we

create a constant object so you're

saying alright let's go ahead and do

that then so let's go ahead and create

another method on a constant excuse me

another function talking in Java another

computer program language but let's go

ahead and create another special

constant function so void Sally let's

just name this print shiz - and in order

to create a constant function you go

ahead and you type the keyword constant

right after your function definition so

Co n s T so now what happens is right

after this

we build the body for it so this is just

going to see out umm I am the constant

function just like that and of course

we'll end that line right there and now

like I said before we need to prototype

it is a constant two so we need to

basically tell every single file that

we're going to be working with constant

objects and in order to prototype it

here's what we do

go ahead and just like before type avoid

print chiz yeah that was called printers

- and you just write Const right there

and now you're telling your compiler

alright this is a constant object

prototype so a lot of people might think

you do this add the Const before make

sure you add it after the parameters and

you should be good to go

so now whenever we try and run this

constant function with the constant

object remember this is our constant

object and this is our constant function

print is - now whenever we try and

run it it should be good to go I am a

regular function

I am the constant function pretty simple

so so as you can see whenever we build

objects if we build a regular object we

can use the regular functions with them

whenever we build a constant object

we're telling our C++ compiler all right

this object we don't want anything to be

able to modify it or change it so it

told us all right I can do that but you

need to build me some constant functions

if you do that for me

I'll run your program just fine so that

was our little deal we had with the

compiler and that's why this one ran

with the right wave function and this

constant object ran with the constant

functions so now you should understand

how to prototype constant functions how

to create them and also why constant

objects need constant functions and

again later on you're going to be seeing

why in like bigger programs um you need

to be able to create some objects that

you want to be able to make constant and

why they were useful for your compiler

so for now I just want to teach you guys

the very basic so in the next story

we're probably going to be covering

constants a little bit more because

there's actually a lot to go over with

constants and classes so for now that's

all you get thank you guys for watching

don't forget subscribe and I'll see you

guys in the next video