Hello,

I have a question about classes on C++.

So far I have 3 classes on my project. BibEntry ( abstract class), KeyFinder (class to find the \cite in file and store to private KEY at abstract) and TypeFinder ( class to find which entry is it article, journal so on )

overloaded ifstream in KeyFinder will find the key in .tex file and use the method setKey from abstract class to store it
overloaded ifstream in Type Finder will find the type from the key obtained from abstract class.

I had this snippet on main function

BibEntry *entry = 0;
  KeyFinder key;
  TypeFinder type ;
 
  infile1 >> key;
  infile2 >> type;
 
  entry = &key;
  cout<<entry->getKey()<<endl;

  entry = &type;
  cout<<entry->getKey()<<endl;

MY QUESTION IS : How come when I call key.getKey the searched key appears on display but type.getKey won't appear on display. Also, why is it that during the overloaded stream for TypeFinder the command type.getKey appears empty?

Both my classes inherit from abstract class so both of them should have access to private data KEY in abstract class. I'm very confused. I tried casting it to (TypeFinder) key but compiler didn't allow.

What can I do so that both TypeFinder and KeyFinder can access the same Key that the stream over loader had set ?

I attached all my files for your convenience. please remove the .doc extension for the input files.

Edited 5 Years Ago by gus7984: Please

Attachments
// Abstract base class 

#include <iostream>
#include "BibEntry.h"
using namespace std;

//constructor
BibEntry::BibEntry()
{
  //empty constructor
} //end of contructor

//Sets Year value
void BibEntry::setYear( const string &yr )
{
  year = yr;
} // end of setYear

//Gets year value
string BibEntry::getYear() const
{
  return year;
} //end of getYear

//Sets Key value
void BibEntry::setKey( const string &ky )
{
  key = ky;
} //end of setKey

//Gets Key value
string BibEntry::getKey() const
{
  return key;
} //end of getKey

//Sets note value
void BibEntry::setNote( const string &nt )
{
  note = nt;
} //end of setNote

//Gets note value
string BibEntry::getNote() const
{
  return note;
} //end of getNote
// BibEntry.h
// Bibtex entry abstract base class
#ifndef BIBENTRY_H
#define BIBENTRY_H

#include <string>
using namespace std;

class BibEntry
{
public:
  BibEntry();

  void setYear( const string & );
  string getYear() const;

  void setKey( const string & );
  string getKey() const;

  void setNote( const string & );
  string getNote() const;

  virtual void printBibEntry() const = 0;

private:
  string year;
  string key;
  string note;

};

#endif //End BibEntry.h
// KeyFinder.cpp
// Overloaded stream operators and set key functions

#include "KeyFinder.h"
using namespace std;

ifstream &operator>>( ifstream &input, KeyFinder &key )
{
  string buffer, found_key;
  size_t location;

  while( getline( input, buffer ) )
  {
    if( buffer.find( "\cite{" ) != string::npos ) {
      location = buffer.find( "\cite{" );
      location += 5;

      for( ; buffer.at(location) != '}' ; location ++ ){
        found_key += buffer.at(location);
      }
      
        key.setKey(found_key);

    }
  }
  
  return input;
}

void KeyFinder::printBibEntry() const
{

}
// KeyFinder.h
// This class is used to find the citation done on the text and fills in 'key' private data in abstract class.

#ifndef KEYFINDER_H
#define KEYFINDER_H

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

#include "BibEntry.h"  //BibEntry class definition

class KeyFinder : public BibEntry
{
  friend ifstream &operator>>( ifstream &, KeyFinder &);

public:
  
  virtual void printBibEntry() const;
}; 

#endif
\documentclass[11pt]{article}

\usepackage{graphicx}
\usepackage{subfig}


%%% For the letter format: Gives 1in margins all sides
\setlength {\topmargin}{0in}
\setlength {\headheight}{0in}
\setlength {\headsep}{0cm}
\setlength {\textheight}{9in}
\setlength {\oddsidemargin}{0in}
\setlength {\evensidemargin}{0in}
\setlength {\textwidth}{6.5in}
\setlength {\parskip}{0cm}
\setlength {\parindent}{1.5em}


\title{Here is the title\thanks{Here are the thanks.}}

\author{
Stefano Basagni \\
E-mail: {\tt basagni@ece.neu.edu} 
}
\date{}

\begin{document}

\maketitle
\thispagestyle{empty}

\begin{abstract}
Here is where the paper abstract goes.
\end{abstract}

\section{Introduction}
	
