# How to handle the Specification and Implementation files to be used in C++?

I am learning how to create Classes. I already created the Specification file(Rectangle.h) and Implementation file(member function Rectangle.cpp) separately. In the book the CD that came with it supposed to have ways to do it, BUT of cause I bought used and don't have the CD!!!

I already know how to #include Rectangle.h into the Implementation file. It's simple, just #include ""C:\\Users\\alanr\\Desktop\\C++ exercise\\Gaddis\\inout files\\Rectangle.h" for my directory in the laptop..................BUT it's so clumsy!!!

In a complete program that use this Class, I have to pull in the Specification file( header file) and Implementation file to use in the program, There must be an easier and cleaner way to do it like we just #include<iostream>, none of the C:\\..... stuffs. You guys have a standard way of doing this easier?

Do you create a folder to store all the header files and implementation file and use a special way to call them without having to writhe all the "C:\\....." stuffs?

EDIT: I forgot to say, I know you can physically copy the .h and .cpp file into the program using it. To me, that's even worst than writing "C:\\....." because you actually go and copy and paste every time you write a program that use the Class Object. There got the be a better way.

Thanks

Last edited:

Related Programming and Computer Science News on Phys.org
pbuk
Gold Member
There are a number of different ways to organise files, some of which depend on settings in Visual Studio. For now you should just include all the files for a project in a single folder and then you can just #include "Rectangle.h".

There are a number of different ways to organise files, some of which depend on settings in Visual Studio. For now you should just include all the files for a project in a single folder and then you can just #include "Rectangle.h".
Thanks

But I still have to have both files in the same place. So I have no choice but to copy those from the specification program and the implementation program into the new project that uses it. TWhat if I need to change the .h file, I have to do it over again?

Or just don't worry about it, just do it this way and wait for later when I learn other ways?

Thanks

Staff Emeritus
2019 Award
This has nothing to do with C++. You can have everything in one big .cpp file if you wish. Many find this to be a less than ideal organization, but it's perfectly legal.

It has more to do with Visual Studio.

FactChecker
Gold Member
What operating system are you working on and what are you using to compile? Are you using MS Visual Studio? Whatever tools you are using, there are always ways to put the header files in one directory and keep the implementation code someplace else. That is the only way that a large group of programmers can formally agree on the interfaces before the implementation code is completed.

This is question about VS, I am running on Windows 10. I have no issue understanding what this whole thing trying to do and the code. I just don't know how to put them together.

I am trying to integrate the Specification, implementation file to work together with the program that uses it. I have no idea how to integrate them together. The book just skip this all together and act as if it's supposed to just work!!! I typed in the Rectangle.h file and Rectangle.cpp file. There is no instruction in running the compiler on these two files. I have no idea what to do. I already put the rectangle.h into the calling project in the same folder as the source.cpp of the project. But there is no instruction what to do with the implementation file Rectangle.cpp. I copy the Rectangle.h, Rectangle.cpp and the source.cpp here. Can anyone help me? I don't have the CD from the book.

This is the Rectangle.h that define the Rectangle Class Object.
C++:
//Specification file for Rectangle
#ifndef Rectangle_H
#define Rectangle_H
//Rectangle class declaration
class Rectangle
{
private:
double width;
double length;
public:
void setW(double);
void setL(double);
double getW() const;
double getL() const;
double getA() const;
};
#endif //#pragma once

This is the Rectangle.cpp That has all the member functions.
C++:
//Rectangle implementation file
#include <iostream>
#include <cstdlib>
using namespace std;

void Rectangle::setW(double w)
{
if (w >= 0)    width = w;
else { cout << "Invalid width.\n\n"; exit(EXIT_FAILURE); }
}
void Rectangle::setL(double len)
{
if (len >= 0) length = len;
else { cout << "Invalid length.\n\n"; exit(EXIT_FAILURE);
}
}
double Rectangle::getW() const
{    return width;}
double Rectangle::getL() const
{    return length;}
double Rectangle::getA() const
{    return width * length;}

This is the source.cpp of the program that call the Rectangle class.
C++:
// 13.4 using Rectangle Object .h and .cpp
#include <iostream>
#include "Rectangle.h" //Needed for Rectangle Class
using namespace std;

