classes

Pages: 12
But your declaration would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Example
{
private:
     string ex;

public:
     Example()
     {
          //initialize private data member
          ex = "";
     } //end of default constructor

     void setParameter(string exString)
     {
          //assign value to private data member
          ex = exString;
     } //end of setParameter method

      type methodName(parameters)
     {
          //code goes in here
     } //end of method
};
Last edited on
ok so this is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>

using namespace std;

class Vigenere{

private:
	char shift(char c, int shamt);
	int shamt;
	string keyWord(string);

public:

// default constructor
Vigenere::Vigenere(int shiftAmount){ // Specify shift amount
	int shamt = shiftAmount;
}

Vigenere(string keyWord){
string key;
	for (int i = 0; i < key.size(); i++){
    if (key[i] >= 'A' && key[i] <= 'Z')
    	key += key[i];
    else if (key[i] >= 'a' && key[i] <= 'z')
    	key += key[i] + 'A' - 'a';
    }
}

Vigenere::encrypt(string text){ // Encrypts message

string message; 
	for (int i = 0, j = 0; i < text.length(); i++){
    char c = text[i];
    if (c >= 'a' && c <= 'z')
        c += 'A' - 'a';
    else if (c < 'A' || c > 'Z')
    message += (c + message[j] - 2 * 'A') % 26 + 'A';
        j = (j + 1) % message.length();
    }
    return message;
}

Vigenere::decrypt(string text){ // Decrypts message

string message;
    for (int i = 0, j = 0; i < text.length(); i++){
    char c = text[i];
    if (c >= 'a' && c <= 'z')
    	c += 'A' - 'a';
    else if (c < 'A' || c > 'Z')
    message += (c - message[j] + 26) % 26 + 'A';
    j = (j + 1) % message.length();
    }
    return message;
}

Vigenere::simplify(string text){ // Returns edited copy of string with only UPPERCASE letters
	string simplified = "";
	int size = text.size();
	for(int i = 0; i < size; i++){
    if (isalpha(text[i])){
    	simplified = simplified + (char) toupper(text[i]);
	}
}
	return simplified;
}

char Vigenere::shift(char c, int shamt){ // Shifts the order of the letters

	int result = (c-'A'+shamt);

	if (result >= 26) result = result - 26;
	if (result < 0) result = result + 26;
	
	return result+'A';
}

};

