Need Help with Headers and .cpp files

Hey everyone, I have a practice assignment that I don't understand. If some one can show how .cpp and header files are related or how to format a header file, it would greatly be appreciated.

Here is the actual question, if some one could walk me through it, it would help.

Question # 1:
In a file named CBook.h create the class definition for a book.

1. The characteristics of the class (i.e. properties) are a book title, book publisher, an array of up to 4 authors, book publication year, price, quantity in stock, and number of authors. Use appropriate data types. This file should also contain the prototypes of the methods described as part of the CBook.cpp implementation file. Member variable names should be prefaced with a lower case "m". If the .h file is included more than once, a problem should not occur.

In a file named CBook.cpp write the class implementation.

2. Write a constructor that takes no arguments. Assign *unassigned* to string fields and zeros for numeric fields. If number of authors is set to zero then the author names themselves do not need to be set to "unassigned".

3. Write set… methods for title, publisher, price¸ and quantity. Do not allow assigning negative values for price or quantity.

4. Write get… methods for the same fields as in item 3.

5. Write a setBookInfo method which accepts all the values a book object needs and assigns them to the object. You can assume that all the values are valid.

6. Write an isAuthor method which accepts an author name and returns true if that is one of the authors of the book. Otherwise, false is returned. (bool values).

7. Write an isInStock method which returns true if there is at least one copy of the book in stock, otherwise returns false.

8. Write a makeSale method which decreases the quantity in stock by 1. There is no return value.

9. Write a printInfo method which displays the information about a specific book object. The display should look something like:
****************************
Title: Office 2013 Introduction
Publisher: Thomson
Year of Publication: 2013
Number of Authors: 3
Authors:
Gary B. Shelly
Thomas J. Cashman
Misty E. Vermaat
Price: 137.50
Quantity in Stock: 39
*****************************


In a file named book.cpp write the driver program.

10. Create an author array that contains one author to be used to pass to the setBookInfo method. Declare a single book object using values you make up. Assign the object values using the setBookInfo method. Display the contents of the object.

11. Declare an array in the driver program of 15 book objects called bookArray. Declare an integer which keeps track of how many elements are in the array. Write a function called populateArray which accepts the book array and a reference argument of the count of how many elements. The function will open the supplied file (BookData.txt) and assign the book information to book objects within the array.

12. The main program should loop around displaying all the book information read in.

13. Ask the user to enter the name of an author. Write a loop that will look to see which books have that author and for each such book displays the book title.

14. You are given a sample output file showing output from item 10, item 12, and item 13 listed above.
There are several different file extensions which are supported by some IDEs, typically those are ".cpp", ".h", ".hpp", ".c" etc.
You only need to be concerned with .cpp and .h at the moment.

Typically, you use .h (also known as header files) to declare classes, functions, and sometimes variables. Notice, I said declare, not define. If I want to write a Radio class - in a header file - I would say that the Radio class (and all of its members) is/are something that can exist.

Let's also assume that I want my Radio class to have a member function named "toggle()", which turns the radio on if it's off, and off if it's on. It would look something like this:

radio.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef _RADIO_H_/*ignore this line for now*/
#define _RADIO_H_/*ignore this line for now*/

class Radio {
public :
	bool on;
	Radio();		/*both the Radio default constructor and the toggle() function are declared, but have no body.*/
	void toggle();	/*in order for them to do things, I need to give them a body, which is done in a separate .cpp file*/

};

#endif/*ignore this line for now*/ 


.cpp files, on the other hand, you would use to define class methods, other functions and variables. In other words, now you're giving the previously defined things (that exist, but have no value or purpose) a value or a purpose.

I now want to give a body to both my Radio default constructor, and the Radio.toggle() function. Usually, the implementations (definitions and body functions) of a class go into a separate .cpp file by the same name.

radio.cpp
1
2
3
4
5
6
7
8
9
#include "radio.h"

Radio::Radio() {/*Default constructor now has a function body, in which I initialize the Radio.on member to false.*/
	on = false;
}

void Radio::toggle() {/*toggle() function now has a body*/
	on = !on;/*on is equal to not-on. If on is true, on will be false and vice versa*/
}


There are a few cases in which not every .h file will be accompanied by a .cpp file (this has to do with templates, so don't worry about that yet).

There may also very well be cases in which not every .cpp file is related to a .h file, such as in the case of possibly a main or "driver program".

When all your class members are defined, you can #include the .h in any other file that may need to know about your class and it's methods. This relationship between .h and .cpp files works as long as the linker can find an implementation of a class and its methods.

Another very important thing to know about header files, is that they need a so called "header guard"/"include guard". The header guard is important, because if you don't have a header guard, and happen to include the same header twice (or in a file which you include later on somewhere else), you'll get compilation errors pertaining to multiple inclusions (the same class/function/whatever already has been declared).

Header guards make sure that headers are only included once, and only for the first time they are included. There are several ways of doing this. One way of doing it is by using the non-standard #pragma once pre-processor directive. Here is an example of it's application:

1
2
3
4
5
6
7
8
9
#pragma once

class Radio {
public :
	bool on;
	Radio();
	void toggle();

};


Another way of doing it would be to use a "macro guard" - to #define a reserved macro keyword. This is the method that I prefer:

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef _RADIO_H_/*if _RADIO_H_ has not been defined*/
#define _RADIO_H_/*then define it*/
/*otherwise, if it has already been defined, all the code from here to the #endif does not get compiled, and as a result, does not get declared more than once*/

class Radio {
public :
	bool on;
	Radio();
	void toggle();

};

#endif/*necessary for any #if. The #endif is at the end of the file, because if a definition already exists for _RADIO_H_, I don't want to include this class declaration again.*/ 


Just for the sake of completeness, this is how I would use my radio class in a "driver program":

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "radio.h"

int main(int argc, char* argv[]) {
	Radio radio;
	std::cout << "radio.on: " << radio.on << std::endl;
	radio.toggle();
	std::cout << "radio.on: " << radio.on << std::endl;

	std::cin.get();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.