Professional Documents
Culture Documents
to
Basic Objects
CS 1037a – Topic 1
Overview
1–5
Procedural Programming
Code organised into procedures/functions
Any function can call any other function
(functions are ‘global’)
global
int student_grades[62];
functions
1–9
Compound Variables
For CPU, these programs are identical...
struct Point {
two compound
int x, y;
variables variable
};
p1_x p1_y p1
1–11
struct Defines New Type
Not new variable. New variable type.
After, can declare new vars of that type.
char, Blah p1; // COMPILER ERROR: what is a 'Blah' ??
int,
// Sprechen Sie C++?
float,...
struct Blah {
int x, y; // Oh, so THAT's what a 'Blah' is...
};
char,
int,
float,
Blah p2; // OK, I'll reserve 8 bytes for 'p2'
Blah,...
Blah p3; // OK, and another 8 bytes for 'p3'
Compound Variables };
draw_card(card_x, card_y,
card_suit, card_denom);
example
output
1–14
With Compound Variables
Simplifies interface; more expressive code
struct Card { // define a grouping of data
int x, y;
char suit, denom;
};
void draw_card(Card c) {
fill_rect(c.x, c.y, c.x+40, c.y+80);
draw_char(c.x, c.y, c.denom);
draw_char(c.x+10, c.y, c.suit); (5 of Hearts)
}
draw_card(card);
example
output
1–15
Arrays vs Structures
“But Prof, can’t I just use an array?”
int rect[4]; // a Rect is just 4 ints
rect[2] = 40; // set right <-- 40
rect[3] = 80; // set bottom <-- 80
draw_rect(rect); // design draw_rect to take array
1–17
Compound Variables
‘Structure’ suggested by names Structure explicit
struct Card {
int x, y;
char suit, denom;
};
int card_x, card_y;
char card_suit, card_denom; Card card;
1–18
Important Benefit of Struct
(repeated)
b
1–19
Part B
Example of basic pass-by-reference:
// add (dx,dy) to p's coordinates
void shift_by(Point& p, int dx, int dy);
Two int variables will exist: One int variable will exist:
1. b local variable to main 1. b local variable to main
2. x local variable to negate 2. x is really b with a ‘mask on’
b 1–22
Example Motivation
A function returning one value is easy, e.g.
int minimum(int a, int b) {
if (a < b) return a;
return b;
}
Problematic example...
int px = 1, py = 4;
(1,4)
// reflect (px,py) along line x=y
int temp = px;
swaps
px , py
px = py; (4,1)
py = temp;
a
1–28
References Are Important for
Procedural Programming
Can help code readability, function design
Can be more efficient than copy (e.g. big structs)
struct Card { // Card with its own bitmap
int x, y;
char suit, denom;
unsigned char bitmap[80][40][3]; // 40x80 RGB image
};
// now... do you want draw_card(Card c) or draw_card(Card& c)?
1–31
Problem: Repetitive Code
Repetitive code in function definitions
associated with same struct
bool rect_contains(Rect& rect, Point p) {
return rect.l <= p.x && p.x < rect.r &&
rect.t <= p.y && p.y < rect.b;
}
1–32
Solution: Member Functions
C++ lets you do exactly same thing but
omit code ‘obvious’ from Rect& context
bool Rect::contains(Point p) {
return l <= p.x && p.x < r &&
t <= p.y && p.y < b;
}
int Rect::area() {
return (r - l)*(b - t); // where will l,t,r,b come from?
} // don't worry, explained soon
int Rect::perimeter() {
return 2*(r - l) + 2*(b - t);
}
1–33
Functions vs Member Functions
Procedural style Object-oriented style
struct Rect { struct Rect {
int l, t; int l, t;
int r, b; int r, b;
};
bool rect_contains(Rect& rect, Point p); bool contains(Point p);
int rect_area(Rect& rect); int area();
int rect_perimeter(Rect& rect); int perimeter();
};
global function declarations member function declarations
void Point_shift_by(Point& p, int dx, int dy) { void Point::shift_by(int dx, int dy) {
p.x += dx; x += dx;
p.y += dy; y += dy;
} }
Point p; Point p;
same
size!
1–38
Object Oriented is Soft. Eng.
Object-oriented (OO) language features
can help, or they can complicate.
• Inheritance struct Circle: public Shape {
private:
• Information hiding float m_radius;
(Encapsulation) };
overview
1–44
struct Card {
int x, y;
char suit, denom;
};
Point doesn’t really need custom “copy”. But, later we will use classes 1–49
where “raw” copying of values for member variable is not good enough!!!
More Advanced Constructors
Member variables must also be constructed
class Rect {
public:
Point a, b; // Point class has its own constructor!
Rect();
Rect(Point tl, Point br);
};
overview
1–52
Some standard operators
Some standard operators are defined for
variables of standard types like int, float, char,…
- operator+, operator-, operator+=, operator*, operator>, operator<= …
- operator= (copy or assignment operator)
1-54
Overloading Operators
C++ allows to define (overload)
any operator for any class of objects!
Operator overloading can simplify code
making it read like basic math. For example:
operator+ can be defined for - two Points (vector addition)
- Rect and Point (e.g. shifting Rect by a vector)
1-58
Overloading Operators
some operators could be class members,
while others could be global operators
declarations inside file Point.h
class Point {
int x, y; copy operator
...
void operator=(const Point& src); // some operators must be declared
void operator+=(const Point& dP); // as members of class Point
};
// while others (e.g. operator+) could be
Point operator+ (const Point& a, const Point& b); // declared globally