Abstract Class/w functions

pure virtual functions and non-virtual function

Page 1 of 1

5 Replies - 1738 Views - Last Post: 01 March 2009 - 11:37 AM Rate Topic: -----

#1 cbc123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-January 09

Abstract Class/w functions

Post icon  Posted 28 February 2009 - 07:59 PM

I understand the basics of abstract classes and the use of a pure vitual function. My problem is I have not been able to relay the key argument so I can add it to the return ch in the class SimpleEncryption : public Encryption to specify its character transformation. Any help would greatly appreciated. Here's what I got
class Encryption
{
protected:
   ifstream inFile;
   ofstream outFile;  
   int Key;
public:
   Encryption(char *inFileName, char *outFileName);
   ~Encryption();
   // Pure virtual function.
   virtual char transform(char ch) = 0;
   /*virtual int  EncryptKey(int key) = 0;*/

   /*void Encryption::SetKey(int k){Key = k;}
   int Encryption::GetKey()
   {
	  return Key;
   }*/

   // Do the actual work.
   void encrypt();
};

void Encryption::encrypt()
{
  char ch;
  char transCh;
  //int key;
  //int EncKey;
  
  //EncKey = EncryptKey(key);
  inFile.get(ch);
  while (!inFile.fail())
	{
	  transCh = transform(ch);
	  outFile.put(transCh);
	  inFile.get(ch);
	}
}
//SimpleEncryption subclass
//============================
class SimpleEncryption : public Encryption
{
public:
   char transform(char ch)
   { 
	 return ch + 1;//return ch + key
   }
   SimpleEncryption(char *inFileName, char *outFileName)
   : Encryption(inFileName, outFileName)
   {
   }
};

Could EncryptKey be declared a pure virtual function or should it even matter?

Is This A Good Question/Topic? 0
  • +

Replies To: Abstract Class/w functions

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,232
  • Joined: 14-September 07

Re: Abstract Class/w functions

Posted 28 February 2009 - 08:11 PM

The only reason you would want to declare a base class' method as pure virtual is to ensure that derived classes override it. This is when each would have their own method. I would say yes, unless you want to have a default encryptKey function.
Was This Post Helpful? 0
  • +
  • -

#3 cbc123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-January 09

Re: Abstract Class/w functions

Posted 28 February 2009 - 09:56 PM

View PostKYA, on 28 Feb, 2009 - 07:11 PM, said:

The only reason you would want to declare a base class' method as pure virtual is to ensure that derived classes override it. This is when each would have their own method. I would say yes, unless you want to have a default encryptKey function.

I don't want a default key. I want to have the ch add key to it, but I still have not figured it out. I have tried a few variations most of which lead to " cannot instantiate absract class". Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,232
  • Joined: 14-September 07

Re: Abstract Class/w functions

Posted 28 February 2009 - 10:34 PM

That means you are trying to make an object of an abstract class. Perhaps you could post what you are doing in main()?
Was This Post Helpful? 0
  • +
  • -

#5 cbc123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-January 09

Re: Abstract Class/w functions

Posted 01 March 2009 - 07:37 AM

int main()
{ //int key = 0;
//  Encryption encryptKey;
  /*int AccessKey;

  cout << "Enter the encryption key: ";
  cin >>AccessKey;

  encryptKey.SetKey(AccessKey);
  encryptKey.encrypt();*/
  

  char inFileName[80], outFileName[80];
  cout << "Enter name of file to encrypt: ";
  cin >> inFileName;

  cout << "Enter name of file to receive the encrypted text: ";
  cin >> outFileName;
  
  SimpleEncryption MixChars(inFileName, outFileName);
  MixChars.encrypt();
  /*MixChars.AddKey();*/

  RealOutPut RealChars(inFileName, outFileName);
  RealChars.encrypt();
  

  ConvertToUpper UpperCase(inFileName, outFileName);
  UpperCase.encrypt();
 
  
  return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 cbc123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-January 09

Re: Abstract Class/w functions

Posted 01 March 2009 - 11:37 AM

Here is my revised code. ch is adding key by default, but it is not implementing the change when I enter in the key number.
#include <iostream>
#include <fstream>
#include <cctype>


using namespace std;

class Encryption
{
protected:
   ifstream inFile;
   ofstream outFile;  
   int Key;
public:
   Encryption(char *inFileName, char *outFileName);
   ~Encryption();
   // Pure virtual function.
   virtual char transform(char ch) = 0;
   void Encryption::SetKey(int k){Key = k;}
   int Encryption::GetKey(){ return Key;}
   // Do the actual work.
   void encrypt();
  
};


//constructor opens the input and output file 
//============================================
Encryption::Encryption(char *inFileName, char *outFileName)
{
   inFile.open(inFileName);
   outFile.open(outFileName,ios::out | ios::app);
   if (!inFile) 
	 {
	   cout <<  "The file " << inFileName << " cannot be opened.";
	   exit(1);
	 }
   if (!outFile)
	{
	  cout << "The file " << outFileName << " cannot be opened.";
	  exit(1);
	}
}

//desconstructor allows for file close
Encryption::~Encryption()
{
   inFile.close();
   outFile.close(); 
}
//encrypt function uses the virtual transform member function
//===================================================
void Encryption::encrypt()
{
  char ch;
  char transCh;
 
  inFile.get(ch);
  while (!inFile.fail())
	{
	  transCh = transform(ch);
	  outFile.put(transCh);
	  inFile.get(ch);
	}
}
//SimpleEncryption subclass
//============================
class SimpleEncryption : public Encryption
{
public:
   char transform(char ch)
   { 
	   return ch + Key;
   }
   SimpleEncryption(char *inFileName, char *outFileName, int key)
   : Encryption(inFileName, outFileName)
   {
   }
};

//RealOutPut subclass
//==============================
class RealOutPut : public Encryption
{
public:
   char transform(char ch)
   {
	 return ch;
   }
   RealOutPut(char *inFileName, char *outFileName)
   : Encryption(inFileName, outFileName)
   {
   }
};
//ConvertToUpper subclass
//==============================
class ConvertToUpper : public Encryption
{  
public:
   char transform(char ch)
   {
	   ch = toupper(ch);
	   return ch;
   }
   ConvertToUpper(char *inFileName, char *outFileName)
   : Encryption(inFileName, outFileName)
   {
   }

};


//=======================================
int main()
{ int key;
  

  char inFileName[80], outFileName[80];
  cout << "Enter name of file to encrypt: ";
  cin >> inFileName;

  cout << "Enter name of file to receive the encrypted text: ";
  cin >> outFileName;
  
  cout << "Enter encryption key : ";
  cin >> key;
  SimpleEncryption MixChars(inFileName, outFileName, key);
  MixChars.GetKey();
  MixChars.encrypt();
  

  RealOutPut RealChars(inFileName, outFileName);
  RealChars.encrypt();
  

  ConvertToUpper UpperCase(inFileName, outFileName);
  UpperCase.encrypt();
 
  
  return 0;
}
What do you think?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1