Interface versus Implementation Interface First lines or signatures

  • Slides: 21
Download presentation
Interface versus Implementation • Interface: First lines or signatures and pre- and postcondition comments

Interface versus Implementation • Interface: First lines or signatures and pre- and postcondition comments for public methods of a class. • Implementation: Private attributes and methods and C++ statements inside the bodies of all methods of a class.

UML Diagram for Pokemon - my. Name: string - my. Power: int + +

UML Diagram for Pokemon - my. Name: string - my. Power: int + + + Pokemon() set. Name(string g): void set. Power(int p): void get. Name(): string get. Power(): int to. String(): string Class Name Data Methods (actions) - private + public

#pragma once #include <string> class Pokemon { private: string my. Name; float my. Power;

#pragma once #include <string> class Pokemon { private: string my. Name; float my. Power; public: Pokemon(void); ~Pokemon(void); void set. My. Power(float); void set. My. Name(string); float get. My. Power(void); string get. My. Name(void); string to. String(void); };

#include "Pokemon. h" Pokemon: : Pokemon(void) { my. Power = 0. 0; my. Name

#include "Pokemon. h" Pokemon: : Pokemon(void) { my. Power = 0. 0; my. Name = "None"; } Pokemon: : ~Pokemon(void) { } void Pokemon: : set. My. Power(float p) { my. Power = p; } void Pokemon: : set. My. Name(string n) { my. Name = n} float Pokemon: : get. My. Power(void) { return my. Power; } string Pokemon: : get. My. Name(void) { return my. Name; }

Class Type Variables are References • This declaration gives you a variable that refers

Class Type Variables are References • This declaration gives you a variable that refers to a Pokemon object. Pokemon pokey; pokey is a variable that contains a reference to a Pokemon object. pokey. set. Name(“Pikachu”);

Creating a Pokemon Object • Create a new Pokemon object and assign the reference

Creating a Pokemon Object • Create a new Pokemon object and assign the reference variable to link to it. Pokemon pokey; pokey my. Name: None my. Power: 0 • What do the following two statements do? pokey. set. Name(“Pikachu”); pokey. set. Power(150);

Input. Pokemon Method int main() { Pokemon pokey; input. Pokemon(pokey); return 0; } pokey

Input. Pokemon Method int main() { Pokemon pokey; input. Pokemon(pokey); return 0; } pokey my. Name: None my. Power: 0 void input. Pokmen(Pokemon &p) { p. set. Name(“Pikachu”); p. set. Power(150); }