int main()
{
Rectangle box; //define an instance of Rectangle Class
double rectW;//Local variable
double rectL;

cout << " this program will calculate the area of rectangle.\n\n";
cout << " Enter width = "; cin >> rectW;
cout << " Enter Length = "; cin >> rectL;
cout << endl;
//Store width and length into Box Object
box.setW(rectW); box.setL(rectL);

//Display the rectangle data.
cout << " Width of rectangle is = " << box.getW() << endl;//function return width
cout << " Length of rectangle is = " << box.getL() << endl;//function return length
cout << " Area of rectangle is = " << box.getA() << "\n\n";//return area.
return 0;
}
These are very simple codes, I already ran it with them all in the Same .cpp ( not separate into .h and all, everything contain in one program). Now is just how to separate them and run to get the same result.

Thanks

I guess the place you add your include directories should be here: Tools / Options / Projects and Solutions / VC++ Directories / Include files
https://stackoverflow.com/questions/2676417/how-do-include-paths-work-in-visual-studio
https://docs.microsoft.com/en-us/cpp/build/reference/vcpp-directories-property-page?view=vs-2019
I followed your direction, it's not the same. I read the first link, it was about VS in 2008 that has the same path as yours, mine is VS2019, this is where it's different:

There is no VC++Directories.

I am still searching on the web also. I don't know how relevant is this:

Thanks

Mark44
Mentor
I guess the place you add your include directories should be here: Tools / Options / Projects and Solutions / VC++ Directories / Include files
No, that's where the VS include files go. You shouldn't clutter up the VS system files with programs you're working on..

Organizing a project with multiple source files and one or more header files is pretty straightforward.

When you start a new project, the Header Files folder will be empty, and the Source Files Folder will be empty. If you want to add a header file (.h), right-cick Header Files, and Add --> New item will cause a dialog box to open. In it, select Header file, and enter a name for it, then click Add.

To add a source code file, right-click Source Files, and Add --> New item will cause the same dialog box to open. Select Header file, enter a name for the file, then click Add. There's another option to add a Class, but I don't use that option.

Once you have added the source code file(s) and header file(s), you can type your programs in, or paste the text in.

You showed Rectangle.h, Rectangle.cpp, and source.cpp for the project you're working on. Rectangle.h has the class declaration in it, Rectangle.cpp has the definitions of the member functions, and source.cpp has main().
Rectangle.h would go in the Header Files folder as shown in the screen shot above, and Rectangle.cpp and source.cpp would both go in the Source File folder. As long as there is only one main() function, you can build the project just as you have been doing.

I don't think it's at all relevant to what you're trying to do.

yungman and FactChecker
No, that's where the VS include files go. You shouldn't clutter up the VS system files with programs you're working on..

Organizing a project with multiple source files and one or more header files is pretty straightforward.
View attachment 270303
When you start a new project, the Header Files folder will be empty, and the Source Files Folder will be empty. If you want to add a header file (.h), right-cick Header Files, and Add --> New item will cause a dialog box to open. In it, select Header file, and enter a name for it, then click Add.

To add a source code file, right-click Source Files, and Add --> New item will cause the same dialog box to open. Select Header file, enter a name for the file, then click Add. There's another option to add a Class, but I don't use that option.

Once you have added the source code file(s) and header file(s), you can type your programs in, or paste the text in.

You showed Rectangle.h, Rectangle.cpp, and source.cpp for the project you're working on. Rectangle.h has the class declaration in it, Rectangle.cpp has the definitions of the member functions, and source.cpp has main().
Rectangle.h would go in the Header Files folder as shown in the screen shot above, and Rectangle.cpp and source.cpp would both go in the Source File folder. As long as there is only one main() function, you can build the project just as you have been doing.

I don't think it's at all relevant to what you're trying to do.
Thanks a million!!! If I can, thanks TEN Millions!!

You make my day!!! It's just THAT easy!!!! I was so worry!!! Now I can go watch some tv!!!

Thanks

No, that's where the VS include files go. You shouldn't clutter up the VS system files with programs you're working on..
I don't use VS. But usually you have some header files that you use across different programs, or some third party libraries you are using. You have to tell the compiler where they are. In g++ you use -I. There must be some way to do this with VS?

I don't use VS. But usually you have some header files that you use across different programs, or some third party libraries you are using. You have to tell the compiler where they are. In g++ you use -I. There must be some way to do this with VS?
I saw that g++ in this:
I got in the command file and ran it, it didn't work. It doesn't recognize g++.

