Original Post
I Trying to write the C++ code to do the following:
Create a base class called Publication wich is and abstract class
Thus i have written:
[Edit: Too fucking much code.]
Bah what a headache i have any advice would be appreciated.
"... thats the rub...
[edited by - Oluseyi on April 21, 2002 3:32:05 AM]
*************************************************
//Publication header
#ifndef PUB_H
#define PUB_H
class Publication
{
public:
virtual void getdata()=0;
virtual void putdata()=0;
private:
String title;
double price;
double sales[3];
}
#endif
*****************************************************
Then i want to make two base classes CDRom and book so far i have:
**************************************************
// book class
#ifndef BOOK_H
#define BOOK_H
class book:public Publication
{
public:
void getdata();
void putdata();
private:
double pages;
String author;
String ISDN;
}
#endif
***********************************************************
And:
**************************************************************
//CDRom class
#ifndef CD_H
#define CD_H
class CDRom:public Publication
{
public:
void getdata();
void putdata();
private:
double minutes;
String artist;
String category;
}
#endif
************************************************************
String is a class that has overloaded opperators for working with Strings but the compiler is not recognizing "String" as a Type in the new classes i am creating does anyone know why? I have tried doing #include "header5.h" in the class definitions but it does not seam to work.
Let me show you the functions and if you can offer any advice let me know.
**********************************************************
//book functions
#include <iostream.h>
#include "book.h"
#include "header.h"
book::getdata()
{
cout <<"Enter the title of the book: \n";
cin >> title;
cout<<"Enter the price of the book: \n";
cin >> price;
cout <<"Enter the first months sales: \n";
cin >> sales[0];
cout <<"Enter the second months sales: \n";
cin >> sales[1];
cout <<"Enter the third months sales: \n";
cin >> sales[2];
cout <<" Enter the number of pages: \n";
cin >> pages;
cout << "Enter the authors name: \n";
cin >> author;
cout << "Enter the ISDN: \n";
cin >> ISDN;
}
book::putdata()
{
cout<< "hello";
}
*********************************************************
#include <iostream.h>
#include "CD.h"
#include "header.h"
CDRom::getdata()
{
cout <<"Enter the title of the CD: \n";
cin >> title;
cout<<"Enter the price of the CD: \n";
cin >> price;
cout <<"Enter the first months sales: \n";
cin >> sales[0];
cout <<"Enter the second months sales: \n";
cin >> sales[1];
cout <<"Enter the third months sales: \n";
cin >> sales[2];
cout <<" Enter the number of minutes: \n";
cin >> minutes;
cout << "Enter the artists name: \n";
cin >> artist;
cout << "Enter the category of music: \n";
cin >> category;
}
book::putdata()
{
cout<< "hello";
}
************************************************************
Here is the String class:
**********************************************************88
/**************************************************
Project5
header5.h
Matthew Bauman
04/08/02String class
Description:
1) This is the definition of the String Class
2)no data structures
3)none
**************************************************/
#ifndef HEADER5_h
#define HEADER5_h
class String {
friend ostream &operator<<( ostream &, const String &);
friend istream &operator>>( istream &, String &);
public:
String( const char * ="" );
String(const String &);
~String();
const String &operator=( const String );
String operator+( const String &);
bool operator==(const String &) const;
bool operator<(const String &);
bool operator>(const String &);
int stringlength();
void get_string();
private:
int length;
char *sptr;
void setString(const char *);
};
***********************************************************888
/*************************************************
Project5
function5.cpp
Matthew Bauman
04/08/02
Description : This file contains the functions for
the String class.
1)This code allows the overloading of opperators
to manipulate the String type like the more common
typse.
2)if else
3)References are used to manipulate the data
in the String classes in order for them to
be overloaded.
*************************************************/
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <assert.h>
#include "header5.h"
/*****************************************************
Procedure: initialize
Purpose: initialize the class private member values.
Parameters: A pointer to a character array.
Return Value: none
******************************************************/
String::String( const char *s ):length(strlen(s)){
setString(s);
}
/*************************************************
Procedure: initialize
Purpose: copy String to another String.
Parameters: a reference to a String type
Return Value: none
*************************************************/
String::String(const String ©): length(copy.length)
{
setString(copy.sptr);
}
/**************************************************
Procedure: initialize
Purpose: Set a String classes member values to
be equal to another String classes member values.
Parameters: a String type.
Return Value: a constant reference to a String.
**************************************************/
const String &String::operator=( const String right ){
if ( &right != this )
{
delete [] sptr;
length=right.length;
setString( right.sptr );
}
else
cout << "Attempted to assign a string to itself";
return *this; //enables cascading assignment
}
/******************************************************
Procedure: Destructor
Purpose: release any dynamic memory being used by the program.
Pameters: none
return value: none
********************************************************/
String::~String(){
delete [] sptr;
}
/******************************************************
Prcedure:count characters
Purpose: return the number of characters in a string.
Parameters: none
return value: none
******************************************************/
int String::stringlength(){
length=strlen(sptr);
return length;
}
/******************************************************
Procedure:set a strings value.
Purpose:Assign a string to a String type's
member variable.
Parameters: a constant pointer to a char string.
Return Value: none
******************************************************/
void String::setString(const char *string2 ){
sptr=new char [ length +1 ];
assert( sptr != 0 );
strcpy( sptr, string2);
}
/*****************************************************
Procedure: Set Sptr
Purpose: Set Sptr
Parameters: none
Return value: none
******************************************************/
void String::get_string(){
cout << " Enter the String ";
cin >> sptr;
cout << " You have to the String to be "<<sptr<< endl;
}
/***************************************************
Procedure: cout
Purpose: overload the << operator to work with the
String class
Parameters: Reference to an ostream, Reference to
a String type.
Return value: A reference to and Ostream.
****************************************************/
ostream &operator<<( ostream &output, const String &s ){
output << s.sptr;
return output;
}
/**************************************************
Procedure: cin
Purpose: overload the >> operator to work with the
String class
Parameters: Reference to an istream, Reference to
a String type.
Return value: A reference to and istream.
***************************************************/
istream &operator>>( istream &input, String &s ){
char temp[100];
input >> setw(100) >> temp;
s = temp; //use string class assignment operator
return input;
}
/************************************************
Procedure: add Strings together
Purpose: allow the + operator to add Strings.
Parameters: a String
Return value: String
*************************************************/
String String::operator+( const String &right ){
String Temp;
Temp.sptr = new char[length + right.length+1];
strcpy(Temp.sptr, sptr);
strcat(Temp.sptr, right.sptr);
return Temp;
}
/**************************************************
Procedure:check for equality of the two strings
Purpose: return true if the values are equal and
turn false if they are not equal.
Parameters: Reference to a string
return value: bool
***************************************************/
bool String::operator==(const String &right)const
{
return strcmp( sptr, right.sptr ) == 0;
}
/**************************************************
Procedure:check to see if the calling String is less
that the perameter
Purpose: return true if the value is less than and
return false if it is greater.
Parameters: Reference to a string
return value: bool
***************************************************/
bool String::operator<(const String &right){
return strcmp( sptr, right.sptr ) < 0;
}
/**************************************************
Procedure:check to see if the calling String is greater
that the perameter
Purpose: return true if the value is greater than and
return false if it is less.
Parameters: Reference to a string
return value: bool
**************************************************/
bool String::operator>(const String &right){
return strcmp( sptr, right.sptr ) > 0;
}
***************************************************
Heres what the compiler said:
--------------------Configuration: HW6 - Win32 Debug--------------------
Compiling...
book.cpp
c:\program files\microsoft visual studio\myprojects\hw6\book.h(8) : error C2504: 'Publication' : base class undefined
c:\program files\microsoft visual studio\myprojects\hw6\book.h(17) : error C2146: syntax error : missing ';' before identifier 'author'
c:\program files\microsoft visual studio\myprojects\hw6\book.h(17) : error C2501: 'String' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hw6\book.h(17) : error C2501: 'author' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hw6\book.h(18) : error C2146: syntax error : missing ';' before identifier 'ISDN'
c:\program files\microsoft visual studio\myprojects\hw6\book.h(18) : error C2501: 'String' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\hw6\book.h(18) : error C2501: 'ISDN' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(8) : error C2556: 'class book __thiscall book::getdata(void)' : overloaded function differs only by return type from 'void __thiscall book::getdata(void)'
c:\program files\microsoft visual studio\myprojects\hw6\book.h(11) : see declaration of 'getdata'
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(8) : error C2371: 'getdata' : redefinition; different basic types
c:\program files\microsoft visual studio\myprojects\hw6\book.h(11) : see declaration of 'getdata'
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(10) : error C2065: 'title' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(13) : error C2065: 'price' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(16) : error C2065: 'sales' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(16) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(16) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(19) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(19) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(22) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(22) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(28) : error C2065: 'author' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(31) : error C2065: 'ISDN' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(35) : error C2556: 'int __thiscall book::putdata(void)' : overloaded function differs only by return type from 'void __thiscall book::putdata(void)'
c:\program files\microsoft visual studio\myprojects\hw6\book.h(12) : see declaration of 'putdata'
C:\Program Files\Microsoft Visual Studio\MyProjects\Hw6\book.cpp(35) : error C2371: 'putdata' : redefinition; different basic types
c:\program files\microsoft visual studio\myprojects\hw6\book.h(12) : see declaration of 'putdata'
Error executing cl.exe.
book.obj - 22 error(s), 0 warning(s)
#endif
**************************************************