{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

day19 - Object-oriented concepts 1 Exercise Write a program...

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

1 Object-oriented concepts

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

View Full Document
2 Exercise Write a program to produce the following output: p1 is (7, 2) p1's distance from origin = 7.280110 p2 is (4, 3) p2's distance from origin = 5.000000 p1 is (18, 8) p2 is (5, 10) The formula to compute the distance between points ( x 1 , y 1 ) and ( x 2 , y 2 ) is: ( 29 ( 29 2 1 2 2 1 2 y y x x - + -
3 Solution #include <stdio.h> #include <math.h> int main() { // create two points int x1 = 7; int y1 = 2; int x2 = 4; int y2 = 3; // print each point printf("p1 is (%d, %d)\n", x1, y1); double dist1 = sqrt(x1 * x1 + y1 * y1); printf("p1's distance from origin = %lf\n", dist1); printf("p2 is (%d, %d)\n", x2, y2); double dist2 = sqrt(x2 * x2 + y2 * y2); printf("p2's distance from origin = %lf\n", dist2); // move points and then print again x1 += 11; y1 += 6; printf("p1 is (%d, %d)\n", x1, y1); x2 += 1; y2 += 7; printf("p2 is (%d, %d)\n", x2, y2); }

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

View Full Document
4 Writing functions on points There are lots of opportunities to create functions that take a point's information (x and y) and does something useful. Example: void printPoint(int x, int y) { printf("(%d, %d)", x, y); }
5 What if there were more than just x and  y? Suppose we were keeping track of student data Name Birthdate Gender Year entered List of courses taken Grades in those courses List of current courses

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

View Full Document
6 Print student data Would a function to print a student's information look like this? void printStudentInfo(char *pName, int birthMonth int birthDay, int birthYear, int gender, int yearEntered, int coursesTaken[], int grades[], int currentCourses[], . ..) As you can tell, this can get pretty ugly.
7 Classes and objects

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

View Full Document
8 Objects So far, we have seen: variables , which represent data (categorized by types ) functions , which represent behavior object: An entity that contains both data and behavior. There are variables inside the object, representing its data. There are functions inside the object, representing its behavior. Object functions are commonly referred to as methods . class: Category or type of object We group objects into classes.
9 Class vs. object Theoretical examples: A class Person could represent objects that store a name, height, weight, hair color, IQ, etc… A class Laptop could represent objects that store speed, screen size, color, dimensions, brand, etc…

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

View Full Document
10 Classes are like blueprints Music player #1 state:   station/song,   volume, battery life behavior:   power on/off   change station/song   change volume   choose random song Music player #2 state:   station/song,   volume, battery life behavior:   power on/off   change station/song   change volume   choose random song Music player #3 state:   station/song,   volume, battery life behavior:   power on/off   change station/song   change volume   choose random song
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 37

day19 - Object-oriented concepts 1 Exercise Write a program...

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

View Full Document
Ask a homework question - tutors are online