Hi there.

I am currently trying to get my head around using classes across two files. I currently have a super class and a sub class stored in a file and the main function in another. When I try and pass arguments to the class they work fine if the values are int's. However, what I want to pass are book names. Here is the code.

//publication.cpp

#include <iostream>
#include <string>
using namespace std;

//the publication super class
class publication
{
private:
	char title;	//the title of the book
	char genre;	//the genre of the book
	double date;	//the date of release
public:
	//construct publication
	publication(char t, char g, double d)
	{
		title = t;
		genre = g;
		date = d;
	}

	//accessor functions
	char get_title() { return title; }
	char get_genre() { return genre; }
	double get_date() { return date; }

};

//the book sub class
class book : public publication
{
private:
		int pages; //the number of pages
public:
	//construct book
	book (char t, char g, 
		double d, int p) : publication (t ,g, d)
	{
		pages = p;
	}

	//accessor funtions
	int get_pages() { return pages; }
};

and the other file:

//main.cpp

#include <iostream>
#include <string>
#include "publication.cpp"

using namespace std;

int main()
{
	//write a book!
	book dune(dune, scifi, 1965, 1021);

	return 0;
}

I tried using type string instead of char but that didn't work either. I know I won't get any printed content, but I am working through it in baby steps.

Compiler errors are:

error C2065: 'dune' : undeclared identifier
error C2065: 'scifi' : undeclared identifier

Please can I have some help?

Thank you.

Recommended Answers

All 3 Replies

The name of a book, or genre or title is a string. So you should either use STRING class or use C char strings. (Char*)

//publication.cpp

#include <iostream>
#include <string>
using namespace std;

//the publication super class
class publication
{
private:
	char* title;	//the title of the book
	char* genre;	//the genre of the book
	double date;	//the date of release
public:
	//construct publication
	publication(char* t, char* g, double d)
	{
		title = t;
		genre = g;
		date = d;
	}

	//accessor functions
	char* get_title() { return title; }
	char* get_genre() { return genre; }
	double get_date() { return date; }

};

//the book sub class
class book : public publication
{
private:
		int pages; //the number of pages
public:
	//construct book
	book (char* t, char* g, 
		double d, int p) : publication (t ,g, d)
	{
		pages = p;
	}

	//accessor funtions
	int get_pages() { return pages; }
};

And

//main.cpp

#include <iostream>
#include <string>
#include "publication.cpp"

using namespace std;

int main()
{
	//write a book!
	char* title = "Dune";
	char* genre = "Scifi";
	book dune(title, genre, 1965, 1021);
	cout << dune.get_pages() << endl; //Get pages
	cout << dune.get_genre() << endl; //Get Genre

	return 0;
}

or do like this

#
int main()
#
{
#
//write a book!
#
book dune("dune", "scifi", 1965, 1021);
.
.
.
.

That worked a treat.

Thank you very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.