{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture13slides

lecture13slides - Topics for Wednesday November 21st 1...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Topics for Wednesday, November 21 st 1. Review of classes 2. Review of constructors 3. Destructors 4. More constructor and destructor detail 5. Compositions: Classes in classes! 6. Overloading operators with classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Class review: The Enigma machine ENIGMA.H class CEnigma { public: CEnigma(int key); CEnigma(void); string Encrypt(string msg); string Decrypt(string code); private: int m_key; }; ENIGMA.CPP CEnigma::CEnigma(int key) // c’tor { m_key = key; } CEnigma::CEnigma(void) // c’tor { m_key = 2; } This is a good example of why we want our logic and member variables to be hidden. We want to hide our algorithm and our key value!
Background image of page 2
CEnigma Continued string CEnigma::Encrypt(string msg) { for (int i=0;i<msg.length();i++) { msg[i] += i; } return(msg); } string CEnigma::Decrypt(string msg) { for (int i=0;i<msg.length();i++) { msg[i] -= i; } return(msg); } MAIN.CPP void main(void) { CEnigma a(1), b; string c1 = a.Encrypt("Hello"); cout << c1 << endl; string c2 = b.Encrypt("Hello"); cout << c2 << endl; string m1 = a.Decrypt(c1); cout << m1 << endl; cout << a.m_key; // private: error! }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Constructors Remember: Constructors are called every time we create a new instance of a class. main() { CEnigma a(10); // constructor called CEnigma b; // constructor called ... } The constructor initializes a newly created object. Once the constructor is done, we can use the instance to do work for us: string e = a.Encrypt(“Encrypt me”); ... The constructor is just like an init function...
Background image of page 4
CEnigma with an Init function class CEnigma { public: void Init(int key); void Init(void); string Encrypt(string msg); string Decrypt(string code); private: int m_key; }; void CEnigma::Init(int key) { m_key = key; } void CEnigma::Init(void) { m_key = 2; } ... (rest of class implementation)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}