int main(int argc, char *argv[]){

  if (argc != 3){
    cerr << "USAGE: " << argv[0] << " -d|e shiftAmount" << endl;
    exit(1);
  }
  
  int shamt = atoi(argv[2]);
  
  bool encrypt;
  string option(argv[1]);
  string line;
  string encodedLine;	
  
  if (option == "-e"){
    encrypt = true;
  }	else {
    encrypt = false;
  }
  
  Vigenere cipher(shamt);
  
  while( getline(cin, line) ){
    if (encrypt){
      encodedLine = cipher.encrypt(cipher.simplify(line));
    } else {
      encodedLine = cipher.decrypt(line);
    }	
    cout << encodedLine << endl;
  }

	Vigenere cipher("VIGENERECIPHER");
    string encrypted = cipher.encrypt();
    string decrypted = cipher.decrypt(encrypted);
 
    cout << original << endl;

	return 0;
}
and this is where i get the messages:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
vigenere2.cpp:24:11: error: extra qualification on member 'Vigenere'
Vigenere::Vigenere(int shiftAmount){ // Specify shift amount
~~~~~~~~~~^
vigenere2.cpp:28:11: error: extra qualification on member 'keyWord'
Vigenere::keyWord(string key){
~~~~~~~~~~^
vigenere2.cpp:28:11: error: C++ requires a type specifier for all declarations
Vigenere::keyWord(string key){
          ^
vigenere2.cpp:38:11: error: extra qualification on member 'encrypt'
Vigenere::encrypt(string text){ // Encrypts message
~~~~~~~~~~^
vigenere2.cpp:38:11: error: C++ requires a type specifier for all declarations
Vigenere::encrypt(string text){ // Encrypts message
          ^
vigenere2.cpp:52:11: error: extra qualification on member 'decrypt'
Vigenere::decrypt(string text){ // Decrypts message
~~~~~~~~~~^
vigenere2.cpp:52:11: error: C++ requires a type specifier for all declarations
Vigenere::decrypt(string text){ // Decrypts message
          ^
vigenere2.cpp:66:11: error: extra qualification on member 'simplify'
When you declare the methods inside of the class, you don't need the Vigenere::. So you would something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Vigenere{

private:
	int shamt;
	string keyWord;

public:

// default constructor
Vigenere(int shiftAmount){ // Specify shift amount
	int shamt = shiftAmount;
}

Vigenere(string key){
     keyWord = key;
	for (int i = 0; i < keyWord.size(); i++){
    if (keyWord[i] >= 'A' && keyWord[i] <= 'Z')
    	keyWord += key[i];
    else if (keyWord[i] >= 'a' && keyWord[i] <= 'z')
    	keyWord += keyWord[i] + 'A' - 'a';
    }
}

string encrypt(string text){ // Encrypts message

string message; 
	for (int i = 0, j = 0; i < text.length(); i++){
    char c = text[i];
    if (c >= 'a' && c <= 'z')
        c += 'A' - 'a';
    else if (c < 'A' || c > 'Z')
    message += (c + message[j] - 2 * 'A') % 26 + 'A';
        j = (j + 1) % message.length();
    }
    return message;
}

string decrypt(string text){ // Decrypts message

string message;
    for (int i = 0, j = 0; i < text.length(); i++){
    char c = text[i];
    if (c >= 'a' && c <= 'z')
    	c += 'A' - 'a';
    else if (c < 'A' || c > 'Z')
    message += (c - message[j] + 26) % 26 + 'A';
    j = (j + 1) % message.length();
    }
    return message;
}

string simplify(string text){ // Returns edited copy of string with only UPPERCASE letters
	string simplified = "";
	int size = text.size();
	for(int i = 0; i < size; i++){
    if (isalpha(text[i])){
    	simplified = simplified + (char) toupper(text[i]);
	}
}
	return simplified;
}

char shift(char c, int shamt){ // Shifts the order of the letters

	int result = (c-'A'+shamt);

	if (result >= 26) result = result - 26;
	if (result < 0) result = result + 26;
	
	return result+'A';
}

};
ok that makes a LOT of sense now, thank you
You're welcome. I just find it much easier to not have to keep using the scope resolution operator (::). Let me know if you have any other class questions. I try to make as many classes as I can. I find abstraction interesting.
ok that definitely works, once again thank you
ok if i run into another question i'll let you know, and i'm glad to know that you like abstraction, I myself i'm starting to love programming for its beautifull complexity
Yes. Class hierarchies are fun once you get there. This site has some good tutorials on classes.
ok I have one last question
Ok
when I comment out (// int shamt && // int shamt = shiftAmount) the program compiles without errors but when i type in my message it displays:

Floating point exception: 8

I don't know what this means
try (/*int shamt && int shamt = shiftAmount*/)
ok i'll try that
i don't think that would work because it comments out more than I need to
ok so i solved the comment part, but I still get the "Floating point exception: 8" message when i run the program, I think the error is in my encrypt or decrypt functions and I cant seem to find it
ok so this are my encrypt and decrypt functions respectively:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
string encrypt(string text){ // Encrypts message

string message; 
	for (int i = 0, j = 0; i < int(text.length()); i++){
    	char c = text[i];
    if (c >= 'a' && c <= 'z')
        c += 'A' - 'a';
    else if (c < 'A' || c > 'Z')
    message += (c + message[j] - 26 * 'A') % 26 + 'A';
        j = (j + 1) % message.length();
    }
    return message;
}

string decrypt(string text){ // Decrypts message

string message;
    char c;
    for (int i = 0, j = 0; i < text.length(); i++){
    	c = text[i];
    if (c >= 'a' && c <= 'z')
    	c += 'A' - 'a';
    else if (c < 'A' || c > 'Z')
    message += (c - message[j] + 26) % 26 + 'A';
    j = (j + 1) % message.length();
    }
    return message;
}

Ok, so it doesn't compile at all?
Topic archived. No new replies allowed.
Pages: 12