Constant Members in Class | C++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to nourish aid this is Kishore and now I'm going to discuss about constant numbers okay now I'm going to discuss about constant members actually in C language we are declaring constant values okay and in C language how to define a constant okay it is the thing action generally constant okay little data type let us identify our variables okay unvalued here the point is first of all we have to define constant keyword it is a keyword next datatype unvarying - well and what is the rule for constant it should be defined only one time for example it is a normal variable int a equal to 100 now there is no problem at all suppose here I am NOT going to provide 100 here and later I am going to provide a equal to 100 and here also there is no problem at all because of now a is a variable variable means were changeable at any time that means now we can change the a value at any time okay but here I am going to discuss about the constant now suppose I have declared a favor elastic constant a equal to 100 now in this line there is no problem at all but here you are going to get error okay see this here value is same actually constant meaning is what fixed value simply take constant means what fixed value we are not able to change and here the interesting point is actually value 100 okay here also hundred now okay means value change not at all but it never supports because of there is a rule for constant what it is the constant value should be defined only once at the time of declaration the constant value should be provided at the time of declaration only okay here already we have provided now once again another initialization not allowed for example this time int that's it Oh the constant IND actually value defined or not defined okay now I am going to provide a equal 200 actually it is accepted no not at all that is why here the most important point is what whenever the constant is declared you should have to provide the initial value also further initializations are not that or that means what constants are nothing but fixed values and best constant value we know that pi value okay suppose pi equal to three point one four okay five L equal to three point one four it is never changed because of it is a scientific value okay and now in this situation PI easy what local variable or global variable what it may be leave it now it is a normal variable but in C++ we are having class class is having what data members now in this session I will cover how to define a data member as a constant okay in this class I will cover how to define the data members of a class as constants okay and now constant means what first think it is a fixed value and we are not able to change the constant they are use it too okay make the object values constant that means they make to their use it to make the object values that make two objects stays in state a constant plus what static data members already we have discussed okay they are access to divided classes as well an object here now it is what constant they are going to be make the constant state pixel okay so now how to declare and how to access static data members and one more thing we are having two types of strategies one is static data members another one is static member functions okay one is static data members another one is static member functions first of all I am going to give how to declare a static data member okay static data member is user to okay and fix the values static data members are user to fix the values of a class okay they are going to initialize an object with sexual values now how to declare the static data members for example class okay here simple interest I am going to declare one simple interest plus generally simple index means we are having a formula PTR by 100 here P stands for what principal amount T stand for what time R stands for what rate of interest now suppose float okay suppose principal amount okay and ind time okay here P stands for principal amount T stands for time and rate of interest I want to make constant okay here the rate of interest is I want to make constant that's why here it is the keyword constant okay and radar windows means wat floating that's why fruit okay and here our next in previous example in c language we are using the constant mem and i have discussed about how to declare the constant numbers in c language what is the rule for constant members it is initialized at the time of de creation only okay now the most important point is static constant member should be initialized at the time of declaration on but here we are using the constant data member inside a class under as per class rules as per class rules we are not able to provide the values okay at the time of class declaration directly data member never initializer we know that now they are private data members as per class hiding concept is what private data should be accessible with the member functions of class only but here the point is what it is a constant member and constant member actual rule is what it should be initializer at the time of declaration for the initial editions are not eloped but here the point class never allows to direct English lies now how to initialize a constant data member symbol now declare the constant data number here okay next now how to initialize a constant member now the rule for initializing a constant number is the constant data member the constant data number should be initialized with the constructor or constructor list of the same class it is a major rule okay every constant every constant data member should be initialized with the constructor or construct a list of the same class that is why here public why because there is a real for constructed the constructor should be declared in public section it is a another rule for constructor is what constructor name should be matching e to the class that is what here if a class name is what symbol enters the size function name also simple interest and later constructor me the function that's a bracket now colon operator now colon operator next what is the striking constant member R that is why r okay let's say a C is the constructor and later : symbol on data member name are constantly remembering our and in brackets you have to initialize the value to 0.5 for example the in simple interest rate of indices 2.5 now simple interest is class name with constructor : under data member unvalued posit either open the brackets close the world now it is what constructor without any code and especially this constructor is designed for what purpose to initialize the constant data member it is the professor to initialize a constant next later suppose void show okay and to show the data first of all without initializer okay principal amount I'm also know that is why I am going to use the function now read the function read of what I want to read here in spool amount that is why principal amount is what float now float okay the principle Emma and the other one is what int t i TI means time here I'm going to give like this P equal to P a and T equal to T I now what happens we are sending the principal amounts on a month and some time now that amount and time is assigned to normal variables P and T and here P and T are what normal data members only R is a constant already our value initially you know now the remaining are what P entity and they are positive they are initialized like next wide show I want to show the come on otherwise float okay I want to find out what simple interest that is why float show off directly write like this return P into T into R by hundred that's so it is the formula already P values initialized T value initially now or well already initialize it okay now P into T into R by hundred now the class also close now it is how to declare the constant member it is how to initialize a constant it is how to use the constant number now how to call when calling is there we should have to go for main function okay now why it may and now we have to declare the object of this class s I suppose yes the object okay next here the point is constructor we are having okay to say of our 2.5 what happens whenever the class is created whenever the class is created automatically that value becomes our value two point five okay now here I am going to write like this s dot okay read function test dot region of thousand comma for example ten months but so now the thousand is received by principal amount it is positive 2 P and ton is received by t TI is positive P and already our value initialized with two point five now s dot shows C out okay interest equal to just dot show now let's see here program closed now it is going to show what the interest in the Smith they returned three into T into R by hundred now it is give simple interest okay it is how to declare the constant data number on how to define it on how to exit okay next now what is a constant number function what is a constant number function I will show in this example I have disgusted what is a constant data number and how to access that now I am going to give you what is a constant number function on how to access the constant member functions constant number function whenever remember function is constant okay that is also called axis that is also called axial function that means whenever a constant number function is declared now inside that function you are not able to change the object values it never allows to modify the object values that is why constant data members are okay use it to use to fix the state of an object they are going to be fixed the state of an outfit doesn't allows to change of set of an object okay here how to access constant number function okay say this for example class some test and here int a comma B okay int a comma B next public next here void read inside this a value 10 B value to simple example I want to give sit now tell me here class name is what test KBR the what normal members okay data members and number function inside the read member function what we are doing a value initialized with depth and we value initially with 20 and here it is a normal number function X I am going for another function void show okay here a valley is what already a well initialize and here a value for example 30 and B value for example 40 now I am saying C out a equal to a and C out B equal to B next class also closer okay here watch it the class name is test and it is having two data members and two number functions here already invaded function without changes a B values with the ten point on show function also we have changed a value once again with 30 and 40 now later printing the value or now watch bouncing suppose it is the main function later test to T so T is the what object okay now the object is define let us screen clear T dot read okay fine now what happened T is the object and here a value becomes 10 B value becomes 20 when t dot read function is called a value becomes 10 and B value becomes talanted later you are going to call we are going to call show function now get CH now what happened was it when T dot show function is called inside the show what we are doing a value 30 B value 40 already a value B values are initialized with 10 20 now what happens they are replaced with 30 under 40 now they are going to replace it with 30 and 40 that so answer is a value 30 B value 40 according to this example according to this example we can change the data member values okay we can change the data member values through the member functions at any time through the member function at any time but my requirement is my requirement in this function ok the user is not able to change the AV values I want to make this function okay I want to make this function is not able to change the a and B means I never want to change the object state in this function ok then how to then make it like this wide shoe simply constant that's all now add this constant keyword to the function now what happens this will become a constant member function now this will become a constant member function actually constant means what fix it but here the values are going to be changed means object state is going to change that is why he reduce here now this program is not going to access a and B values it is only for printing okay that is why it can access the data but it is not going to modify the data that is why constant number functions are called accessors and here we have changing but it oh no that's why real member is called fire that's why here Reid is a modifier and constant this is a axis versus my Excel modifier both are what both car member functions but when it is a more different it is able to change the object state that means it can change the data members values when it is an access okay what is called access when a member function is declared with the constant keyword with the constant keyword it is called access under this axial member function is not able to modify the data members means it is not able to change the object state that is why this one is called only the accessor it can access but it is not able to modify that's why whenever you are trying to do this one reduce yes okay now it is giving the error it is what is a expert what is an Excel and what is a modifier function that's why we are having two types of number of functions modifier function acts are functions okay that's why excel member functions are used to okay access member functions are used to make the object not changeable okay and whenever an object is changeable whenever an object value are steady changeable it is called mutable object it is called writable and then it is not changeable it is called the immutable object here the point whenever we can change the object state then it is called mutable object whenever the value is not changeable means of this state is not changeable it is called immutable objects and immutable objects are possible with the XR member functions okay thank you thank you for watching [Music]
Info
Channel: Naresh i Technologies
Views: 40,679
Rating: undefined out of 5
Keywords: Constant Members in Class, C++ Tutorial, Kishore, c++, Constant Members, Class, Class in c++, c++ Training, Naresh IT
Id: lZjh-G8bt6g
Channel Id: undefined
Length: 20min 4sec (1204 seconds)
Published: Tue Apr 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.