topic01_Procedural_vs_Objects

topic01_Procedural_vs_Objects - From Procedural Style to...

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

View Full Document Right Arrow Icon
From “Procedural Style” to Basic Objects CS 1037a – Topic 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
What You Should Know Variables, built-in types Arrays – but just basics Procedures, if-else statements, loops 1–2 int num_students = 64; int student_grades[64]; student_grades[7] = -100; // die cheater die int average_grade() { int sum = 0; for ( int i = 0; i < num_students; ++i) sum += student_grades[i]; return sum / num_students; }
Background image of page 2
What You Will Learn A ) Structures and why they‟re helpful B ) Very basic pass-by-reference in C++ C ) Why structures naturally lead to objects 1–3 struct Point { int x, y; }; // now 'Point' is a type, like float // add (dx,dy) to p's coordinate void shift_by(Point& p, int dx, int dy); struct Point { int x, y; void shift_by( int dx, int dy); // a 'member function' };
Background image of page 3

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

View Full DocumentRight Arrow Icon
Procedural Programming You already know basics of this style. 1–4 float vector_length( float x, float y) { return sqrt(x*x + y*y); } float len = vector_length(3.0f,4.0f); // len <-- 5.0 procedure definition (aka “function definition” ) argument declarations (aka “formal parameters” ) procedure call (aka “function call” ) arguments (aka “actual parameters” )
Background image of page 4
Procedural Programming Code organised into procedures/functions Any function can call any other function (functions are „ global‟ ) C/C++ defines „scope‟ of variables. .. 1–5 int student_grades[62]; void adjust_grades( int amount) { int i; for (i=0; i < 62; ++i) student_grades[i] += amount; } 1. global variables 2. local variables
Background image of page 5

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

View Full DocumentRight Arrow Icon
Procedural Programming Lots of software written in procedural style • Linux , Windows kernels (in C) • Apache web server (in C) • Javascript , Python interpreters (in C) Five gazillion other things (mostly C) Sounds good, so why talk of objects ? • Many „software engineering‟ reasons (blah) • We only present one practical line of reasoning • Start by understanding structures 1–6
Background image of page 6
Part A Example of a structure: Why are structures helpful? 1–7 struct Point { int x, y; }; // now 'Point' is a type, like float
Background image of page 7

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

View Full DocumentRight Arrow Icon
Compound Variables For CPU, these programs are identical ... 1–8 int p1_x, p1_y; void main() { p1_x = 5; p1_y = 10; } struct Point { int x, y; }; Point p1; void main() { p1.x = 5; p1.y = 10; } ? ? ... ... ? ? ? ? ? ? ? ? ? p1_x p1_y ? ? ... ... ? ? ? ? ? ? ? ? ? p1 5 10 5 10 memory after program runs memory after program runs memory memory two variables compound variable
Background image of page 8
Compound Variables Same with these; identical „machine code‟ 1–9 int p1_x, p1_y; int p2_x, p2_y; void main() { p1_x = 5; p1_y = 10; p2_x = p1_x; p2_y = p1_y; } struct Point { int x, y; }; Point p1; Point p2; void main() { p1.x = 5; p1.y = 10; p2 = p1; }
Background image of page 9

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

View Full DocumentRight Arrow Icon
struct Defines New Type Not new variable. New variable type . A fter , can declare new vars of that type. A struct definition defines a new type that is an aggregate (compound) of existing types 1–10 Blah p1; // COMPILER ERROR: what is a 'Blah' ?? // Sprechen Sie C++? struct Blah { int x, y; // Oh, so THAT's what a 'Blah' is. .. }; Blah p2; // OK, I'll reserve 8 bytes for 'p2' Blah p3; // OK, and another 8 bytes for 'p3' char, int, float,. ..
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 49

topic01_Procedural_vs_Objects - From Procedural Style to...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online