Example Code: [ bestval.cpp ]
Look at the walls in our lecture rooms. How many different kinds of socket can you see? Each is a different interface. An interface is a place where you can "plug things in" and so get to use things on the other side of the interface.
In programming, an interface is a list of function prototypes. You use the interface by calling functions in the list. The functions hide the code implementing them. You use functions in the list with out worrying (too much) about what they are.
If it quacks like a duck, and it walks like a duck; then it is a duck!
class Duck
{ public:
void walk();
string quack()const;
};
Notice the phrases: member function, constructor, mutator function, accessor function.
Example code: [ product1.cpp ]
class Card
{
public:
void read();
void print() const;
private:
int suit;
int rank;
};
int i;
void add(int n);
int value() const;
void add(int n);
int value() const;
void print() const;
Note: you can have functions that do not change data fields but don't have the const that tells the compiler that that don't keep things constant. Good programmers don't write functions like this -- ambiguous about whether they are accessors or mutators. Nether do the write functions that both change the data and return values...
(Notice the need to write a program to test the class...)
Can I borrow and open up your expensive watch?
I only want to know what time it is!