You never use VS? I don't know any other ones, the debugger works really good. I had to re-install VS, it seems to work better now. One thing, the projects VS creates are huge.....I mean huge!!! Don't know why it's so big, takes a long time to do backup.

Last edited:
I am starting to study new material, in the book it said you can compile the implementation file first. The main program can take in the Rectangle.h and the object file of implementation file. How do you compile the implementation file Rectangle.cpp in post #6?

I tried, it flag error because it's missing int main(). All I have to do is add in

int main()
{
return 0;
}
into the Rectengle.cpp, it will compile, but that's not going to work. How do you compile just the Rectangle.cpp?

Also, I am reading the inline member functions. If I can put some of the inline function into the Specification file(Rectangle.h), why can't I put all the member functions into the Specification file and save the trouble of having a separate Implementation file?

thanks

pbuk
Gold Member
I am starting to study new material, in the book it said you can compile the implementation file first.
...
How do you compile just the Rectangle.cpp?
I am not sure if you are reading this right, in any case as you are using Visual Studio Code then it has its own ways of working for things like this. I suggest you ignore any parts of the book that address the mechanics of compilation and build tools like 'make' and move on.

Also, I am reading the inline member functions. If I can put some of the inline function into the Specification file(Rectangle.h), why can't I put all the member functions into the Specification file and save the trouble of having a separate Implementation file?
You can, but as things get more complicated it becomes easier to manage if you split them so it is a good idea to learn how to do this now.

I have never seen the term 'Specification file', we usually call this a 'header file'.

yungman
I am not sure if you are reading this right, in any case as you are using Visual Studio Code then it has its own ways of working for things like this. I suggest you ignore any parts of the book that address the mechanics of compilation and build tools like 'make' and move on.

You can, but as things get more complicated it becomes easier to manage if you split them so it is a good idea to learn how to do this now.

I have never seen the term 'Specification file', we usually call this a 'header file'.
Must be just my book that call Specification file.

I saw that g++ in this:
I got in the command file and ran it, it didn't work. It doesn't recognize g++.

You never use VS? I don't know any other ones, the debugger works really good. I had to re-install VS, it seems to work better now. One thing, the projects VS creates are huge.....I mean huge!!! Don't know why it's so big, takes a long time to do backup.
VS is a nice IDE, but I mainly use a linux operating system and I don't usually use an IDE at all, except Qt creator when I am making a Qt based GUI application.

Last edited:
Mark44
Mentor
I don't use VS. But usually you have some header files that you use across different programs, or some third party libraries you are using. You have to tell the compiler where they are. In g++ you use -I. There must be some way to do this with VS?
I've written several hundred C++ programs, most of them quite small. None of them share header files, as they are mostly intended for use in the classes I teach, so are examples that focus on a small number of C++ aspects.

If one does write programs in VS that need to share header files, the header files can be placed in a common directory. There is a project property in VS that one can set so that VS knows where to look for header files that aren't in the "usual" place. The same -I switch that you mentioned can also be used in VS if you want to compile the code from the command line.
However, as I said before, IMO it's not a good idea to clutter up the compiler's header file directory with additional header files.

FactChecker
Gold Member
IMO it's not a good idea to clutter up the compiler's header file directory with additional header files.
I used to work on some large projects where I often needed to modify the VS files to change where the header files for different versions were stored. I agree that it is a very bad idea to put your application header files in the directories of the standard libraries.

It works if you just specify like #include "C:\\.........\\Rectangle.h" in both the Rectangle.cpp and source.cpp, I even deleted the Rectangle.cpp in the main program and ADD-->Existing and do "C:\\.........\\Rectangle.cpp", then you can put both anywhere you like. It work like a champ. I just changed the source.cpp this way.
C++:
// 13.4 using Rectangle Object .h and .cpp
#include <iostream>
#include "C:\\Users\\alanr\\Desktop\\C++ exercise\\Gaddis\\inout files\\Rectangle.h" //Needed for Rectangle Class
using namespace std;

int main()
{
Rectangle box; //define an instance of Rectangle Class
double rectW;//Local variable
double rectL;

cout << " this program will calculate the area of rectangle.\n\n";
cout << " Enter width = "; cin >> rectW;
cout << " Enter Length = "; cin >> rectL;
cout << endl;
//Store width and length into Box Object
box.setW(rectW); box.setL(rectL);

//Display the rectangle data.
cout << " Width of rectangle is = " << box.getW() << endl;//function return width
cout << " Length of rectangle is = " << box.getL() << endl;//function return length
cout << " Area of rectangle is = " << box.getA() << "\n\n";//return area.
return 0;
}