p & pokey refer to same Pokemon int main() { Pokemon pokey; input. Pokemon(pokey);

p & pokey refer to same Pokemon int main() { Pokemon pokey; input. Pokemon(pokey); } my. Name: None pokey my. Power: 0 void input. Pokmen(Pokemon &p ) { p. set. Name(“Pikachu”); p. set. Power(150); }

Class Objects as Parameters • Pokemon is a class type variable. • input. Pokemon

Class Objects as Parameters • Pokemon is a class type variable. • input. Pokemon receives P, which is a reference to a Pokemon object. • When main calls input. Pokemon method with pokey that means p gets a link pointing (or a reference referring) to main’s pokey

Method Changes main’s pokey int main() { Pokemon pokey; input. Pokemon(pokey); cout << pokey.

Method Changes main’s pokey int main() { Pokemon pokey; input. Pokemon(pokey); cout << pokey. get. Power(); return 0; } pokey my. Name: Pikachu my. Power: 150 void input. Pokemen(Pokemon &p ) { p. set. Name(“Pikachu”); p. set. Power(150); }

Input of Pokemon Data int main() { string n int p Pokemon pokey; cout

Input of Pokemon Data int main() { string n int p Pokemon pokey; cout << (“Enter name: “); cin >> n cout <<(“Enter power: “); cin >> p pokey. set. Name(n); pokey. set. Power(p); return 0; } Can we avoid repeating the bold lines for every Pokemon? ;

Complete input. Pokemon // Pre-conditions: Receives a // reference or link to a Pokemon

Complete input. Pokemon // Pre-conditions: Receives a // reference or link to a Pokemon // object named P. // Post-conditions: Prompts user to // enter name & power. Sets name // and power of the given Pokemon. void input. Pokemon( Pokemon &p)

Input. Pokemon Method • • • #include "Pokemon. h" #include <iostream> using namespace std;

Input. Pokemon Method • • • #include "Pokemon. h" #include <iostream> using namespace std; • • void input. Pokemon(Pokemon&); int main() { Pokemon pokey; input. Pokemon(pokey); cout << pokey. to. String(); return 0; } • • • void input. Pokemon(Pokemon &p) { string n ; float pow; cout << "Enter name: "; cin >> n; cout << "Enter power: "; cin >> pow; p. set. My. Name(n); p. set. My. Power(pow); }

Class Types are Pass by Value int input. Pokmen(Pokemon &p) { String n, pow;

Class Types are Pass by Value int input. Pokmen(Pokemon &p) { String n, pow; cout << “Enter name: “; cin >> n; cout << “Enter power: “); cin >> pow; p. set. Name(n); Changes made by “setters” p. set. Power(pow); change actual Pokemon object given in main }

Initializing a Reference Variable • Must create an object and assign the reference variable

Initializing a Reference Variable • Must create an object and assign the reference variable to access an object Student sue (“Sue”, 16, 4. 0); sue “Sue” 16 4. 0 “Freshman” Now that the reference variable is attached to an object. . . We can use the reference to access methods of the object sue. get. Name()

Example • Create two different Student objects and assign the references Student mike (“Mu”,

Example • Create two different Student objects and assign the references Student mike (“Mu”, “Mike”, 32, 3. 7); Student john (“Jay”, “John”, 38, 3. 9); mike “Mike” 32 2. 7 “Sophomore” john “John” 38 3. 9 “Sophomore”

Re-Assign References • Can re-assign a reference to access another object. Student mike(“Mu”, “Mike”,

Re-Assign References • Can re-assign a reference to access another object. Student mike(“Mu”, “Mike”, 32, 3. 7); Student john(“Jay”, “John”, 38, 3. 9); mike = john; “Mike” 32 2. 7 “Sophomore” mike john “John” 38 3. 9 “Sophomore”

Lost Objects • An object that is not connected to a reference variable is

Lost Objects • An object that is not connected to a reference variable is lost forever! Student mike(“Mike”, 32, 3. 7); Student john(“John”, 38, 3. 9); mike = john; “Mike” 32 2. 7 “Sophomore” With no reference to connect it, the object is no longer useful. Therefore it is known as garbage. john mike “John” 38 3. 9 “Sophomore”

Exercise • Student mike(“Mike”, 32, 3. 7); • Write a declare Student copy of

Exercise • Student mike(“Mike”, 32, 3. 7); • Write a declare Student copy of statement that will and create another object that is an exact the object mike.

Multiple References to One Object Student sue (“Doe”, “Sue”, 16, 4. 0); Student susan

Multiple References to One Object Student sue (“Doe”, “Sue”, 16, 4. 0); Student susan = sue; susan. add. Credits(16); int credits = sue. get. Credits(); // Gets 32 BEFORE AFTER “Sue” 16 4. 0 “Freshman” sue susan “Sue” 32 4. 0 “Sophomore” sue susan

Exercise Student sue (“Doe”, “Sue”, 16, 4. 0); Student joe (“Blow”, “Joe”, 32, 3.

Exercise Student sue (“Doe”, “Sue”, 16, 4. 0); Student joe (“Blow”, “Joe”, 32, 3. 0); Student X; X. add. Credits(16); X = sue; X. add. Credits(16); X. to. String(); sue. to. String(); X = joe; joe. set. Gpa(3. 5); X. to. String();