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
€95.97
List Price:  €134.37
You save:  €38.39
£79.80
List Price:  £111.73
You save:  £31.92
CA$139.82
List Price:  CA$195.75
You save:  CA$55.93
A$153.75
List Price:  A$215.26
You save:  A$61.51
S$134.64
List Price:  S$188.51
You save:  S$53.86
HK$778.36
List Price:  HK$1,089.74
You save:  HK$311.37
CHF 89.34
List Price:  CHF 125.09
You save:  CHF 35.74
NOK kr1,107.14
List Price:  NOK kr1,550.05
You save:  NOK kr442.90
DKK kr715.75
List Price:  DKK kr1,002.09
You save:  DKK kr286.33
NZ$171.37
List Price:  NZ$239.93
You save:  NZ$68.55
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,945.63
List Price:  ৳16,724.36
You save:  ৳4,778.73
₹8,442.99
List Price:  ₹11,820.52
You save:  ₹3,377.53
RM446.75
List Price:  RM625.47
You save:  RM178.72
₦169,271.38
List Price:  ₦236,986.70
You save:  ₦67,715.32
₨27,777.22
List Price:  ₨38,889.22
You save:  ₨11,112
฿3,446.26
List Price:  ฿4,824.91
You save:  ฿1,378.64
₺3,454.90
List Price:  ₺4,837
You save:  ₺1,382.10
B$580.04
List Price:  B$812.08
You save:  B$232.04
R1,811.35
List Price:  R2,535.96
You save:  R724.61
Лв187.69
List Price:  Лв262.77
You save:  Лв75.08
₩140,436.95
List Price:  ₩196,617.35
You save:  ₩56,180.40
₪370.16
List Price:  ₪518.24
You save:  ₪148.08
₱5,893.31
List Price:  ₱8,250.87
You save:  ₱2,357.56
¥15,475.45
List Price:  ¥21,666.25
You save:  ¥6,190.80
MX$2,042.64
List Price:  MX$2,859.78
You save:  MX$817.14
QR364.56
List Price:  QR510.41
You save:  QR145.84
P1,367.06
List Price:  P1,913.94
You save:  P546.88
KSh12,945.58
List Price:  KSh18,124.33
You save:  KSh5,178.75
E£4,964.52
List Price:  E£6,950.52
You save:  E£1,986
ብር12,237.67
List Price:  ብር17,133.23
You save:  ብር4,895.55
Kz91,290.87
List Price:  Kz127,810.87
You save:  Kz36,520
CLP$98,658.13
List Price:  CLP$138,125.33
You save:  CLP$39,467.20
CN¥724.22
List Price:  CN¥1,013.94
You save:  CN¥289.72
RD$6,024.63
List Price:  RD$8,434.73
You save:  RD$2,410.09
DA13,426.15
List Price:  DA18,797.15
You save:  DA5,371
FJ$227.57
List Price:  FJ$318.61
You save:  FJ$91.03
Q771.64
List Price:  Q1,080.33
You save:  Q308.69
GY$20,913.50
List Price:  GY$29,279.73
You save:  GY$8,366.23
ISK kr13,966.60
List Price:  ISK kr19,553.80
You save:  ISK kr5,587.20
DH1,005.63
List Price:  DH1,407.93
You save:  DH402.29
L1,821.98
List Price:  L2,550.85
You save:  L728.86
ден5,904.20
List Price:  ден8,266.12
You save:  ден2,361.91
MOP$801.48
List Price:  MOP$1,122.11
You save:  MOP$320.62
N$1,812.81
List Price:  N$2,538.01
You save:  N$725.20
C$3,678.31
List Price:  C$5,149.78
You save:  C$1,471.47
रु13,500.25
List Price:  रु18,900.90
You save:  रु5,400.64
S/379.05
List Price:  S/530.69
You save:  S/151.63
K402.47
List Price:  K563.48
You save:  K161
SAR375.40
List Price:  SAR525.58
You save:  SAR150.17
ZK2,764.29
List Price:  ZK3,870.12
You save:  ZK1,105.82
L477.77
List Price:  L668.90
You save:  L191.12
Kč2,432.37
List Price:  Kč3,405.42
You save:  Kč973.04
Ft39,496.05
List Price:  Ft55,296.05
You save:  Ft15,800
SEK kr1,103.50
List Price:  SEK kr1,544.95
You save:  SEK kr441.44
ARS$100,374.93
List Price:  ARS$140,528.92
You save:  ARS$40,153.99
Bs690.75
List Price:  Bs967.07
You save:  Bs276.32
COP$438,931.09
List Price:  COP$614,521.09
You save:  COP$175,589.99
₡50,918.63
List Price:  ₡71,288.12
You save:  ₡20,369.49
L2,526.16
List Price:  L3,536.73
You save:  L1,010.56
₲780,388.98
List Price:  ₲1,092,575.79
You save:  ₲312,186.81
$U4,261.82
List Price:  $U5,966.72
You save:  $U1,704.90
zł416.31
List Price:  zł582.85
You save:  zł166.54
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.