You can take some time here to introduce to the problem and to your own contribution with this paper.

Here is a way of inserting a pic, which is alone.

\begin{figure}[!ht]
\centering
\includegraphics[width=3in]{figSolo.eps}
\caption{This is a figure by itself.}
\label{fig:solo}
\end{figure}

You can refer to the pic like this: Figure~\ref{fig:solo}.


\subsection{This is a Sub-section}

Here is an example for inputting figures with \texttt{subfig}, a package for inserting sub-figures within one figure.

\begin{figure}% 
\centering 
\subfloat[Caption of the first figure.]{\label{fig:LabelFirstFig}\includegraphics[width=2.5in]{figA}} 
\qquad 
\subfloat[Caption of the second figure.]{\label{fig:LabelSecondFig}\includegraphics[width=2.5in]{figB}}%
\caption{This is the common caption.} 
\label{fig:2figs}
\end{figure}

This kind of figure can be referred to as a whole (as in Figure~\ref{fig:2figs}) or by referencing to each single sub-figure: Figure~\ref{fig:LabelFirstFig} and Figure~\ref{fig:LabelSecondFig}.

\section{This is Another Section}

This is a footnote.\footnote{\ This is the body of the footnote. There is even a macro for ellipsis: \ldots}
%
Let's cite a paper: This a paper by Chlamtac et al.~\cite{ChlamtacF99}.

%%% BIBLIOGRAPHY is best done through bibtex
\bibliography{bigBib}
\bibliographystyle{acm}


% That's all, folks!
\end{document}
#include <iostream>
#include <string>
#include <fstream>
#include "BibEntry.h"
#include "KeyFinder.h"
#include "TypeFinder.h"
using namespace std;

int
main ()//int argc, char *argv[]
{
  ifstream infile1 ("laTeXtemplate.tex" , ios::in);
  ifstream infile2 ("bigBib.bib" , ios::in);
  
  if ( !infile1 )
  {
    cerr<< "File1 could not be loaded" << endl;
    exit(1);
  }

  if ( !infile2 )
  {
    cerr<< "File2 could not be loaded" << endl;
    exit(1);
  }
  
  int a;

  BibEntry *entry = 0;
  KeyFinder key;
  TypeFinder type;
  
  entry = &key;
  infile1 >> key;

  cout<<entry->getKey()<<endl;
  
  entry = &type;
  //infile2 >> type;

  cout<<entry->getKey()<<endl;

  /*BibEntry *entry1 = new KeyFinder;
 
  ;
  cout<<entry1->getKey()<<endl;
  infile2 >> entry1;
   
  entry1 = new TypeFinder;
  cout<<entry1->getType()<<endl;*/


  cin>>a;
  return 0;
}
//TypeFinder.cpp
// Overloaded stream instertion to find entry type

#include "TypeFinder.h"
using namespace std;

ifstream &operator>>( ifstream &input, TypeFinder &type )
{
  string buffer, found_key;
  size_t location;
  
  while( getline( input, buffer ) ){
  
    if( buffer.find( type.getKey() ) != string::npos ) {
      location = buffer.find( type.getKey() );

      for( location = 0 ; buffer.at(location) != '{' ; location ++ ){
        found_key += buffer.at(location);
      }
      
      type.setType( found_key );

    }
  }
  
  return input;
}

void TypeFinder::setType( const string &type1 )
{
  type = type1;
}

string TypeFinder::getType() const
{
  return type;
}

void TypeFinder::printBibEntry() const
{

}
// TypeFinder.h
// This class is used to find Entry type based on the citation obtain by FindKey.

#ifndef TYPEFINDER_H
#define TYPEFINDER_H

#include <fstream>
#include <string>
#include "BibEntry.h"
//#include "KeyFinder.h"


class TypeFinder : public BibEntry
{
  friend ifstream &operator>>( ifstream &, TypeFinder & );

public:
  void setType( const string & );
  string getType() const;
  virtual void printBibEntry() const;

private:
  string type;
};

#endif

Q1:
"key.getKey the searched key appears on display but type.getKey won't appear on display"??
A1:
easy the first file contains \cite so the keyfinder found it and store it in key
and the second file found the type but store it in type so the key is empty

Q2: i didnot understand what you want to do but if you are asking about to subclasses to share variable from parent use static modifire

I may be wrong, but I believe "protected" members of a base class are inherited by subclasses, while "private" members are indeed private to the base class, and are -not- inherited (or at least are not accessible) in subclasses.

This article has been dead for over six months. Start a new discussion instead.