hey everyone I hope you are safe and doing good so in the series of learning C++ programming language in the previous video we have seen what are ex specifiers private public and protected right now although we haven't discussed protected because that is used in inheritance and that thing we'll discuss later in the C++ course right so we have discussed private and public with help of examples right now in this video we'll see how to implement class methods you know I hope you know what are class methods these are simply functions but these functions are not free floating function in the program these function are you know bound to a specific class that is why these are known as methods right class you know member functions or methods we can say more specifically if we say then we uh you know uh call them member functions right or methods also we can say right now how to implement those methods till now we were implementing or we were defining those methods inside the class only right those deposit and uh withdraw and all inside the class only we have just defined those functions so right so that is also fine but there are three ways basically to implement class methods one is inside the class second is outside the class and third is separate the specifications of those methods from the implementation from the actual definition right so declaration and definition would be different I mean this inside the class outside the class this would be obviously within the same file whatever we create like CPP program right but here we divide our project or that our program into two files one is file and one is CPP file in Ed file only the Declaration of the class and those methods only within that class no implementation of those methods and one separate CPP file where we Implement those methods right so generally uh in software development when you're using this oops concept generally that would be divided the project or that thing would be divided into two files one is do H file one is RO C file we don't put everything in a single file when you're are dealing with the projects large projects right at industry level this is recommended just put your Declarations of your classes functions Global variables macro definition in a separate file and the implementation or definition of those thing in a separate file right you'll be habitual of using these two files and all once you know we'll be uh moving further in this course And discussing projects and more programs right so three ways to implement class methods I'll show you all the meth uh three ways inside we have seen to some word but I'll show you that thing also how to implement outside and how to do this thing with the help of these two files this already we have discussed uh in one of the coding exercise not in the previous video but you know I think previous two previous video we have discussed one coding exercise there we have taken two uh you know classes and do CPP you can go and watch that video also okay now first let's see inside the class how to define class methods inside this uh class so let's create a new file first within this C++ forace only okay so let's have a class account we have string name and balance okay this is undefined because you are not using those STD the name space I haven't used here using namespace STD that is why you have to separately include STD and you know the scope resolution operator fine okay these two are private because I haven't specified anything here and let's take method now public so see I have told you in previous video These are these attributes name and balance are private so if you want to access or set or get the values of these things we generally use getter and Setter methods right so let's take one method set balance and balance is equal to whatever the amount you pass that would be the balance and one is get method so it is going to return the balance so return type would be in if you want to check out the balance you just call get balance and it is going to return whatever the balances right so these are two getter and Setter methods so this is the way we are defining these methods or implementing these method inside the class so this is the first way right but generally this method is used for short and you know inline functions the functions which are having only one line of definition which are very short sweet right like this I have only one line of definition of this function so these are like type of short function or inline function right now I have one more function deposit or any other function which is having 2 3 4 lines of code right so obviously we will not Define that in within the class that is not a that is not recommended you can Define that but that is not recommended because we want our class to be you know a short and sweet and crispy what I'm what I'm you know saying about this class these words crispy and sweet short class right so that easily by looking at that class we get the idea these are okay these are the methods these are the you know attributes and all it's not like that we have three four functions and every function we are defining within the class only right the class would be very lengthy right that would not be recommended so inside the class just Define those functions which which are considered which can be considered as inline function uh mainly those getter and Setter methods these we Define within the class so this is one way you can Define the methods within the class only second way is outside of the class now suppose I have two more functions I want to deposit something and suppose uh I want to withdraw something so I'm just declaring these methods within the this class I'm not defining I'm not implementing this within within the class I'm I'm going to implement these outside of the class but with that same CPP file within that same you know uh file program so outside of the class we can Define it's not compulsory that you have to Define these within the main function only outside of the class just Define but how to define proper syntax you have to follow you have to tell that this deposit it's not like that directly write word deposit and something like this like double money and here just what we do is we update the balance right something like this see we cannot access balance the balance is undefined because when you declare when you define a function something like this it will search for a free function because this function is not bound to any class or any particular thing it is a free function word deposit this is kind of regular function we are using right so you have to tell the compiler that this deposit function is you know bound is bind with a specific class you have to tell that thing right so how to tell that thing first the return type then the class name this thing is important then scope resolution operator then the function name and then see now the there is no error now as we know like we have tell the compiler that that deposite function is from this class account and yeah obviously this is public method so using this public method we can access balance which is a private class member private attribute of this class right it's private if you want to specify then you can write down here private this also will work right so there's no error now this is how you can Define outside of the class same suppose let's define this withraw also okay here we don't have money uh we have the you know the parameter name this name is amount Al so you can write down here amount don't worry it's okay any name you can take but this and this should be same right so this is how we can Define outside of the class right now if you want to check like if they are working or not then let's create an object of this class in main I hope you can see the coding class name is account and uh let's take here account one the object name right right and we haven't taken anything to set and get the names so directly here only I just write down name equal to jti katri or using get and set methods you can just set the name right like we have set the balance so account One account 1 dot see here you will not see any uh option of accessing those name and balance because these are private all these things are public you can access these things so if you want to set the balance just call this and some balance let's set right now if you want to check out the balance then just call this get balance and obviously this is going to return get balance so directly if you write if you call this something like this you will not get any output let's me let me just run this and and I'll show you so you not getting any output because it is returning something get balance but you are not accepting that thing somewhere you're just calling so either you take a in variable and something like this or you can just directly call this in see out whatever it will return this will print so STD you have to use the name space in which this C out is now let's run this and C 10,000 rupes you got it [Music] now right so this this is how you can Define and declare or if you want to just call all other functions do deposit and I want to deposit some [Music] money and now I want to check the money account one that balance has been updated or not so obviously see out and before that STD let's run this and see the updated balances okay let's just first comment out this sline so first balance was 10,000 then it was suppose let's 10,000 again add so it would be 20,000 the balance would be 20,000 see 20,000 you got it right now third thing now we are going to separate the specifications from the implementation right so for that we'll create one H file one CPP file right and see let me just show you that thing also we will just take this class right and we will let's just or let's create a new file and I'll show you implement 3.h because this is the third way to implement so here we will take only the Declaration of the class that's it no definition at all right so from this we just take this class we cut this class from this CPP file and just paste this here right so yeah we are using this string here so obviously what you have to include now we have this thing only the class although we have these functions inside the class we have defined that thing we can do or you don't want to Define you can just you know uh just declare these functions and Define in other CPP file but let's keep it as it is we I'll show with these two functions right okay now uh in this cppp file we just Define these functions and we are calling these function with this one way is this this also we can do right in within this only we have main function but here one problem is C this account we don't know like where this account from where this this account class is where because in within this this CPU file there's no account class right this account class is within this do file so you have to include that do file first here hash include and how to include so you go to account. H sorry it's not account. we have second name Implement 3. right now see there is no error and the there are not angular brackets angular brackets are used this kind of bracket are used to include those you know standard system libraries those are defined string iio string vector and all this is we have our own module of our project right so you have defined your own you know module with do extension so that is why to implement these kind of thing we use double quotation mark this is the you know Convention of the rule kind of rule right now here we have defined this and we are just accessing so let me show you just run this and we'll see what output you getting you should get 20,000 here also right let's run this see output is 20,000 although it will be a little bit slower because it will check all the hash include preprocessor it will check okay it's in do file so it will check this file and all right but here one problem is if you include this doed file two times suppose uh by mistake I have I have included here also by mistake so let's run this and see it will show you one error see there redefinition of the class account previous definition of the class account two time you have defined this class here here here also right so to you know take care of this thing what we can do either you have to take care only one time you have to remember include this class or second thing maybe you are dealing with large projects we don't know like suppose one uh one place we have included that class at some other point also we are using those classes from H file the files again we want to include again we have accidentally include then will give so we can do one thing to avoid this thing we can use hash include guards right and what is that thing let me just show you before this just include hash if n def if andf and then the guard name so any name you can take but generally we take like those name as name of our file so Implement three is our file so generally we take that that name or we can take this account also the class name or any name ABC also you you can take suppose I'm taking here A B C let's see underscore H but the name should be in caps right that is general you know Convention of writing hash include so and this would be preceded and succeeded with underscore then hash Define hash ABC underscore hcore underscore right and after that after completion hash and if this is compulsory right this is kind of we can say macro right now hash if n death we can say it will avoid you know uh to be included the same same header file multiple times within the same CPP file do CPP file or the same translation unit more precisely if you say right or for now you don't understand the translation unit we just I just call uh within the same cppb file right so now see now suppose I have included this two times and let me just show you run this and I'll show you the output see it will not give any error 20,000 why so because once it will see hash include and all it will go to this dotage and it will see the hash include guards if not defined it means N means if not defined this header then Define this right and whatever is there after this hash defin line that would be included and all till this end if right now again it will see hash include Implement 3 again like it will check this so the first condition is if not defined ABC and H I mean the name of that has include but it has already been defined because we have already you know uh imported or included that header already uh previously right so already defined so this will not give true if not defined means yeah it will give false because already defined so that is why we will not enter here and directly and if that's it so whatever is there within this whatever the class and all that would not be included again or that would not be defined again that is why you will not get any error that is why we generally in file we use these hash include guards these are known as hash include guards the name you can take anything ABC generally we take Implement three I mean the name of that module and all right and we preceded these are preceded with underscore and ending would also be with then underscore right you can take any name and you can check if you not take these uh underscore or not that would work or not that is up to you right and one thing also we can do generally we do uh in Project is we have do file just Define this and in one CPP file let's create one CPP file Implement 3. CPP the same name H header file is having same name with CPP and here within this CPP we are having definition of these functions so let's define these functions contr X from this and let's define here right now obviously for this you have to include Implement dot h you got [Music] it here we have just the Implement do H and we are just defining these two function that's it nothing else no main function at all and one more main CPP file there we have our main function we just create the object when we can call that's it so we have three files now as many files as you can have in your project right don't worry so in one I hope you are getting what I'm trying to you know uh do here in CPP we have this this and let's create one more file and let's name this only the main main CPP or maincore implement 3. CPP because I guess I'm having main. CPP in this workspace so it will not work so mainor Implement 3. CPP or you just create another project of another folder for your project and keep all the files related to that project in that folder only main Implement so here we have just these things contrl C and just contrl V so just include this only once delete this although we have used those guards so you not get any error here I'm just creating account object I'm just accessing those things that's it so three files basically we have right so CPP file all the CPP file on of those project would be uh compiled and create their you know they'll be created those what we can say object file right and the Linker will link all these object object file right and then give you the output right you just have to run your main file there we have main function obviously to start compilation or to run the program the driver is main only so you will run this file only right so let me just show you I don't know I don't think so it will work okay let's show you otherwise we'll run it manually let's run this main file here we have main let's run the this and definitely you'll get error see undefined reference to account deposit and all okay you have undefined reference to this account 1 do deposit because this function is defined in this Implement 3. CPP so this Doo file object the Linker is not able to link these files okay let's run this manually and I'll figure out this and in the next video we'll see that thing also so one more way is let's run this okay it will give better we'll run this with the help of uh proper commands so the command is g++ and two two CPP file we have do CPP file right one is main underscore Implement 3 CPP and the next is Implement 3 CPP I hope it work Implement 3. CPP I'm not sure see this is the command I hope you can see this okay let me just fine to CP file I'm going to compile so let's run this and yeah we got this definitely we [Music] have that a.exe file I don't know where it is but let me just run this how to run this this and a.exe see 20,000 we are getting the correct outut right but manually uh we are you know compiling and running this program because because of some error Linker is not able to link all the object file of all these CPP files but I'll figure out this thing you got this 20 ,000 we got this one this is the correct output so like this here we have three files one is file just having the Declaration of our class right or defining to uh this definition of two those get and set methods one is CP file that is having only a definition or implementation of those methods third one is our main file there we are just creating object and calling the all the class members or methods of that class right so when you will be working on large project now then you will do this method you will separate up your project in different different files there can be three four five six multiple files one would be file and other would be CP file and all and one is main file right I hope you got this the three uh methods inside the class outside the class and how to define how to separate the specification from the Imp mentation what are hash include guards and all I think video is lengthy a little bit so if you're watching till the end till now then please let me know in comment section and the target I'll keep I generally don't do this thing but just to keep me motivated I just uh keep the target of the likes on this video 500 only right so now that's it for this video