FactChecker
Gold Member
That certainly works. Eventually, you will want to start putting less detailed and specific paths in your include statements. You don't want your code to depend on the exact location of everything. The compiler can search in a list of relative-path directories. That allows you to move your code without needing to change all your include statements.

I would recommend that you have a directory for headers at a high level in that exercise directory and put the relative path in the include like:
#include "..\\..\\includes\\Rectangle.h";

pbuk
Gold Member
This is not a good idea, why do you insist on doing things your own way? Doing it your way makes it impossible for anyone else to help you, do it the way everybody else does.

Just in case you are in any doubt: never put absolute paths in source code files.

This is not a good idea, why do you insist on doing things your own way? Doing it your way makes it impossible for anyone else to help you, do it the way everybody else does.

Just in case you are in any doubt: never put absolute paths in source code files.
I just keep experimenting until I learn a better way. One gain knowledge by keep experimenting.

I am 40 pages into Chapter 13, sounds like all the Classes of ADT are very much like function that is EXTERNAL to the main program, that the program call them when there's a need.

The normal functions are still inside the program, just take it outside main() so it does not have to be repeated used over and over at different places, and also make it simpler for people to understand. The Classes take it to another level that you don't even put the code in the main program and just do it by linking. All is just to make the code more modular and break up the big program so different people can work on it at the same time. All they need is to specify the interface and what parameters are being passed.

Ha ha, I found myself talk a little Russian now, all the Object, instance, header, scope resolution operator, dot operator...............I am so bad in remembering names, I used to be able to remember people's phone number and forgot their names!! It was so hard for me at the beginning here, here you guys trying to explain things to me, I just could not come right out and say you are not helping because your explanations are usually worst than my questions!! Reading online were even worst. It takes time to get into this, now that I am on chapter 13, all of a sudden, it's like the fog starts to lift a little. Knock on wood, 40 pages into Chapter 13, I really don't have serious question, only question so far is how to put all the files together and now I got it. Hopefully it remains the same for the rest of the chapter. I am studying Constructors, next is Destructors, then array of Classes. That's the end of the chapter.

I run into VS problem. You can see below

On the left top, you see a red wiggle line under #include. It said cannot open RectConstr.h.
With exception of adding the constructor, both the RectConstr.h and RectConstr.cpp are the EXACT same as Rectangle.h and Rectangle.cpp used before.

In the RectConstr.h
C++:
//Specification file for Rectangle with Constructor
#ifndef Rectangle_H
#define Rectangle_H
//Rectangle class declaration
class Rectangle
{
private:
double width;
double length;
public:
Rectangle();//Constructor
void setW(double);
void setL(double);
double getW() const;
double getL() const;
double getA() const;
};
#endif //
and RectConstr.cpp
C++:
//Rectangle implementation file with Constructor
#include <iostream>
#include <cstdlib>
using namespace std;

Rectangle::Rectangle()
{width = 0.0; length = 0.0;}
void Rectangle::setW(double w)
{
if (w >= 0)    width = w;
else { cout << "Invalid width.\n\n"; exit(EXIT_FAILURE); }
}
void Rectangle::setL(double len)
{
if (len >= 0) length = len;
else { cout << "Invalid length.\n\n"; exit(EXIT_FAILURE);}
}
double Rectangle::getW() const
{return width;}
double Rectangle::getL() const
{return length;}
double Rectangle::getA() const
You can see I only added line11 Rectangle() in the RectConstr.h and line 7 to 8 in RectConstr.cpp.
The files are all there, I can click the files on the left of the image above and actually show the codes of both the .h and .cpp codes. They are in the program.

Only major change is the name of the files, I use RectConstr instead of Rectangle to distinguish them that they have Constructor in them.

What is the problem here?

thanks

EDIT: I have been reading more, the Gaddis book starts to refer to the stuffs in the CD that came with the book. Of cause I don't have the CD. I ended up have to buy another same book that comes with the CD, the same edition. It said it comes with the CD, I hope it's true. Luckily it's cheap, it's only \$11 total. I just hate to buy another book with only 2 more chapters to go, but what else can I do?

Last edited: