Class Header and Implementation

8 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
€96.21
List Price:  €134.70
You save:  €38.49
£79.77
List Price:  £111.68
You save:  £31.91
CA$143.95
List Price:  CA$201.54
You save:  CA$57.58
A$160.33
List Price:  A$224.47
You save:  A$64.14
S$135.93
List Price:  S$190.31
You save:  S$54.37
HK$776.63
List Price:  HK$1,087.32
You save:  HK$310.68
CHF 90.06
List Price:  CHF 126.09
You save:  CHF 36.02
NOK kr1,139.30
List Price:  NOK kr1,595.07
You save:  NOK kr455.76
DKK kr717.87
List Price:  DKK kr1,005.05
You save:  DKK kr287.17
NZ$177.29
List Price:  NZ$248.21
You save:  NZ$70.92
د.إ367.26
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,945.58
List Price:  ৳16,724.30
You save:  ৳4,778.71
₹8,518.63
List Price:  ₹11,926.43
You save:  ₹3,407.79
RM448.65
List Price:  RM628.13
You save:  RM179.48
₦154,771.52
List Price:  ₦216,686.32
You save:  ₦61,914.80
₨27,852.21
List Price:  ₨38,994.21
You save:  ₨11,141.99
฿3,415.60
List Price:  ฿4,781.97
You save:  ฿1,366.37
₺3,525.07
List Price:  ₺4,935.24
You save:  ₺1,410.17
B$620.23
List Price:  B$868.35
You save:  B$248.12
R1,852.48
List Price:  R2,593.54
You save:  R741.06
Лв188.12
List Price:  Лв263.38
You save:  Лв75.25
₩146,004.73
List Price:  ₩204,412.46
You save:  ₩58,407.73
₪366.53
List Price:  ₪513.16
You save:  ₪146.62
₱5,844.26
List Price:  ₱8,182.20
You save:  ₱2,337.93
¥15,707.80
List Price:  ¥21,991.55
You save:  ¥6,283.75
MX$2,012.91
List Price:  MX$2,818.16
You save:  MX$805.24
QR364.43
List Price:  QR510.22
You save:  QR145.78
P1,382.66
List Price:  P1,935.78
You save:  P553.12
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,089.49
List Price:  E£7,125.49
You save:  E£2,036
ብር12,725.10
List Price:  ብር17,815.66
You save:  ብር5,090.55
Kz91,190.88
List Price:  Kz127,670.88
You save:  Kz36,480
CLP$99,038.09
List Price:  CLP$138,657.29
You save:  CLP$39,619.20
CN¥729.75
List Price:  CN¥1,021.68
You save:  CN¥291.93
RD$6,065.06
List Price:  RD$8,491.33
You save:  RD$2,426.27
DA13,522.10
List Price:  DA18,931.49
You save:  DA5,409.38
FJ$232
List Price:  FJ$324.81
You save:  FJ$92.81
Q770.19
List Price:  Q1,078.30
You save:  Q308.10
GY$20,913.42
List Price:  GY$29,279.63
You save:  GY$8,366.20
ISK kr13,961.60
List Price:  ISK kr19,546.80
You save:  ISK kr5,585.20
DH1,005.73
List Price:  DH1,408.06
You save:  DH402.33
L1,844.84
List Price:  L2,582.85
You save:  L738.01
ден5,920.21
List Price:  ден8,288.54
You save:  ден2,368.32
MOP$800
List Price:  MOP$1,120.03
You save:  MOP$320.03
N$1,842.15
List Price:  N$2,579.09
You save:  N$736.93
C$3,678.44
List Price:  C$5,149.97
You save:  C$1,471.52
रु13,621.60
List Price:  रु19,070.79
You save:  रु5,449.18
S/372.86
List Price:  S/522.02
You save:  S/149.15
K405.52
List Price:  K567.74
You save:  K162.22
SAR375.60
List Price:  SAR525.86
You save:  SAR150.25
ZK2,766.47
List Price:  ZK3,873.18
You save:  ZK1,106.70
L478.77
List Price:  L670.30
You save:  L191.52
Kč2,420.15
List Price:  Kč3,388.31
You save:  Kč968.16
Ft39,675.18
List Price:  Ft55,546.85
You save:  Ft15,871.66
SEK kr1,106.30
List Price:  SEK kr1,548.87
You save:  SEK kr442.56
ARS$102,565.23
List Price:  ARS$143,595.42
You save:  ARS$41,030.19
Bs692.24
List Price:  Bs969.16
You save:  Bs276.92
COP$438,826.54
List Price:  COP$614,374.71
You save:  COP$175,548.17
₡50,760.09
List Price:  ₡71,066.16
You save:  ₡20,306.06
L2,539.15
List Price:  L3,554.92
You save:  L1,015.76
₲780,390.60
List Price:  ₲1,092,578.06
You save:  ₲312,187.46
$U4,451.49
List Price:  $U6,232.27
You save:  $U1,780.77
zł411.19
List Price:  zł575.68
You save:  zł164.49
Already have an account? Log In

Transcript

Hello and welcome to the 74th tutorial in the c++ provinces. And in this part we'll be looking at class headers implementation for us. Up till now we have been creating classes within the main dot cpp file for the purpose of these tutorials, but when you create LOD reputations, they should be separated into header and implementation roles. They fall with the dot h extension is the header file. And the file with the dot cpp extension is the implementation file. If you notice the main file is called main dot cpp.

I didn't mention before, and this is a implementation for Okay, so what we're going to do is just going to here, open up our project I'm going to create a header and an implementation file first. So we're gonna do this one click New foil. So we're going to create Have a file. in Visual Studio it has obviously its own system not too difficult if you don't know you can google it or messages via YouTube, or comment on this video. And for this farmers gonna call it extra class need to provide the target which is basically our application, click Create. What is great about Visual Studio sorry, going off on one I should be doing this as well.

I'm sorry about that by click again. And now we're going to create the implementation file which is CPP. So this one right here. This was one extra class note, we don't actually have to put the extension here at all we just put the name and then you pass the extension itself. So what we want to do go to extra class dot h and we want to first of all, do a preprocessor check. So we're going to hash if not defined, and this may whatever you want, basically, don't use it again, this definition.

And what we recommend is and where a lot of other places would recommend as well is doing like two underscores the name of your class separated a separate word by another school and then at the end, underscore to underscore. So when they follow the first load it presently it checks. If not defined extra class, if it isn't defined, then what you do is define x. Last thing, just do hash and the F, and the contents of this file, the entire contents will go within here, aim, I think, why are you doing this? The reason we're doing this, and we're illustrating what we've actually done on the code, or what, what the actual benefit of this is, we're all sound but the reason we're doing this if you're into the file, and including straight after you've included the four toys, obviously, this will prevent the content from being coded twice because if the contents are included again, you can't have a class with the same name.

Obviously, you can have it within different namespaces. But that's not the purpose of this tutorial. If you have a class with the name extra class, like we have, we have two classes that name the compiler doesn't know which one you're referring to. So you can only have one class name, saying we have variables and methods. And so this will prevent the application or the file from including this file twice which is pretty cool. And in here with some critter class, extra class, really simple class, public abstract class we print on the shop create a variable into the COI Now all we're going to do is actually implement our extra class CPP.

And first of all, we're just going to hash include. And then we're going to do quotation marks within within quotation marks. Cool. So custom font. And what we're going to include is extra class dot heritage. We're going to be implementing all of these basically a method on the constructor within this separate file, instead of doing it in one file.

And we're also going to hash glue this in unnecessarily. This just depends on the functionality of your application to oil stream. We printed some software so we need it. First of all, we're going to implement our constructor from the extra class colon colon, extra class. And here we're gonna set it to zero by default is good to initial variables then do void that's the plus print function STD see print and nine just need to spell c correctly and that is the extra class implementation done. So what we can do now is go to our main dot cpp so whenever we want to use this class we just do hash include and then do quotation marks again because we created the class and it's not a lot before the NG x cluster he donating for the DDP let's do the head end which is great.

And now in here, what we can do is create a object x class we'll call it easy. We'll do easy to print function, d e c.on. Good luck. To do STD C, and usually on STD line, if we run this, we get print from printed out which is from here. And we get all printed out as 90, can we set it to 90 here before printed? Yeah.

Another great thing about separated man, if we had several CPP for which an application or a property would we have several files that are creating that class or duplicating a generic class in every single file, you can just include it. And if you need to make a change in here, maybe add a bit more functionality, you function is changing here, and it propagates throughout all the other files that this extra class or any class have been included in, and therefore you can use all this new and updating and change functionality. Before we go. We're just going to demonstrate this Right here why we need this, we talked about it, but let's just demonstrate it. So let's just copy and paste this, run it, it runs fine. But if I were to remove these two lines, the the key line here is this, but I will remove both of the lines for now.

And when it builds out, and the bill fails redefinition of x class, but if we were to remove one of these and run it, they'll succeed because we only got one extra class. But if we put this back in the files, because we've essentially what the compiler is seeing is this. That's what they'll be seeing the compiler will. So it's looking I think, in the next class, I okay. And then he looks down here. And then there's another expert class sorry about the notification in the number extra class, so which one's the right one so what you want to make sure You have these in every class header, that you create knowledge per se p, just a header.

Obviously, make sure these hash defines or different and these checks are different. Because that also can cause issues as well if you accidentally forget to change the name because what you'll probably end up doing when you're creating all the classes, your main copy and paste an existing class potentially, and then just change the name and change a few things that you need to change in there. And you might forget to change the hash define, we do some time and that causes some error. So just make sure you change that. In the next part of this series, we're going to be looking at lists. If you have any questions, feel free to message us or support us on our systems career.

Okay, the email will be in the description. You can comment on this video or just directly messages via YouTube, or the recording for source code will also be in the description. And as usual, thank you for watching and hope you have a great 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.