declare

Separating a C++ class into a .h and .cpp files



Sharing buttons:

okay so let's now take a look at how we

can go about splitting this up into a

separate dot H and dot CPP files okay in

C lion-o the easiest thing to do come

over here

oops sorry I'm going to right-click on

on the project itself and I'm gonna say

I want to create a new C++ class right

it asks me for the name and you'll

notice it says it does say that it's

going to create both the CPP and the H

I'll make sure to add it to to both

targets and we'll click OK

ok so now we have we have a our our dot

H file and our dot CPP file so now we

can go about moving things over

accordingly okay so remember the dot H

file the header files are supposed to be

used are basically there for before the

declarations the C++ files are there for

the implementation ok so the easiest way

of doing this if you if we've done it in

this fashion basically what we're gonna

do is we're gonna grab the whole class

right here and I'm even gonna grab

actually leave that for right now ok so

I'm gonna just cut this out of main all

together I'm gonna come over into into

student dot H and we're going to we're

gonna paste this in here ok so there it

is now a couple of things the the pound

if not define pound defined remember

those are conditional compilation

directives right so the idea basically

is that that we want to make sure that

that the student dot H header file since

it declares what the class is um is only

included once in the project if somehow

it ends up this this will and this will

in fact prevent it from from ending up

being included multiple times in in all

the files of the project

okay so now that we have this I'm gonna

leave that there I'm also gonna go back

over to I'm going to go to student C++

and I'm gonna paste that whole thing in

here okay just like so alright so

essentially we have kind of two copies

now we'll go back and and edit things

accordingly okay so over here

essentially what we want is is the any

declaration types of stuff so the actual

implementation of the code we're going

to remove now right away you'll notice

it's complaining about complaining about

string right because that actually is

supposed to be in the STD namespace so I

can add I can add in a using directive

about that I may actually also need a

pound include string okay let's see yeah

there we go

happy now okay so as I said all the

implementation stuff pretty much all

that good code that we just that we

wrote before we're gonna get rid of okay

so I'm gonna select that whole thing and

just put a semicolon there okay same

thing here I'm gonna grab this whole

thing and we'll throw a semicolon there

I'm gonna grab all this part here put

the semicolon there we leave our friend

in just like that okay so that is

there's our dot H file right

so you'll notice it tells us which

methods we're going to we're going to

have which member functions along with

our friend but it does not provide the

the actual implementation for those okay

the other thing is I'm going to grab our

overloaded operator again cut that out

and we'll paste that one in here again

I'm going to do the same thing thing

over in the in the CPP file okay now

once again I'm just gonna select all

that and put the semicolon in there okay

so you notice all this stuff goes

between the if not defined defined and

the and the

NDF part okay so those whoops semicolon

not an L right so there are all of the

declarations for the for the various

functions okay

now let's have a look at the C++ file

okay so the first thing we're gonna do

in the C++ file we're gonna get rid of

the the class part so that is this class

part and we don't need the instance

variables in there any longer either

also of the public label doesn't make

sense okay so all those go away then I'm

gonna scroll all the way down and just

get rid of the ending part right that is

that part right there okay so I have my

code sort of indented here let's see let

me go ahead and just select that I

should be able to let's see there we are

on indent okay

so we have that stuff now let's let's

see what other stuff we need to we need

to handle here so a couple of things

first of all I'm kind of surprised I'm

not actually getting an error about it

but notice um because I am using Ostrom

here and it's actually not complaining

so kind of surprised about that because

I would imagine I would actually need I

Oh stream for that

alright back over here once again let's

use our using namespace STD okay so

that's pretty good since we are

including that header in here

those include should actually should

actually follow us through now some of

the issues that we're having okay so

let's start taking a look at at the

errors here right so notice it says I

don't I don't know what name is right

doesn't know about that at all okay now

so what part of the issue that's going

on here is that now we've pulled all of

these functions that were part that

we're inside of student outside right

there is no class students surrounding

all of this

okay so now this has a different meaning

now as far as C++ is concerned this is

just a regular old function named

student okay but that's not what we mean

what we want to say is here we're

providing the definition for the student

function that is in the student class or

the constructor so the way of doing that

is to use this student : : right so now

I'm saying oh I want the student member

function right that is in the scope of

okay so and of course since it has the

same name as the class that is the

constructor notice all of those little

errors went away I'll get rid of this a

commented out code and only need to have

it there now down here when we're

talking about get name again we're not

talking about a standalone function

called get name we're talking about the

get name function that is inside the

student class let me just go ahead and

paste that in there right and again you

see voila it's happy it finds a finds

name it's good to go okay notice that I

didn't put student : : up here right see

the issue is the constructor never has a

return type specified right so that's

why we have that right at the front if

you've really thought about it

the the type that a constructor returns

is actually the same as the class so

it'd kind of be redundant it would be

student student right so here again

we're saying that the getname method is

part of the student class same thing

down here we're gonna say okay set GPA

is again a member of the student class

okay you can see those errors just just

disappearing now down here notice I

don't put student : : why because the

put to operator to overload it to make

it actually work it can't be a member

function okay some operators in C++ we

can actually overload them as member

functions we can't do that we

the with the put to operator on the pro

tip on this um the reason that you can't

the first parameter notice is Ostrom if

the first parameter actually were

student if that's how the put to

operator worked then we could actually

do that but it isn't

according it's it's it's a no stream so

it has to set outside okay

and so I don't have to add student : :

to that you notice I didn't actually

have to do anything else to this to make

that one work okay so that's how we've

how we've broken this out again kind of

come over here make sure that all that

looks good let's go over to Main

of course main now notice is is not

compiling why is it not compiling

because it doesn't know about student

right to make it to get it to understand

a student I have to include my new

header file namely student dot H and we

used the quotes around that as opposed

to the angle brackets because this is

something that's in our project alone

and not part of the part of the standard

of the the standard include path alright

let's go ahead and and build this thing

on what I'm actually going to do since I

did make a lot of changes and so forth

I'm going to do a clean first so that

kind of deletes anything that that is

already compiled so we'll go ahead and

clean that right and now then I'm going

to go back to I'll run it so that should

that should recompile everything for us

and and run that let's see what we get

okay so I run that and sure enough there

we are okay so we have a we did get

everything compiled we still get the

same result okay so that gives you an

idea about how you could go from from a

call the class code in in one section to

spreading it across the files and a

little bit about about the scoping

operator again that that colon colon

operator and where it needs to go and

why okay hopefully that helps in the

next video what we're going to do is

we're going to actually create a class

that is a collection of students and and

look

at how we can how we can then manipulate

these student objects from from that